How to stop a moving game object as soon as it collides with another game object?

Viewed 10377

I have a ring as game object which I move on the y axis up and down with the keyboard keys.

As information: Both game objects have a rigidbody attached as well as a collider.

Here some code:

// get key up and the ring will move upwards
    if (Input.GetKey (KeyCode.UpArrow)&&(shouldMove))
    {
        transform.position += Vector3.up * speed * Time.deltaTime;
    }

    // get key down and the ring will move downwards
    if (Input.GetKey (KeyCode.DownArrow))
    {
        transform.position += Vector3.down * speed * Time.deltaTime;
    }

Now I would like do disable the movement of this ring as soon as it hits another game object. I tried the the OnCollisionEnter function which gives me the information that I hit an object (using debug.log) however I can continue move it and pushing the game object which was hidden...

void OnCollisionEnter(Collision collision) {


    if (collision.gameObject.name == "Sphere") {            
        Debug.Log ("Hit Sphere");

    }

}
1 Answers
Related