Search Unity

Basic AI Rotation

Discussion in 'Scripting' started by DevoMage, Apr 20, 2014.

  1. DevoMage

    DevoMage

    Joined:
    Dec 19, 2012
    Posts:
    16
    i am using the following code attached to a capsule to represent an enemy. the capsule has a child cylinder rotation = (90, 0, 0) and position = (0, 0, 1.75) to act as a pointer. when the capsule hits anything with a collider it will bounce.

    the code works "almost" the way it should, but is off by a slight amount... anyone have any ideas to make this working properly?

    demo project:
    http://www.magestorm.com/temp/BasicAI.unitypackage

    thanks in advance for any help with this!


    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class AIMovement : MonoBehaviour
    5. {
    6.     public float minSpeed = 3;
    7.     public float maxSpeed = 7;
    8.  
    9.     private Vector3 velocity;
    10.  
    11.     ContactPoint hit;
    12.  
    13.     void Start()
    14.     {
    15.         float mySpeed1 = Random.Range(minSpeed, maxSpeed);
    16.         float mySpeed2 = Random.Range(minSpeed, maxSpeed);
    17.  
    18.         velocity = new Vector3(mySpeed1, 0, mySpeed2);
    19.     }
    20.  
    21.     void FixedUpdate()
    22.     {
    23.         // lock up/down movement
    24.         velocity.y = 0;
    25.  
    26.         transform.Translate(velocity * Time.deltaTime, Space.World);
    27.  
    28.         //rotate so pointer is pointing at forward movement direction
    29.         transform.rotation = Quaternion.LookRotation(hit.normal);
    30.     }
    31.  
    32.     void OnCollisionEnter(Collision coll)
    33.     {
    34.         hit = coll.contacts[0];
    35.  
    36.         velocity = Vector3.Reflect(velocity, hit.normal); // reflect the velocity
    37.     }
    38.  
    39. }
    40.  
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    It is not a good idea to use a Physics based call back in a translation based system. You are attempting to use the physics system to give you a response, when you are not letting the physics system manage the movement.

    In your case, I would simply let the physics do the work. give it a rigidbody, and just set it's velocity rather than translate the object. You will get much better results that way.
     
  3. DevoMage

    DevoMage

    Joined:
    Dec 19, 2012
    Posts:
    16
    very helpful link:
    http://answers.unity3d.com/questions/196896/help-with-bounce-agnle-and-velocity-change.html?sort=oldest

    final code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class AIMovement : MonoBehaviour
    5. {
    6.     public float startVel = 5;
    7.     private Vector3 velocity;
    8.  
    9.     void Start()
    10.     {
    11.         velocity = new Vector3(0, 0, startVel);//move only in the forward direction
    12.     }
    13.  
    14.     void FixedUpdate()
    15.     {
    16.         velocity.y = 0; // make sure velocity is strictly horizontal
    17.         transform.Translate(velocity * Time.deltaTime, Space.World);
    18.     }
    19.  
    20.     void OnCollisionEnter(Collision coll)
    21.     {
    22.         if (coll.collider.tag == "Wall" || coll.collider.tag == "Player")
    23.         {
    24.             ContactPoint hit = coll.contacts[0];
    25.  
    26.             Vector3 normal = hit.normal;
    27.             normal.y = 0;
    28.  
    29.             Debug.DrawRay(hit.point, normal, Color.green, 0.3f); // draw green normal
    30.             Debug.DrawRay(hit.point, -velocity, Color.red, 0.3f); // draw red "in" velocity
    31.            
    32.             velocity = Vector3.Reflect(velocity, normal); // reflect the velocity
    33.  
    34.             Ray ray = new Ray(hit.point, velocity);
    35.             transform.LookAt(transform.position + ray.direction);
    36.  
    37.             Debug.DrawRay(hit.point, velocity, Color.blue, 0.3f); // draw blue "out" velocity
    38.         }
    39.     }
    40.  
    41. }
    42.