- Home /
How to make a follow AI
I know there are a lot of questions about this but all of them are outdated or don't work. So I want a basic ragdoll AI that follows the Player until it touches the player. But none of the code works with ragdolls. Here is the code I saw and did.
var Player : Transform;
var MoveSpeed = 4;
var MaxDist = 10;
var MinDist = 5;
function Start ()
{
}
function Update ()
{
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position) >= MinDist){
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
//Here Call any function U want Like Shoot at here or something
}
}
}
But that did not work. Also, the Player is called Player and enemy called jeff. THANKS!
Answer by Callz01 · Jul 21, 2019 at 09:48 PM
You might want to use "transform.position = Vector3.MoveTowards();"
Answer by naderlabbad309 · Jul 22, 2019 at 05:00 PM
hi
try this
transform.LookAt(Player.transform);
transform.position += transform.forward * movementSpeed * Time.deltaTime;
But do I add that to the code I provided at the start and if so where? If not what do I do? ( Sorry I'm a noob at unity ) Thanks for your help!
Well delete what you have put this
transform.LookAt(Player.transform);
transform.position += transform.forward * movementSpeed * Time.deltaTime;
in function Update () not Start and keep var Player : Transform;
Answer by SahanD · Jul 23, 2019 at 06:04 AM
You got your answer but this will also help you > VIRIDAX GAME STUDIOS Basic AI Controller VIRIDAX GAME STUDIOS Basic AI Controller
It says Player does not exist in the current context even though my guy is named player
Your answer
Follow this Question
Related Questions
Problems with simple AI script 3 Answers
Enemy following the target with ITween 2 Answers
ai pathfinding project getting started scene problems 1 Answer
Player not following touch after camera is rotated? 0 Answers
Change AI Follow Players 1 Answer