所以,我一直在遵循这个指南:
https://www.youtube.com/watch?v=RQyfHmf7v9s
制作一个太空射击游戏。
但似乎有一行代码(这一行:)
gameObject.layer = correctLayer;
每次都将我的对象转换为“默认”层。
这是代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("WaterEffects/ForCamera")]
public class HitHandler : MonoBehaviou {
public int health = 1;
float invulnTimer = 0.25f;
int correctLayer;
void start()
{
correctLayer = gameObject.layer;
gameObject.SetActive(true);
}
void OnTriggerEnter2D()
{
Debug.Log("Trigger!");
health--;
invulnTimer = 2f;
gameObject.layer = 10;
}
void Update()
{
invulnTimer -= Time.deltaTime;
if (invulnTimer <= 0)
{
gameObject.layer = correctLayer;
}
if (health <= 0)
{
Die();
}
}
void Die()
{
Destroy(gameObject);
}
}
我不知道为什么start函数不保存正确的层,即使在unity中,我已经将对象定义为它们的反射层。
注意:我已经使对象运动并检查了IsTrigger。当第一行代码未被使用时,它确实起作用。