Hi, due to my boss asking to make a custom MoveTo in the scope of making the basic modules for a bigger project AND enabling me to get familiar with Unity (this was a few months ago when i began using Unity), i made the customised MoveTo. It’s pretty simple, but now that i have to change it to get a new behaviour, i’m a bit confused at what i wrote then. After reading it a few times i still think the code isn’t really good to use as the base for the new behaviour. Can anyone tell me if i’m right in thinking that the code isn’t written properly?
public float movementSpeed = 1; //The default speed of movements
public float movementSmoothing = 0.25F; //The default step smoothness of movements
private Vector3 moveTo; //Used by MoveTo, saves the target point
private bool isMoving = false; //Used by MoveTo, tells if Update should compute a MoveTo on transform
public void MoveTo(Vector3 point)
{
moveTo = point;
isMoving = true;
}
void Update()
{
//MoveTo
if (isMoving)
{
if (!moveSmoothActive) //Used if no smoothing is activated
{
//Computes the direction of the object
Vector3 direction = Vector3.Normalize(moveTo - transform.position);
//Computes the next step the object should do
Vector3 step = transform.position + direction * Time.deltaTime * movementSpeed;
//If closer than next step, go to final step, else go to next step
if (Vector3.Distance(transform.position, moveTo) > Vector3.Distance(transform.position, step))
transform.position = Vector3.Lerp(transform.position, step, movementSmoothing);
else
{
transform.position = moveTo;
isMoving = false;
}
}
//Used if smoothing is activated
else if (Vector3.Distance(transform.position, moveTo) > movementStopDistance)
transform.position = Vector3.Lerp(transform.position, moveTo, movementSmoothing);
//Else go to 'moveTo'
else
{
transform.position = moveTo;
isMoving = false;
}
}
}
So, i know this is my code, but it was strongly influenced by what the other, more experienced with Unity, coder told me while i was making it. As i wrote previously, i find the code wierd without being able to put the finger on what exactly is the weird part.