I writing a script that would make my players reach a target destination. It looks like this so far:
private void MoveTowardsGoal() {
if (Vector3.Distance (reachGoalTarget.transform.position, transform.position) > 2.0f) {
rDirection = (reachGoalTarget.transform.position - transform.position).normalized;
if (rAcceleration < rMaxSpeed) {
rAcceleration += 0.5f;
}
rVelocity = rDirection * Time.deltaTime * rAcceleration;
transform.Translate (rVelocity,Space.World);
}
}
It works fine: The objects move towards the destination point. However, I want them to move looking at the target in the forward direction (forward z axis). Currently they move with their x axis facing front.
I tried a simple rotation inside Start()
like transform.LookAt(reachGoalTarget.transform)
. With this, they look in the forward direction but then they completely move off their initial positions by rotating themselves to reach the goal.
What am I doing wrong?
LookAt
code in the update method instead of the start method? \$\endgroup\$