Prevent colliders intersecting.

I’ve got a cube with a collider and rigid body attached that follows the mouse position around the screen so that the centre of the cube is where the mouse position is. This means that if I move the mouse onto the side of another object, half of the cube is inside the other object. I’ve tried playing around with rigid body settings however I can’t stop the cube following the mouse to intersect with other objects. Any ideas?

Forgot to mention, when I do use a rigid body with gravity on, when the cube hits another object it just flies off in a different direction, even after playing with all the rigid body settings.

If you want it to collide, you can not use Transform.position or Transform.Translate because that means you are teleporting the object to that position.

You need to use CharacterController.Move or .SimpleMove, or use Rigidbody.AddForce, Rigidbody.Moveposition to make sure that an object collides with the other.


Code Snippet: Add Rigidbody to your cube and then attach the following script:

using UnityEngine;
using System.Collections;

public class MoveWithMouse : MonoBehaviour {



	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	

		Vector3 v3 = Input.mousePosition;
		v3.z = 10.0f;
		v3 = Camera.main.ScreenToWorldPoint(v3);

		this.gameObject.rigidbody.MovePosition(v3);
	
	}



}