Search Unity

Simple Raycasting AI

Discussion in 'Scripting' started by BusterBlader, Jun 29, 2012.

  1. BusterBlader

    BusterBlader

    Joined:
    Apr 23, 2012
    Posts:
    79
    Hey guys,

    I want to create a simple Raycasting based AI. But i don't know how to do it :(

    I have a simple AI script which tells my enemy where he should go and where he should look at:

    using UnityEngine;
    using System.Collections;

    public class AIFollower : MonoBehaviour
    {
    public GameObject target;
    public float speed;
    public float rotateSpeed;
    public float minDistance;
    public float viewDistance;
    public float fromCenterToSurface;
    public float fallSpeed;
    public float range;
    public int currentDamage;
    public Transform attackDot;
    public float attackTimer;
    public float coolDown = 2.0f;
    private bool playerSpotted = false;
    private Vector3 lastPlayerPos = Vector3.zero;

    // Use this for initialization
    void Start ()
    {
    target = GameObject.FindWithTag("Player");
    }

    // Update is called once per frame
    void Update ()
    {
    if(attackTimer > 0)
    attackTimer -= Time.deltaTime;

    if(attackTimer < 0)
    attackTimer = 0;


    Walk(target.transform.position);
    RotateTo(target.transform.position);

    SurfaceDetection();
    }

    void Walk (Vector3 targetPosition)
    {
    Vector3 velocity;
    Vector3 moveDirection = transform.TransformDirection(Vector3.forward);

    Vector3 delta = targetPosition - transform.position;

    if (delta.magnitude > minDistance delta.magnitude <= viewDistance)
    {
    velocity = moveDirection.normalized * speed * Time.deltaTime;
    }
    else
    {
    velocity = Vector3.zero;

    if(attackTimer == 0)
    {
    Attack(range, currentDamage, attackDot.position);
    attackTimer = coolDown;
    }
    }

    rigidbody.velocity = velocity;
    }

    void RotateTo (Vector3 targetPosition)
    {
    Quaternion destRotation;
    Vector3 relativePos;
    //relativePos = targetPosition - transform.position;

    Vector3 tap;
    Vector3 trp;
    tap = targetPosition;
    trp = transform.position;
    tap.y = 0;
    trp.y = 0;
    relativePos = tap - trp;

    destRotation = Quaternion.LookRotation(relativePos);
    transform.rotation = Quaternion.Slerp(transform.rotation, destRotation, rotateSpeed * Time.deltaTime);
    }

    void SurfaceDetection ()
    {
    RaycastHit hit;

    if (Physics.Raycast(transform.position, -Vector3.up, out hit, fromCenterToSurface))
    {

    }
    else
    {
    rigidbody.velocity = rigidbody.velocity + (-Vector3.up * fallSpeed * Time.deltaTime);
    }

    Debug.DrawRay(transform.position, -Vector3.up * fromCenterToSurface, Color.red);
    }

    void Attack(float attackRange, int damage, Vector3 attackPoint)
    {
    Vector3 forward = transform.TransformDirection(Vector3.forward);
    RaycastHit hit;

    if (Physics.Raycast(attackPoint, forward, out hit, attackRange))
    {
    if (hit.transform.gameObject.tag == "Player")
    {
    hit.transform.gameObject.GetComponent<PlayerHealth>().AdjustHealth(currentDamage);
    }
    }
    }
    }

    Can someone tell me what i must write to give the enemy pathfinding? This will be really nice :)

    Thanks
    BusterBlader
     
  2. BusterBlader

    BusterBlader

    Joined:
    Apr 23, 2012
    Posts:
    79
    Sorry...i don't know how to give the script
    Code (csharp):
    1. -Tags :S
     
  3. UnknownProfile

    UnknownProfile

    Joined:
    Jan 17, 2009
    Posts:
    2,311
     
  4. szi_John

    szi_John

    Joined:
    Feb 10, 2012
    Posts:
    70
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class AIFollower : MonoBehaviour
    7. {
    8.     public GameObject target;
    9.     public float speed;
    10.     public float rotateSpeed;
    11.     public float minDistance;
    12.     public float viewDistance;
    13.     public float fromCenterToSurface;
    14.     public float fallSpeed;
    15.     public float range;
    16.     public int currentDamage;
    17.     public Transform attackDot;
    18.     public float attackTimer;
    19.     public float coolDown = 2.0f;
    20.     private bool playerSpotted = false;
    21.     private Vector3 lastPlayerPos = Vector3.zero;
    22.  
    23.     // Use this for initialization
    24.     void Start ()
    25.     {
    26.         target = GameObject.FindWithTag("Player");
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update ()
    31.     {
    32.         if(attackTimer > 0)
    33.             attackTimer -= Time.deltaTime;
    34.  
    35.         if(attackTimer < 0)
    36.             attackTimer = 0;
    37.  
    38.  
    39.         Walk(target.transform.position);
    40.         RotateTo(target.transform.position);
    41.  
    42.         SurfaceDetection();
    43.     }
    44.  
    45.     void Walk (Vector3 targetPosition)
    46.     {
    47.         Vector3 velocity;
    48.         Vector3 moveDirection = transform.TransformDirection(Vector3.forward);
    49.  
    50.         Vector3 delta = targetPosition - transform.position;
    51.  
    52.         if (delta.magnitude > minDistance  delta.magnitude <= viewDistance)
    53.         {
    54.             velocity = moveDirection.normalized * speed * Time.deltaTime;
    55.         }
    56.         else
    57.         {
    58.             velocity = Vector3.zero;
    59.  
    60.             if(attackTimer == 0)
    61.             {
    62.                 Attack(range, currentDamage, attackDot.position);
    63.                 attackTimer = coolDown;
    64.             }
    65.         }
    66.  
    67.         rigidbody.velocity = velocity;
    68.     }
    69.  
    70.     void RotateTo (Vector3 targetPosition)
    71.     {
    72.         Quaternion destRotation;
    73.         Vector3 relativePos;
    74.         //relativePos = targetPosition - transform.position;
    75.  
    76.         Vector3 tap;
    77.         Vector3 trp;
    78.         tap = targetPosition;
    79.         trp = transform.position;
    80.         tap.y = 0;
    81.         trp.y = 0;
    82.         relativePos = tap - trp;
    83.  
    84.         destRotation = Quaternion.LookRotation(relativePos);
    85.         transform.rotation = Quaternion.Slerp(transform.rotation, destRotation, rotateSpeed * Time.deltaTime);
    86.     }
    87.  
    88.     void SurfaceDetection ()
    89.     {
    90.         RaycastHit hit;
    91.  
    92.         if (Physics.Raycast(transform.position, -Vector3.up, out hit, fromCenterToSurface))
    93.         {
    94.  
    95.         }
    96.         else
    97.         {
    98.             rigidbody.velocity = rigidbody.velocity + (-Vector3.up * fallSpeed * Time.deltaTime);
    99.         }
    100.  
    101.         Debug.DrawRay(transform.position, -Vector3.up * fromCenterToSurface, Color.red);
    102.     }
    103.  
    104.     void Attack(float attackRange, int damage, Vector3 attackPoint)
    105.     {
    106.         Vector3 forward = transform.TransformDirection(Vector3.forward);
    107.         RaycastHit hit;
    108.  
    109.         if (Physics.Raycast(attackPoint, forward, out hit, attackRange))
    110.         {
    111.             if (hit.transform.gameObject.tag == "Player")
    112.             {
    113.                 hit.transform.gameObject.GetComponent<PlayerHealth >().AdjustHealth(currentDamage);
    114.             }
    115.         }
    116.     }
    117. }
    118.  
     
    Last edited: Jun 29, 2012
  5. BusterBlader

    BusterBlader

    Joined:
    Apr 23, 2012
    Posts:
    79
    Thanks for posting it with Tags :) But my problem is still there :( Please help :D I want the enemies to avoid obstacles ;)