Search Unity

Trying To Make Mixamo Character Walk In Specified Directions

Discussion in 'Animation' started by swikarrr, Oct 4, 2021.

  1. swikarrr

    swikarrr

    Joined:
    Sep 7, 2021
    Posts:
    1
    Hey guy's i am trying to make a mixamo character walk in the specific directions , it does so but it looks at different location very fast before moving to the next direction.



    I have included what happens to my character in the video.

    Here is the code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PathfindingTester : MonoBehaviour
    5. {
    6.     // The A* manager.
    7.     private AStarManager AStarManager = new AStarManager();
    8.     // Array of possible waypoints.
    9.     List<GameObject> Waypoints = new List<GameObject>();
    10.     // Array of waypoint map connections. Represents a path.
    11.     List<Connection> ConnectionArray = new List<Connection>();
    12.     // The start and end target point.
    13.     public GameObject start;
    14.     public GameObject end;
    15.     public int Count;
    16.     // Debug line offset.
    17.     Vector3 OffSet = new Vector3(0, 0.3f, 0);
    18.     public float speed;
    19.     private Rigidbody rb;
    20.     private Transform target;
    21.     int current;
    22.     Vector3 target2;
    23.     //float WPradius = 0.5f;
    24.     Connection aConnection;
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.  
    30.         if (start == null || end == null)
    31.         {
    32.             Debug.Log("No start or end waypoints.");
    33.             return;
    34.         }
    35.         // Find all the waypoints in the level.
    36.         GameObject[] GameObjectsWithWaypointTag;
    37.         GameObjectsWithWaypointTag = GameObject.FindGameObjectsWithTag("Waypoint");
    38.         foreach (GameObject waypoint in GameObjectsWithWaypointTag)
    39.         {
    40.             WaypointCON tmpWaypointCon = waypoint.GetComponent<WaypointCON>();
    41.             if (tmpWaypointCon)
    42.             {
    43.                 Waypoints.Add(waypoint);
    44.             }
    45.         }
    46.         // Go through the waypoints and create connections.
    47.         foreach (GameObject waypoint in Waypoints)
    48.         {
    49.             WaypointCON tmpWaypointCon = waypoint.GetComponent<WaypointCON>();
    50.             // Loop through a waypoints connections.
    51.             foreach (GameObject WaypointConNode in tmpWaypointCon.Connections)
    52.             {
    53.                 Connection aConnection = new Connection();
    54.                 aConnection.SetFromNode(waypoint);
    55.                 aConnection.SetToNode(WaypointConNode);
    56.                 AStarManager.AddConnection(aConnection);
    57.             }
    58.         }
    59.         // Run A Star...
    60.         ConnectionArray = AStarManager.PathfindAStar(start, end);
    61.         //  Debug.Log(ConnectionArray.Count);
    62.  
    63.         // rb = GetComponent<Rigidbody>();
    64.         //rb.MovePosition((ConnectionArray[0].GetFromNode().transform.position + OffSet));
    65.         //transform.position = ConnectionArray[0].GetFromNode().transform.position;
    66.  
    67.     }
    68.     // Draws debug objects in the editor and during editor play (if option set).
    69.     void OnDrawGizmos()
    70.     {
    71.         // Draw path.
    72.         foreach (Connection aConnection in ConnectionArray)
    73.         {
    74.  
    75.             Gizmos.color = Color.red;
    76.             Gizmos.DrawLine((aConnection.GetFromNode().transform.position + OffSet),
    77.             (aConnection.GetToNode().transform.position + OffSet));
    78.  
    79.         }
    80.     }
    81.  
    82.     // Update is called once per frame
    83.     void Update()
    84.     {
    85.        
    86.         if (transform.position != ConnectionArray[current].GetToNode().transform.position)
    87.         {
    88.             Vector3 pos2 = Vector3.MoveTowards(transform.position, ConnectionArray[current].GetToNode().transform.position, speed * Time.deltaTime);
    89.             //GetComponent<Rigidbody>().MovePosition(pos2);
    90.             //Debug.Log(transform.position);
    91.             SetNewTarget(pos2);
    92.             Vector3 direction = target2 - transform.position;
    93.             transform.Translate(direction.normalized * speed * Time.deltaTime, Space.World);
    94.         }
    95.         else
    96.         {
    97.             current = (current + 1) % ((ConnectionArray.Count));
    98.  
    99.             // if (current + 2 == (ConnectionArray.Count - 1) && (transform.position != ConnectionArray[current].GetToNode().transform.position))
    100.             if (current + (ConnectionArray.Count - 1) == (ConnectionArray.Count - 1) && (transform.position != ConnectionArray[current].GetToNode().transform.position))
    101.             {
    102.                 // Debug.Log(current);
    103.                 //  Debug.Log("From Else");
    104.  
    105.                 Count = Count + 1;
    106.  
    107.                 ConnectionArray.Reverse();
    108.                 //speed = speed - 1f;
    109.                 Debug.Log(speed);
    110.  
    111.                 Vector3 pos3 = Vector3.MoveTowards(transform.position, ConnectionArray[current].GetFromNode().transform.position, speed * Time.deltaTime);
    112.                 SetNewTarget(pos3);
    113.                 Vector3 direction = target2 - transform.position;
    114.                 transform.Translate(direction.normalized * speed * Time.deltaTime, Space.World);
    115.                 //GetComponent<Rigidbody>().MovePosition(pos3);
    116.                 if (Count == 2)
    117.                 {
    118.                     speed = 0f;
    119.                 }
    120.  
    121.             }
    122.             else
    123.             {
    124.                 {
    125.  
    126.                     current = (current) % ((ConnectionArray.Count));
    127.                 }
    128.  
    129.             }
    130.  
    131.  
    132.         }
    133.  
    134.  
    135.     }
    136.  
    137.     void OnTriggerEnter(Collider other)
    138.     {
    139.  
    140.         if (other.gameObject.CompareTag("target"))
    141.         {
    142.  
    143.             other.gameObject.SetActive(false);
    144.  
    145.         }
    146.     }
    147.  
    148.     void SetNewTarget(Vector3 newTarget)
    149.     {
    150.         target2 = newTarget;
    151.         transform.LookAt(target2);
    152.     }
    153.  
    154.  
    155.  
    156.  
    157. }