Search Unity

Not rotating in direction the modal is facing

Discussion in 'Navigation' started by olliewe88, Apr 19, 2020.

  1. olliewe88

    olliewe88

    Joined:
    Dec 4, 2015
    Posts:
    11
    Hi,

    My character is moving sideways. He always moves sideways. I would like him to move like a normal person in a straight line. I have tried making a empty gameobject and having him as a child but he doesn't like that. I have also tried changing the pivot points in 3DS max but have had no success. Please help

    Code (CSharp):
    1. public class testwalk : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.     //public GameObject target = null;
    5.     private NavMeshAgent nma = null;
    6.     public float wanderRadius;
    7.     public float wanderTimer;
    8.  
    9.     private Transform target;
    10.     private NavMeshAgent agent;
    11.     private float timer;
    12.  
    13.     public static Vector3 newPos;
    14.  
    15.  
    16.     void Start()
    17.     {
    18.         agent = GetComponent<NavMeshAgent>();
    19.         timer = wanderTimer;
    20.         NavMeshHit myNavHit;
    21.         if (NavMesh.SamplePosition(transform.position, out myNavHit, 100, -1))
    22.         {
    23.             transform.position = myNavHit.position;          
    24.  
    25.         }
    26.         nma = this.GetComponent<NavMeshAgent>();
    27.  
    28.     }
    29.  
    30.     void Update()
    31.     {
    32.         //nma.SetDestination(target.transform.position);
    33.         timer += Time.deltaTime;
    34.  
    35.         if (timer >= wanderTimer)
    36.         {
    37.             newPos = RandomNavSphere(transform.position, wanderRadius, -1);
    38.             agent.SetDestination(newPos);
    39.             timer = 0;
    40.  
    41.             nma.updateRotation = true;
    42.             transform.LookAt(newPos + transform.position);
    43.             transform.Rotate(0, -190, 0);
    44.  
    45.             transform.LookAt(newPos + transform.position);
    46.  
    47.  
    48.         }
    49.      
    50.  
    51.  
    52.  
    53.     }
    54.  
    55.     public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask)
    56.     {
    57.        
    58.         Vector3 randDirection = Random.insideUnitSphere * dist;
    59.  
    60.         randDirection += origin;
    61.  
    62.         NavMeshHit navHit;
    63.  
    64.         NavMesh.SamplePosition(randDirection, out navHit, dist, layermask);
    65.  
    66.         return navHit.position;
    67.     }
    68. }