Search Unity

AI agent scripting problem humanoid vs animal/vehical

Discussion in 'Navigation' started by Ridgerunner, Jul 18, 2017.

  1. Ridgerunner

    Ridgerunner

    Joined:
    May 31, 2014
    Posts:
    34
    I have a script that I have found and I'm trying to make it work to alien my AI Agent to the y an z axis mostly the y axis to the ground while still having my AI controller script control the x axis (left right)

    This scripted works for aliening the AI Agent to the ground, But it locks the x axis to a world and will not let the AI Agent rotate left or right on the x axis I need the axis to be free to rotate on x axis any direction, Can some one give a hand with my problem I think I need to pass in a local transform in to the Euler of the AI Agent but can't seem to figure it out ,

    Also das any one know if Unity has planes to add an humanoid vs animal/vehical script?
    As we all know a humanoid Agent walking upright so there really is no problem with humanoid AI Agents ,But with an animal/vehicals they follow the contour of the ground so it looks really bad when animal/vehicals AI Agent dos not follow the contour of the ground.


    Here is the script I'm trying to convert


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class EnemyMeshAline : MonoBehaviour {

    public GameObject target;
    float curDir = 0f; // compass indicating direction
    Vector3 curNormal = Vector3.up; // smoothed terrain normal
    public float turn;

    // Update is called once per frame
    void Update ()
    {

    curDir = (curDir + turn) % 360; // rotate angle modulo 360 according to input
    RaycastHit hit;
    if (Physics.Raycast(target.transform.position, -curNormal, out hit))
    {
    curNormal = Vector3.Lerp(curNormal, hit.normal, 4*Time.deltaTime);
    Quaternion grndTilt = Quaternion.FromToRotation(Vector3.up, curNormal);
    target.transform.rotation = grndTilt * Quaternion.Euler(0, curDir, 0);
    }
    }
     
    Last edited: Jul 18, 2017
  2. Taorcb

    Taorcb

    Joined:
    Mar 15, 2015
    Posts:
    39
    First of, use code tags, the copy/pasted code there is nigh unto unreadable.

    Use the cross product of the terrain normal and the agent's forward vectors. Then use Quaternion.LookRotation(). This is untested, but I think it should work:

    Code (CSharp):
    1.  
    2. Quaternion GetGroundRotation()
    3. {
    4.      Ray ray = new ray(transform.position, transform.down);
    5.      RaycastHit hit;
    6.  
    7.      if(Physics.Raycast(ray, out hit, 1.5f)
    8.      {
    9.           Vector3 correctedNormal = Vector3.Cross(hit.normal, transform.forward);
    10.           Quaternion correctedRotation = Quaternion.LookRotation(correctedNormal);
    11.  
    12.           return correctedRotation;
    13.      }
    14.      else
    15.           return transform.rotation;
    16. }
     
    Ridgerunner likes this.
  3. Ridgerunner

    Ridgerunner

    Joined:
    May 31, 2014
    Posts:
    34
    Thanks for the reply what should I put in the cod tags it will not let me put more then one tag in?


    Also do I run this in a update method or will this run on its own in a script
     
    Last edited: Jul 21, 2017