- Home /
Unity 5 object collision not detecting
So I've just started using Unity and new to C#.
I'm running into a problem where my sphere (player) wont detect collision as it touches other objects (runs right through).
I wanted to have the player move units by units corresponding to my left, right, up, down keyboard inputs instead of physics.
heres the code:
public float distance;
void FixedUpdate(){
if (Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") > 0) {
transform.Translate (distance, 0.0f, 0);
} else
if (Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") < 0) {
transform.Translate (-distance, 0.0f, 0);
} else
if (Input.GetButtonDown("Vertical") && Input.GetAxisRaw("Vertical") > 0) {
transform.Translate(0, 0, distance);
}else
if(Input.GetButtonDown("Vertical") && Input.GetAxisRaw("Vertical") < 0){
transform.Translate(0, 0, -distance);
}
}
did you add the rigidbody component into your player object?
Answer by jodev · Apr 27, 2015 at 02:01 PM
You haven't really told how you have set up your player and other objects. This matters because in certain circumstances collisions won't be detected when using OnCollisionEnter(). In particular, if "both objects are kinematic Rigidbodies then OnCollisionEnter, etc, will not be called" (see manual, under Script actions taken on collision).
You'll want to add a RigidBody to your player. If you don't want your player to be affected by gravity, you can check the Is Kinematic checkbox. Then, instead of using the OnCollision...() methods, you may find the OnTrigger...() methods are more suitable for you. The other colliders won't necessarilly need rigid bodies in that case. Do make sure the Is Trigger checkbox is checked for them though.