Skip to main content
Fixed indentation and grammar. Matched title to contents. Some tags.
Source Link
Anko
  • 13.5k
  • 10
  • 56
  • 82

Moving object towards a Why are my target Unity-following objects rotated the wrong way?

I am trying to implementwriting a script where basicallythat would make my players would reach thea target destination. Below is my scriptIt 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: The objects move towards the destination point. ButHowever, I want them to move looking at the target in the forward direction ( forwardforward z axis  ). Currently they move with their x axis facing front.

I tried a simple rotation inside Start()Start() like transform.LookAt(reachGoalTarget.transform)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.

Please provide suggestions as to what I can change or if IWhat am I doing anything wrong here. Thanks!?

Moving object towards a target Unity

I am trying to implement a script where basically my players would reach the target destination. Below is my script

 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. But 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.

Please provide suggestions as to what I can change or if I am doing anything wrong here. Thanks!

Why are my target-following objects rotated the wrong way?

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?

Source Link
ckzilla
  • 415
  • 1
  • 12
  • 22

Moving object towards a target Unity

I am trying to implement a script where basically my players would reach the target destination. Below is my script

 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. But 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.

Please provide suggestions as to what I can change or if I am doing anything wrong here. Thanks!