- Home /
How can I detect collisions without rigidbodys.
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.
Answer by JustinMeyer · Aug 30, 2017 at 03:18 PM
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);
}
}
Answer by TheSOULDev · Aug 30, 2017 at 01:22 PM
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.
Answer by fleischverpackung · Aug 30, 2017 at 01:49 PM
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.
Answer by Senuska · Aug 30, 2017 at 02:14 PM
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 (link) 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.
Your answer
Follow this Question
Related Questions
Similar movement to a game (look description) 1 Answer
Distribute terrain in zones 3 Answers
How to detect collision on only one side of an object? [C#] 4 Answers
How to make a cube (go to a certain position) after colliding with a plane? 1 Answer
How do I make my character controller jump when I use this code? 0 Answers