Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Why the character is not rotating and turning to the next waypoint and how to make it walk ?

Discussion in 'Scripting' started by DubiDuboni, Jun 10, 2020.

  1. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    Some things I wanted to do and the WAypoints script was working before but not now.

    * When the game is starting the Waypoints script is waiting number of second before making the character Move()

    * When the character is start moving he should rotate smooth facing the first waypoint. Now it seems that the character just rotating at one facing the first waypoint and not smooth and maybe some slowly.

    * When the character is start moving he should change to Walk animation smooth.

    * When the character is getting to the first waypoint he should rotate smooth facing the next waypoint.

    * When the character is getting close or on the last waytpoint he should change the animation to Idle smooth.

    Now how the script is the character is not doing any of the thing I wanted.
    He is not rotating smooth facing the next waypoint. He is not changing to walk and then to idle animations smooth.

    The character is not turning when reaching the first waypoint to the next waypoint but keep moving forward nonstop.

    The walk animation stop after short time I tried to change the animation it self loop to true but this is not a solution.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Reflection;
    5. using UnityEngine;
    6. using UnityEngine.AI;
    7.  
    8. public class Waypoints : UnityEngine.MonoBehaviour
    9. {
    10.     public List<GameObject> waypoints = new List<GameObject>();
    11.     public Animator _animator;
    12.     public int waitTimeBeforeGo;
    13.     public int num = 0;
    14.     public float minDist;
    15.     public float speed;
    16.     public bool rand = false;
    17.     public bool go = true;
    18.     public bool loop = false;
    19.     public bool waitTime = false;
    20.  
    21.     private bool lastPoint = false;
    22.  
    23.     // Use this for initialization
    24.     void Start()
    25.     {
    26.         /*var ways = GameObject.FindGameObjectsWithTag("Agent Waypoint");
    27.         foreach (GameObject go in ways)
    28.         {
    29.             waypoints.Add(go);
    30.         }*/
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         float dist = Vector3.Distance(gameObject.transform.position, waypoints[num].transform.position);
    37.  
    38.         if (waitTime == true)
    39.         {
    40.             StartCoroutine(WaitBeforeGo());
    41.         }
    42.         else
    43.         {
    44.             if (go)
    45.             {
    46.                 _animator.SetBool("Walk", true);
    47.  
    48.                 if (dist > minDist && lastPoint == false)
    49.                 {
    50.                     Move();
    51.                 }
    52.                 else
    53.                 {
    54.                     if (!rand)
    55.                     {
    56.                         if (num + 1 == waypoints.Count)
    57.                         {
    58.                             if (loop == true)
    59.                             {
    60.                                 num = 0;
    61.                             }
    62.  
    63.                             lastPoint = true;
    64.                             _animator.SetBool("Idle", true);
    65.                         }
    66.                         else
    67.                         {
    68.                             num++;
    69.                         }
    70.                     }
    71.                     else
    72.                     {
    73.                         num = Random.Range(0, waypoints.Count);
    74.                     }
    75.                 }
    76.             }
    77.         }
    78.     }
    79.  
    80.     public void Move()
    81.     {
    82.         gameObject.transform.LookAt(waypoints[num].transform.position);
    83.         gameObject.transform.position += gameObject.transform.forward * speed * Time.deltaTime;
    84.     }
    85.  
    86.     IEnumerator WaitBeforeGo()
    87.     {
    88.         yield return new WaitForSeconds(waitTimeBeforeGo);
    89.  
    90.         waitTime = false;
    91.     }
    92. }
    93.  
     
  2. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    I found now that if I remove the two lines that start the Walk and Idle animations in the script then the character will get the first waypoint and turn facing the next waypoint.

    but I don't want him to turn using LookAt but make him smooth rotating to make it realistic.
    and not sure how to make the animation transitions to change smooth to Walk and then to Idle and why when the animations set to true it's missing ignoring the first waypoint.