Box goes through walls

So I’ve been trying to make a box follow the cursor and collide with objects but I’ve run into an issue I cant figure out… In this script Everything works fine except that when The box that it is attached to collides its stops, but once it has stopped when i move my cursor farther away it starts to teleport into and eventually past the other box it was collided with… Any ideas?

Here is the script:

using UnityEngine;
using System.Collections;

public class CursorController : MonoBehaviour {

private Vector3 cursorPos;
private Rigidbody body;
public float speed = 10f;
public float slow = 1f;
public float rate = 5f;

// Use this for initialization
void Start () {
	body = gameObject.GetComponent<Rigidbody> ();
}

// Update is called once per frame
void Update () {
	cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
	cursorPos.z = 10f;
	//transform.position = Vector3.MoveTowards(transform.position, cursorPos, speed);
	body.AddForce((cursorPos - transform.position) * speed);
	body.velocity = body.velocity/slow;
	slow = Vector3.Distance (transform.position, cursorPos) / rate;
}

}

try adding mesh collider component to the walls. Make sure you checked on Convex.

first, check your all colliders that they exist :slight_smile:
second, check you rigidbody interpolation checkbox (this helps with this kind of weird behavior at cost of more CPU power)

and last but not least : “body.AddForce((cursorPos - transform.position) * speed);” this is a wors case scenario of using add force in Update() , atleast multiply that “speed” with Time.DeltaTime.