hey i found a post from awhile back about an AI script to follow the player. i tried to get it so it would work for C# but i just cant seem to figure it out. i know i made this sloppy but if you can figure it out please let me know. thanks.
here is the code:
using UnityEngine;
using System.Collections;
public class AIfollow : MonoBehaviour {
public Transform : myTransform; //current transform data of this enemy
private int target : Transform; //the enemy's target
private int moveSpeed = 3; //move speed
private int rotationSpeed = 3; //speed of turning
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
If this should be in c# you want to relplace ‘function’ with ‘void’. Also for your variables you want to have [type] [name] so for example public int Number = 1;
private int Target : Transform; needs to be private Transfrom Target;
The script should work fine.
This is a very stripped down version of player tracking. The AI will come at the player but move through objects and not obey physics. Hope this helps.
using UnityEngine;
using System.Collections;
public class AIFollow : MonoBehaviour {
public float Distance;
public Transform Target;
public float lookAtDistance= 25.0f;
public float chaseRange= 15.0f;
public float moveSpeed= 5.0f;
/*
This ai will fly and move through objects inlcuding terrain!
*/
void Update (){
// Gauge the distance to the player. Line in 3d space. Draws a line from source to Target.
Distance = Vector3.Distance(Target.position, transform.position);
// AI begins tracking player.
if (Distance < lookAtDistance)
{
lookAt();
}
// Attack! Chase the player until/if player leaves attack range.
if (Distance < chaseRange)
{Debug.Log("enemy chase");
chase ();
}
}
// Turn to face the player.
void lookAt (){
// Rotate to look at player.
Quaternion rotation= Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime);
//transform.LookAt(Target); alternate way to track player replaces both lines above.
}
void chase (){
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
}