0
\$\begingroup\$

Instead rotating and facing the target it's rotating to the other side but moving to the target.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoldierPatrol : MonoBehaviour
{
    System.Random rnd = new System.Random();
    public GameObject[] points;
    // assume you have 5 possible points that you can go
    public int moveIndex = 0;
    public float speed = 3;
    public float smoothSpeed = 0.5f;

    void Update()
    {
        Rotate(points[moveIndex]);
        transform.position = Vector3.MoveTowards(transform.position, points[moveIndex].transform.position, speed * Time.deltaTime);
        if (Vector3.Distance(transform.position, points[moveIndex].transform.position) < 1)
        {
            moveIndex = rnd.Next(0, 3);
        }
    }

    private void Rotate(GameObject target)
    {
        transform.rotation = Quaternion.Lerp(transform.rotation, points[moveIndex].transform.rotation, Time.deltaTime * 1);
    }
}
\$\endgroup\$
  • \$\begingroup\$ You're having it rotate to the same rotation as the waypoint, instead of rotating TOWARDS the waypoint. In c# there is a rotateTowards built-in that will work better for you. \$\endgroup\$ – user106170 Oct 31 '17 at 16:20
0
\$\begingroup\$

Solution:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoldierPatrol : MonoBehaviour
{
    System.Random rnd = new System.Random();
    public GameObject[] points;
    // assume you have 5 possible points that you can go
    public int moveIndex = 0;
    public float speed = 3;
    public float smoothSpeed = 0.5f;

    void Update()
    {
        Rotate(points[moveIndex]);
        transform.position = Vector3.MoveTowards(transform.position, points[moveIndex].transform.position, speed * Time.deltaTime);
        if (Vector3.Distance(transform.position, points[moveIndex].transform.position) < 1)
        {
            moveIndex = rnd.Next(0, 3);
        }
    }

    private void Rotate(GameObject target)
    {
        var targetPoint = target.transform.position;
        var targetRotation = Quaternion.LookRotation(targetPoint - transform.position, Vector3.up);
        transform.rotation = Quaternion.Slerp(transform.localRotation, targetRotation, Time.deltaTime * 2.0f);
    }
}
\$\endgroup\$
  • \$\begingroup\$ This answer would be better if it included a brief description of what you changed and why, so a user trying to learn from your example doesn't need to puzzle over a line-by-line diff. \$\endgroup\$ – DMGregory Oct 10 '17 at 3:27
0
\$\begingroup\$

I'll try to answer the "Why". In Scene View and using the Move Tool (in local coordinates), try selecting the game object of the target to which your object tries to rotate to. Take note of the direction of the blue arrow which represents it's "forward/facing" axis. Then do the same with your rotating object and you'll see that your rotating object's blue arrow's orientation is lerp'ing towards that of target's blue arrow.

In other words, if the target you're moving to is looking at your moving object (it's blue "forward" arrow is pointing at your moving object) then using Quaternion.Lerp will cause your moving object to look away from the target as Quaternion.Lerp will eventually make the rotations of each object equal (which in turn result in both forward vectors (blue arrow) point in the same direction).

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.