我希望我的播放器与对象舱发生碰撞。 此动作应该破坏胶囊并向玩家添加速度值10。
但是这段代码不起作用:
public class PlayerController : MonoBehaviour {
public KeyCode moveL;
public KeyCode moveR;
public float horizontal = 0;
public int laneNum = 2;
public string controllocked = "n";
public float speed;
void Update ()
{
GetComponent<Rigidbody>().velocity = new Vector3(horizontal, GM.verticalVelocity, speed);
if ((Input.GetKeyDown(moveL)) && (laneNum > 1) && (controllocked == "n"))
{
horizontal = -2;
StartCoroutine(StopSlide());
laneNum = laneNum - 1;
controllocked = "y";
}
else if ((Input.GetKeyDown(moveR)) && (laneNum < 3) && (controllocked =="n"))
{
horizontal = 2;
laneNum = laneNum + 1;
StartCoroutine(StopSlide());
controllocked = "y";
}
}
void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag == "lethal")
{
Destroy(gameObject);
}
if (other.gameObject.name == "Capsule")
{
Destroy(other.gameObject);
speed = 10;
}
}
IEnumerator StopSlide()
{
yield return new WaitForSeconds(.5f);
horizontal = 0;
controllocked = "n";
}
到目前为止我尝试的是speed += 10
和speed++
都没有效果。
答案 0 :(得分:2)
嗯,首先尝试检查你的播放器,你在播放器中使用什么对撞机类型? 确保检查对撞机组件中的触发器并将刚体添加到其中。 胶囊物体上必须有刚体。 希望它有所帮助。 如果要使用triggerenter,请使用OnTriggerEnter(Collider collider)。如果未选中触发器,则碰撞进入工作