【发布时间】:2017-11-17 09:25:14
【问题描述】:
所以,我一直在关注本指南:https://www.youtube.com/watch?v=RQyfHmf7v9s 制作太空射击游戏。
这个想法是“Enemy”层下的两个对象不会发生碰撞,但是当“Player”层下的对象与“Enemy”发生碰撞时,它会成为“无敌”层的一部分,不会与任何东西发生碰撞。
但似乎代码中的一行(这一行:)
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 函数没有保存正确的层,即使在统一中我已将对象定义为它们的反射层。
注意:我已经制作了 Kinematic 对象并检查了 IsTrigger。当第一个提到的代码行没有被使用时,它确实可以工作。
【问题讨论】:
标签: c# unity3d visual-studio-2017 layer