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): public class CarControl : MonoBehaviour { float speedForce = 25f; float maxSpeed = 25; float torqueForce = -200f; float maxStickinessVelocity = 10f; void FixedUpdate() { Rigidbody2D rb = GetComponent<Rigidbody2D>(); // driftfactor manipulates the car's velocity float driftFactor = 1f; //make the care drift on high speed if (rightVelocity().magnitude > maxStickinessVelocity) { driftFactor = 0.8f; } //make the car stops drifting on low speed if (ForwardVelocity().magnitude > 25f) { if (rightVelocity().magnitude < minStickinessVelocity) { driftFactor = 0.3f; } } //velocity of the car rb.velocity = ForwardVelocity() + rightVelocity() * driftFactor; //accelerate input control if (Input.GetButton("Accelerate")) { rb.AddForce(transform.up * speedForce); //rb.AddForceAtPosition to add force at wheels } //brake input control if (Input.GetButton("Brake")) { rb.AddForce(transform.up * -speedForce /3f); //rb.AddForceAtPosition to add force at wheels } float t = Mathf.Lerp(0, torqueForce, rb.velocity.magnitude /25); rb.angularVelocity = (Input.GetAxis("Horizontal") * t); } //calculates forward velocity Vector2 ForwardVelocity() { return transform.up * Vector2.Dot(GetComponent<Rigidbody2D>().velocity, transform.up); } //calculates side velocity Vector2 rightVelocity() { return transform.right * Vector2.Dot(GetComponent<Rigidbody2D>().velocity, transform.right); } void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag("Track")) { Debug.Log("Track"); } } }
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): public class CarInteraction : MonoBehaviour { void OnCollisionEnter(Collision col) { if(col.transform.CompareTag("Obstacle")) Debug.Log("hit"); } }