- Home /
AI to follow player not working
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;
} }
i dont know why its getting messed up when posting this but heres the coding
Are you getting an error on startup from this or is it not executing as you thought or what? One pitfall I see here is the use of transform.position to move something which usually yields very inconsistent results in a 3D hilly/uneven terrain.
Answer by SudoSandwich · Aug 30, 2013 at 05:33 AM
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.
If your trying to convert js to c# use Convert unity javascript (unityscript) to C# to get a base to start from then you just need to change a few things to get it working.
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);
}
}
hey thanks for this but i put it all in right and it still just sits there and doesnt move
Did you link the Target variable to the player? It works, I tested it before posting. Put the script on the enemy. Drag your player from the hierarchy pane onto the Target variable in the inspector and it should work when you get close and are in front of the AI. Also be sure your AI is pointing the right way. I believe the blue arrow is forward. I dont have my laptop with me atm. I can help more later if you still need help.
NullReferenceException UnityEngine.Transform.get_position () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:26) AIfollow.Update () (at Assets/AIfollow.cs:25)
UnassignedReferenceException: The variable Target of 'AIfollow2' has not been assigned. You probably need to assign the Target variable of the AIfollow2 script in the inspector. UnityEngine.Transform.get_position () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:26) AIfollow2.Update () (at Assets/AIfollow2.cs:18)
where do i assign the target in this script?
Target = your player object
With the enemy selected with the AI script attached drag the player object onto the AIFollow script where it says
Target None(Transform)
Alternatively you can click the little circle next to it and select the target (your player or and other object to follow) in the pop up list.
Answer by WizzDE · Aug 27, 2013 at 08:28 PM
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.
thanks. everything is fine except still the line private int Transform Target; is still giving me errors.i tried everything with this line and no matter what im getting an error for it. do you know why?
Answer by fafase · Aug 28, 2013 at 07:47 PM
You are declaring variable in a UnityScript way for a C# script:
public Transform : myTransform;
private int target : Transform;
should be
public Transform myTransform;
private Transform target;
ok thanks i got that fixed but now im getting a problem with this line:
Quaternion.LookRotation(target.position - myTransform.position), (rotationSpeed*Time.deltaTime);
sorry for all these questions still new at this
just unexpected symbols so its probably the littlest thing wrong but im not sure what if i have to get rid of the () or something. its on this line Quaternion.LookRotation(target.position - myTransform.position), (rotationSpeed*Time.deltaTime);
You have change your code I guess since you show:
Quaternion.LookRotation(target.position - myTransform.position), (rotationSpeed*Time.deltaTime);
But had it different in the first place.
You are either missing a parenthesis or you have one too many
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime)**)**;
or
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime;
im still getting an error on that last line and i copied exactly what you have
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Basic AI Follow Player 2D C# 1 Answer
Complex Enemy follow AI 1 Answer
Distribute terrain in zones 3 Answers
How can I get a character to patrol and follow terrain? 1 Answer