- Home /
How do I know that the object has reached its goal?
I have an object which, when I press a key, should move to the target, how can I find out in the script that the object has reached its goal?
Answer by highpockets · Mar 20, 2019 at 05:14 PM
Use lerp and when the time value reaches 1, you have reached your target:
float timeCount = 0.0f;
void Update(){
object.transform.position = Vector3.Lerp(object.transform.position, targetPos, timeCount);
if(timeCount >= 1.0f){
//target reached
}
timeCount = timeCount + Time.deltaTime;
}
The way I wrote the code is so the timeCount will reach 1 in 1 second, but you can of course change that by changing the way the timeCount updates. For example:
timeCount = timeCount + (Time.deltaTime * 0.5f);
The above will ins$$anonymous$$d reach 1 in 2 seconds
Thanks for the answer! Only vibrates here error error CS0117: 'object' does not contain a definition for 'transform'
it was just an example, but if this script is on the object you want to move, you can just put:
transform.position = Vector3.Lerp(transform.position, targetPos, timeCount);
another error CS1503 error: Argument 2: cannot convert from 'UnityEngine.Transform' to 'UnityEngine.Vector3'
Why did somebody give a negative vote to my answer? I imagine somebody else who gave an answer is displeased, I was simply providing an additional solution here since there are many ways to skin a cat and some cats are easier to skin certain ways than others. People in the future will come to this post and see multiple solutions to accomplish something and everybody will want to approach the situation differently. The lerp function is a good answer to this question just as the other answers have their value. If somebody has a logical explanation why lerp is not a good approach, please explain? And if your explanation makes any sense at all, I will remove the answer..
not sure why, i upvoted because this is a correct way of doing this, just one thing, the code you posted is for non linear speed, i would suggest not doing it that way since managing the speed curve becomes really hard, if your idea was to make a linear movement you are missing a startpoint, that must be constant for all the movement duration.
@xxmariofer is right. $$anonymous$$y code will have the object slow down as it gets closer to the target. For linear, you would need to put a start position that stays constant for the duration of the move.
Answer by dan_wipf · Mar 20, 2019 at 03:23 PM
hm i’d use a Vector3.Distance for +- accurate equation of the target => if(Vector3.Distance(objectPosition,targetPosition) <0.01f)
or
if(objectPos == TargetPos)
but sometimes the target position is not the same as object position, so you use the first code
Careful, as equality of structs is equality of values for value types and references for reference types.
edit: see hellium's reply
Comparing two vectors is fine. Unity has overloaded the ==
operator:
public const float kEpsilon = 0.00001F;
public static bool operator==(Vector3 lhs, Vector3 rhs)
{
// Returns false in the presence of NaN values.
return Sqr$$anonymous$$agnitude(lhs - rhs) < kEpsilon * kEpsilon;
}
Answer by dargonknight · Mar 20, 2019 at 03:29 PM
@Trild123787898 i agree with @dan_wipf 's answer but an other option would be to simply add a trigger and check the on trigger function
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Target")
{
//ur action goes here
}
}
well yes, but this only works when the script is attached to the gameobject which is moving towards the target, or if the script is attached to the targets object
and it will be not that accurate aswell as vector3 /distance comparison.. the objects position will then be at the BB’s of the collider somwhere: for example:
if you have a size one cube as object to move and aswell as target. and it enter’s the trigger of the targets cube, from starting pos vector3(-10,0,0) to target(vector3(0,0,0) the position of enterinf the trigger zone will then be vector3(0.5,0,0) and the actual position of the object to move will be vector3(1,0,0)
@dan_wipf using a small sphere collider can fix the problem, when giving advise to beginners i believe that it's best to let them start by easy things that can see visually, understand and learn from to do bigger things my option works, it can be seen in the editor, it introduces the trigger etc... that's why i sent it.
Your answer
Follow this Question
Related Questions
make a gameobject follow an array of targets 1 Answer
Look at enemy 1 Answer
how to re-assign transform/rigidbody at runtime ? 0 Answers
Best Way to make a character move 1 Answer