- Home /
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;
}
}
Answer by Abirami-Govindarajan · Jul 31, 2017 at 05:54 AM
try adding mesh collider component to the walls. Make sure you checked on Convex.
Answer by NorthStar79 · Jul 31, 2017 at 11:12 AM
first, check your all colliders that they exist :) 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.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Script is moving player to the wrong position 2 Answers
My Raycast acts like it's hitting something when it isn't 0 Answers
How to hold objects in third person? 1 Answer
[3D] Top-Down Make Character move in the direction of the position he is currently looking at? 1 Answer