- Home /
My Unity Collision Wont Trigger Even with Rigidbody Added
I am new to Unity and I have set up a scene where if my player (An Aircraft) collides with the box collider then it will trigger a message. But whenever it collides it does not show up any message.
Heres the script
public GameObject cube;
void OnCollisionEnter(Collision col)
{
if (col.gameObject.CompareTag("Player"))
{
Debug.Log("Hi");
}
}
Answer by Zoedingl · Jun 21, 2020 at 01:48 PM
Has the box collider also a kinematic Rigidbody? And has the player also a collider and a Rigidbody? You must add these.
Answer by exp626stitch · Dec 16, 2020 at 06:42 PM
@manavento736 If the object is moving fast:
Vector3 lastPos;
public void Start() {
lastPos = transform.position;
}
public void FixedUpdate()
{
lastPos = transform.position;
RaycastHit[] hits = Physics.RaycastAll(new Ray(lastPos, (transform.position - lastPos).normalized), (transform.position - lastPos).magnitude);
//Do something with it using hit
transform.position = hit[1].point;
}
If a collision dosnt occur in a visible frame, it didnt happen, so the raycast goes between where you were and where you are, and if there is a collision, then it teleports the object to the collision point, although I would recommend adding a offset based on half your size.