How can I detect collisions without rigidbodys. I’ve got a player cube moving with c# scripting using Input.GetAxis. and I spawn enemy cubes on the right side of the screen that move left towards the player.
I can’t seem to figure out how to display a message when the player makes contact with the enemy cubes. I don’t want any rigidbodys because the cause the cubes to do weird things. If possible I only want to use the Box Colliders.
Hear are some screen shots with my components:
And hear is the player script that is suppose to detect the collisions.
public float moveSpeed = 10f;
void Update () {
transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis ("Vertical")* moveSpeed);
transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis ("Horizontal")* moveSpeed);
}
void OnTCollisionEnter(Collision collider){
Debug.Log (gameObject.name + "hit " + collider.gameObject.name);
}
If anyone can please help me I’ll be very grateful.
I fixed it! all I had to do was make my player box collider “isTrigger” and un-check “isTrigger” for my enemy cubes. Here is my final script:
public float moveSpeed = 10f;
void Update () {
transform.Translate(Vector3.forward * Time.deltaTime * Input.GetAxis ("Vertical")* moveSpeed);
transform.Translate(Vector3.right * Time.deltaTime * Input.GetAxis ("Horizontal")* moveSpeed);
}
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Enemy") {
Debug.Log (gameObject.name + " hit " + other.gameObject.name);
}
}
To detect collision without rigidbodies you would have to write your own physics system that uses rigidbodies you named differently.
Using rigidbodies really doesn’t make objects behave in an unintended way. There is also no way you can use built-in colliders for collision without rigidbodies, because they work in tandem.
This is the reason why we use an engine.
Because it massively helps to do stuff.
Like hit detection.
Or lightning.
I recommend using colliders.
Rigidbodies are required for Unity’s collision system. You can disable some of the physics on the cubes to get a desired behavior. If you are moving the cubes from a script I would slap a rigid body on there and check the “Is Kinematic” option.
Alternatively if you really wanted to you could use Physics.Raycast (1) to see if you are hitting the player by setting up a really short ray, but I would not do this since it would have a performance cost with a lot of enemies in the scene.