Search Unity

AI Car chases player?

Discussion in 'Scripting' started by yoash, Aug 30, 2015.

  1. yoash

    yoash

    Joined:
    Sep 3, 2013
    Posts:
    36
    I've struggled trying to make a simple car model chase a player and ram into him (Rigidbodies)

    Any pointers on this?

    I just want a simple car that does not fly off the ground when chasing the player, but simply drives towards the player at ground level and rams him without spazing out.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class aiDRIVE : MonoBehaviour {
    5.     public float movementSpeed;
    6.     public Transform player;
    7.     public Vector3 thePosition;
    8.     public float yvalue;
    9.     public float lockPos = 0;
    10.     public Transform target;
    11.  
    12.     void Start () {
    13.  
    14.         yvalue = 0.00f;
    15.         movementSpeed = 15.0f;
    16.         thePosition = player.position;
    17.  
    18.     }
    19.  
    20.  
    21.     void Update () {
    22.  
    23.         if (transform.position.y > 0) {
    24.             transform.position = new Vector3 (transform.position.x, 0, transform.position.z);
    25.  
    26.         }
    27.  
    28.         transform.rotation = Quaternion.Euler(lockPos, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
    29.         Vector3 targetPostition = new Vector3(player.position.x, this.transform.position.y, player.position.z);
    30.         transform.position += transform.forward * Time.deltaTime * movementSpeed;
    31.         transform.LookAt (targetPostition);
    32.  
    33.     }
    34.  
    35. }
    36.  
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,463
    Look into steering behaviours. Specifically Seek.
     
  3. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    693
    If you are familiar with missile target follow system you can use that Technic for vehicles.