# Objects Collision

Discussion in 'Physics' started by entermax, Nov 7, 2018.

1. ### entermax

Joined:
May 30, 2016
Posts:
3
hi guys, am working on a game where I want to destroy my object when it gets hit with another.
make note it is 2D car racing on a track.
the thing is it seems the collision is not being detected. after searching I came a cross the idea of that using "transform" is the reason for that and that i have to use "moveposition" for the rigidbody2D, is this right or wrong?
and this is the code that I am using to control the car, what are the changes I need to do for it to detect the collision?

Code (CSharp):
1. public class CarControl : MonoBehaviour {
2.
3.     float speedForce = 25f;
4.     float maxSpeed = 25;
5.     float torqueForce = -200f;
6.     float maxStickinessVelocity = 10f;
7.
8.     void FixedUpdate()
9.     {
10.         Rigidbody2D rb = GetComponent<Rigidbody2D>();
11.
12.
13.
14.
15.         // driftfactor manipulates the car's velocity
16.         float driftFactor = 1f;
17.
18.
19.         //make the care drift on high speed
20.         if (rightVelocity().magnitude > maxStickinessVelocity)
21.         {
22.             driftFactor = 0.8f;
23.         }
24.
25.
26.
27.         //make the car stops drifting on low speed
28.         if (ForwardVelocity().magnitude > 25f)
29.         {
30.             if (rightVelocity().magnitude < minStickinessVelocity)
31.             {
32.                 driftFactor =  0.3f;
33.             }
34.         }
35.
36.         //velocity of the car
37.         rb.velocity = ForwardVelocity() + rightVelocity() * driftFactor;
38.
39.         //accelerate input control
40.         if (Input.GetButton("Accelerate"))
41.         {
44.
45.         }
46.
47.         //brake input control
48.         if (Input.GetButton("Brake"))
49.         {
52.
53.         }
54.
55.         float t = Mathf.Lerp(0, torqueForce, rb.velocity.magnitude /25);
56.
57.         rb.angularVelocity = (Input.GetAxis("Horizontal") * t);
58.
59.
60.     }
61.
62.
63.     //calculates forward velocity
64.     Vector2 ForwardVelocity()
65.     {
66.         return transform.up * Vector2.Dot(GetComponent<Rigidbody2D>().velocity, transform.up);
67.     }
68.
69.     //calculates side velocity
70.     Vector2 rightVelocity()
71.     {
72.         return transform.right * Vector2.Dot(GetComponent<Rigidbody2D>().velocity, transform.right);
73.     }
74.
75.     void OnTriggerEnter(Collider other)
76.     {
77.         if (other.gameObject.CompareTag("Track"))
78.         {
79.             Debug.Log("Track");
80.         }
81.     }
82. }

2. ### entermax

Joined:
May 30, 2016
Posts:
3
Extra info: the car do collide with walls and other objects. but the OnCollisionEnter method never seem to work.
this one of the scripts i tried to use.
and yes the tag is written correctly

Code (CSharp):
1. public class CarInteraction : MonoBehaviour {
2.
3.     void OnCollisionEnter(Collision col)
4.     {
5.         if(col.transform.CompareTag("Obstacle"))
6.         Debug.Log("hit");
7.     }
8. }

3. ### entermax

Joined:
May 30, 2016
Posts:
3
ok i got what was wrong, needed to use OnCollisionEnter2D.