Search Unity

How can I move an object between set of waypoints with smooth slowly rotation at each waypoint ?

Discussion in 'Navigation' started by Chocolade, May 4, 2020.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    934
    How can I move an object between set of waypoints with smooth slowly rotation at each waypoint facing each next waypoint ?

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Waypoints : MonoBehaviour
    7. {
    8.    public List<Vector3> points = new List<Vector3>();
    9.  
    10.    // Start is called before the first frame update
    11.    void Start()
    12.    {
    13.        points.Add(transform.position);
    14.    }
    15.  
    16.    // Update is called once per frame
    17.    void Update()
    18.    {
    19.        for (int i = 0; i < points.Count; i++)
    20.        {
    21.            transform.position = Vector3.Lerp(points[i], pos2, (Mathf.Sin(speed * Time.time) + 1.0f) / 2.0f);
    22.        }
    23.    }
    24. }
    25.  
    The first waypoint is the transform(moving object) position so when running the game the transform will move to the first waypoint and if needed first it will slowly rotate facing the first waypoint and then will move to it. When the transform will get to the next(first) waypoint it will rotate slowly facing the next waypoint while moving to the next waypoint. And so on till the end of waypoints; then over again from the start on the next rounds, the first waypoint will be the first second point in the list not transferring original position. The original position will be used as a waypoint only for the first time round.

    I tried this but the objectToPatrol is moving stuttering and not moving directly to the first waypoint but moving to the first waypoint but also up.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Waypoints : MonoBehaviour
    7. {
    8.    public Transform[] waypoints;
    9.    public float waypointRadius = 1.5f;
    10.    public float damping = 0.1f;
    11.    public bool loop = false;
    12.    public float speed = 2.0f;
    13.    public bool faceHeading = true;
    14.    public GameObject objectToPatrol;
    15.  
    16.    private Vector3 currentHeading, targetHeading;
    17.    private int targetwaypoint;
    18.    private Transform xform;
    19.    private bool useRigidbody;
    20.    private Rigidbody rigidmember;
    21.  
    22.  
    23.    // Use this for initialization
    24.    protected void Start()
    25.    {
    26.        if (objectToPatrol == null)
    27.        {
    28.            xform = transform;
    29.        }
    30.        else
    31.        {
    32.            xform = objectToPatrol.transform;
    33.        }
    34.  
    35.        currentHeading = xform.forward;
    36.        if (waypoints.Length <= 0)
    37.        {
    38.            Debug.Log("No waypoints on " + name);
    39.            enabled = false;
    40.        }
    41.        targetwaypoint = 0;
    42.        if (GetComponent<Rigidbody>() != null)
    43.        {
    44.            useRigidbody = true;
    45.            rigidmember = GetComponent<Rigidbody>();
    46.        }
    47.        else
    48.        {
    49.            useRigidbody = false;
    50.        }
    51.    }
    52.  
    53.  
    54.    // calculates a new heading
    55.    protected void FixedUpdate()
    56.    {
    57.        targetHeading = waypoints[targetwaypoint].position - xform.position;
    58.  
    59.        currentHeading = Vector3.Lerp(currentHeading, targetHeading, damping * Time.deltaTime);
    60.    }
    61.  
    62.    // moves us along current heading
    63.    protected void Update()
    64.    {
    65.        if (useRigidbody)
    66.            rigidmember.velocity = currentHeading * speed;
    67.        else
    68.            xform.position += currentHeading * Time.deltaTime * speed;
    69.        if (faceHeading)
    70.            xform.LookAt(xform.position + currentHeading);
    71.  
    72.        if (Vector3.Distance(xform.position, waypoints[targetwaypoint].position) <= waypointRadius)
    73.        {
    74.            targetwaypoint++;
    75.            if (targetwaypoint >= waypoints.Length)
    76.            {
    77.                targetwaypoint = 0;
    78.                if (!loop)
    79.                    enabled = false;
    80.            }
    81.        }
    82.    }
    83.  
    84.  
    85.    // draws red line from waypoint to waypoint
    86.    public void OnDrawGizmos()
    87.    {
    88.        Gizmos.color = Color.red;
    89.        if (waypoints == null)
    90.            return;
    91.        for (int i = 0; i < waypoints.Length; i++)
    92.        {
    93.            Vector3 pos = waypoints[i].position;
    94.            if (i > 0)
    95.            {
    96.                Vector3 prev = waypoints[i - 1].position;
    97.                Gizmos.DrawLine(prev, pos);
    98.            }
    99.        }
    100.    }
    101. }
    102.  
    The first problem is that the object I want to move between the waypoints have a Animator component and the animator make it stuttering. Is there a way to keep the Animator enabled and move it smooth I mean that it will keep playing the object animation in this case idle but also move it smooth ?