- Home /
Help here, my code isnt working..
What i want to do here is when my 1st object is exactly in the position of my 2nd object, the 2nd object will move from the previous position of the 1st object..
kinda stuck in here..
function Update ()
{
PositionChanging();
}
function PositionChanging ()
{
if(c1.transform.position==c2.transform.position){
var positionA : Vector3 = new Vector3(-1.5, 2, -4.704057);
newPosition = positionA;
c2.transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime);
}
}
What is working and what is not ?
note : comparing two vectors with == may be hard. Have a look at this question.
NOTE: If the title of your post could easily apply to 95% of the questions on the site, it's a really poor title.
Answer by flaviusxvii · Aug 21, 2013 at 06:04 PM
Lerp is going to need to run over the course of many frames or ticks to get a nice movement. See how it works here: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html
This also means that you can't ONLY take action when c1 and c2 are in the same position. Lerp needs to be called EVERY UPDATE as it is moving, so they clearly won't have the same position after you get it moving.
When the vectors are equals, you can valuate a boolean to true, and after that, still in the Update() method, use Lerp if the boolean is true, then valuate it to false when your object is at the wanted position.