- Home /
How to stop an Object from Moving When It Hits Collider
Hi. As the title of my question states, I want to stop an object from moving once it hits a collider using onTriggerStay(not using the built in physics engine). My question is how can i do this if there is no rigid body attached to the object?
Answer by Bunny83 · May 07, 2012 at 12:08 PM
If you want to use any collision functions (OnCollision Enter / Stay / Exit or OnTrigger Enter / Stay / Exit) you should attach a rigidbody to the moving object no matter if you use physics or not. If you don't need physics make it a kinematic rigidbody.
Next thing is, if the object isn't moved by the physics system (forces / gravity) then you probably move the object in your of your scripts. Well if you don't want to move the object anymore you have to tell your script to not move the object, or just disable the script.
Answer by venkspower · May 07, 2012 at 12:44 PM
Considering the fact that you are moving in x direction. You can do this:
var a : int = 0;
var movingObject : GameObject;
var obstacle : GameObject;
function Update(){
if(a == 0){
movingObject.transform.position.x++;
}
}
function OnCollisionEnter(collision : Collision){
if(collision.gameObject.tag == "obstacle"){ //tag of the object "obstacle"
a = 1;
}
}
Your answer
Follow this Question
Related Questions
OnTriggerEnter doesn't work with fast animations/movements 2 Answers
Affecting multiple objects through trigger? 0 Answers
OnCollisionExit() firing when object has not exited 0 Answers
Collision stops working at certain situations. Help? 1 Answer
How to detect how far another collider is intersecting another! 1 Answer