- Home /
Duplicate Question : http://answers.unity3d.com/questions/623453/stopping-moving-object-going-through-a-wall.html
How to stop my object from going through another object
Hi
I have an object that moves towards another object, a probe that moves towards a cube, I used a script for movement my problem is that my probe doesn't stop when it reaches the cube but it keep going through it and I want it to stop. I've tried box collider, mesh collider rigid-bodies on both objects with no luck so far, my probe is inside a gameObject.
Any tips or guidance on how to stop my object going through the cube is really appreciated.
Here is the script: var smooth : float = 1.0; // Deter$$anonymous$$es how quickly object moves towards position private var targetPosition : Vector3; var speed : float = 60.0;
function Start() { targetPosition = transform.position; }
function Update () {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)) {
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
var targetPoint = ray.GetPoint(hitdist);
targetPosition = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
}
var dir : Vector3 = targetPosition - transform.position;
var dist : float = dir.magnitude;
var move : float = speed * Time.deltaTime;
if(dist > move){
transform.position += dir.normalized * move;
}
else {
transform.position = targetPosition;
}
transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime;
}
I want my moving object to stop when it touches the cube. In other words I want my cube to be solid nothing can go through it.
Either use rigidbody.velocity and rigidbody.AddForce to move your character with a rigibody and colliders on it, or use Raycasts to detect if a wall or something is in front of your character, and if true, restrict X axis movement to only move away.
Answer by ankush_Kushwaha · Jan 16, 2014 at 01:04 PM
I will suggest you to use add force on rigid body but anyway, If you want to use transform.position that is
if(dist > move){ transform.position += dir.normalized * move; }
then you can use IstriggerEnter() function to detect your collision and when collision occurs you just need to stop your code
if(dist > move){ transform.position += dir.normalized * move; }
to execute.
you can put a condition in your if()
if(dist > move && collisionOccured== false){ transform.position += dir.normalized * move; }
and In onTriggerEnter() write OnTriggerEnter(){ collisionOccured = true; }
. For triggerEnter your both bodies must have collider on them and atleast one of them should be a rigid body.
Answer by Professor Snake · Jan 09, 2014 at 11:59 AM
You seem to be directly modifying its transform.position, which will not take any physics into consideration. (transform.position += //your code)
Try adding a rigidbody to the object that you are trying to move and use Rigidbody.AddForce.
How do I add rigidbody.AddForce and is it going to be attached to the moving object or the cube.
I am really new to this so help would be really appreciated.
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html
It, as the name implies, adds a force to a rigidbody. It should replace the transform.position+= //code line, since it's going to be used as a method of moving your object while taking physics into consideration.
Sorry would you $$anonymous$$d adding it to the script that i posted as i am afraid I could ruin it as I am really bad in coding. Cheers
In every place that it says transport.position I replaced it with rigidbody.AddForce. Still not working.