- Home /
Making the object collide while moving in the forward dir. (Vector3.forward)
i'm making an object move in the forward direction i used this
if (target)
{
var distance = Vector3.Distance(transform.position, target.position);
transform.LookAt(target);
if (distance > 2)
{
transform.position += transform.TransformDirection(Vector3.forward * Time.deltaTime * 10);
}
}
but the problem is that i want the object to collide with the other object but it doesn't
please tell me how to do it
A simple question, but it has to be asked: do you have colliders on the two objects that you are trying to collide?
What @NOAA_Julien asks + does the body you are trying to move have a rigidbody component?
Answer by Anxo · Aug 18, 2011 at 05:45 PM
You are telling the object where to go instead of allowing it to figure out where it needs to go. When you tell it
Transform.position +=...
It tells the object where it needs to be. So even if it collides with something, it can not stop because you are telling it "go here"
If you have a rigidbody and you wish to move it forward, but you want it to stop or bump into things that changes its velocity or direction, you will have to use "AddForce" You can check the documentation on how to use it but its something like
private var ThisRigid : Rigidbody;
function Start(){
ThisRigid = rigidbody;
}
function Update(){
ThisRigid.AddForce(Vector3.Forward*10*Time.deltaTime);
}
or something liek that, again check the documentation, I am not around Unity.
If you have a character controller you need to use the Move function. in order to have it collide with stuff. Check Character Controller if this is the case.
Thnx Dude, i fixed the problem an hour ago, i used the $$anonymous$$ove Function, but i marked ur question as a correct question ; cuz its a correct question :)
Your answer
Follow this Question
Related Questions
Camera Colliding with Terrain 1 Answer
How to let the player collide with animated objects? 0 Answers
How to check if an object is colliding with another? 3 Answers
Moving Platform Collisions with Custom Movement Script 0 Answers
Complication with script 2 Answers