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. Dismiss Notice

Car AI

Discussion in 'Scripting' started by mehdimoji, Mar 21, 2018.

  1. mehdimoji

    mehdimoji

    Joined:
    Oct 18, 2017
    Posts:
    22
    Hi guys. I'm making a game which players(cars) are in the map and should find some moving target and go toward them.
    I think on of the best way is : Find a nearest target with Raycast and move toward with Navimesh system.
    But i don't know how to have a relation between WheelCollider and NavMesh (for turn left/right).
    Does anyone know how to do it?Or any way for make it easier?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you can work out the angle between the navmeshagent's current velocity and the forward direction of the transform, then update the wheels accordingly

    https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent-velocity.html
    Code (csharp):
    1.  
    2. signedAngle = Vector3.Angle(transform.Forward, targetVector) *
    3.              Mathf.Sign(Vector3.Dot(transform.Right, targetVector))
    4.  
     
  3. mehdimoji

    mehdimoji

    Joined:
    Oct 18, 2017
    Posts:
    22
    I put this code to UI Text and return me 90, and useless to change Position/Rotation of Target and Car! Just 90.
    Code (CSharp):
    1. void Update () {
    2.         Navi.SetDestination(Target.position);
    3.         float v3 = Vector3.Angle(Target.transform.forward, Navi.velocity) * Mathf.Sign(Vector3.Dot(Target.transform.right, Navi.velocity));
    4.         txt.text = v3.ToString();
    5.     }
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    don't use "icode" tags, use "code" tags, it'll stop the code getting cut off (along with other things)
     
    mehdimoji likes this.