所以,我一直在遵循这个指南:
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; //Object health
float invulnTimer = 0.25f; //Time in which the gameObject is invulnerable
int correctLayer; // save the original layer of the object
void start()
{
correctLayer = gameObject.layer;
gameObject.SetActive(true);
}
void OnTriggerEnter2D()
{
Debug.Log("Trigger!");
health--;
invulnTimer = 2f;
gameObject.layer = 10; //put object in invulnerable layer
}
// Update is called once per frame
void Update()
{
invulnTimer -= Time.deltaTime;
if (invulnTimer <= 0)
{
gameObject.layer = correctLayer; //returns object to original layer (doesnt work)
}
if (health <= 0)
{
Die();
}
}
void Die()
{
Destroy(gameObject); // Destroys object
}
}
我不知道为什么start函数不保存正确的层,即使在unity中,我已经将对象定义为它们的反射层。
注意:我已经使对象运动并检查了IsTrigger。当第一行代码未被使用时,它确实起作用。