ThirdPersonController collision not calling OnTriggerEnter event

自作多情 提交于 2019-12-02 16:29:18

问题


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour {

    public Transform target;

    // Update is called once per frame
    void Update ()
    {

    }

    private void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.tag == "Test")
        {
            this.transform.position = target.position;
        }
    }
}

I have a ThirdPersonController and i want it to collide with a cube or cylinder. The script is attached to the ThirdPersonController.

I tried to add either to the cylinder or the cube Rigidbody turned on/off the Use Gravity and the Is Kinematic but nothing. It's not getting to the event.


回答1:


ThirdPersonController uses CharacterController and OnControllerColliderHit is used for that not OnTriggerEnter.

Note that you must move it with the Move function not directly by its transform in order for OnControllerColliderHit to be called.

void OnControllerColliderHit(ControllerColliderHit hit)
{

}



回答2:


Every thing is correct but the thing wrong here is you are changing the position of the object which is colliding with a second object , but the thing id the thing is the collider is already there..... As it's alternative try to print any statement when collision happens like this

private  void OnTriggerEnter(Collider other)
{
  if (other.tag==your_tag)

{
print("message");
}
}


来源:https://stackoverflow.com/questions/43955160/thirdpersoncontroller-collision-not-calling-ontriggerenter-event

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!