- Home /
Question by
XYZglitch · Aug 04, 2014 at 12:05 PM ·
aiai problems
Follow AI goes up when it gets near me!
Hello! I am not too good at scripting, so if someone could tell me how to alter this script so the AI stays on the ground that would be awesome!
I know it has something to do with not rotating the Y axis but that is as far as I can figure out. Here's what I'm working with
private var Player : Transform; //This get the Player Transform
var Speed : float = 1; //This is the Enemy Speed
var Distance : float = 15; //The Distance before he can see the player
var Smooth : float = 2; //This is how fast the Enemy Rotate
function Awake ()
{
Player = GameObject.FindGameObjectWithTag("Player").transform;
//Set our private var Player to find a GameObject with tag "Player"
}
function Update ()
{
//Check the Distance
if (Vector3.Distance(Player.position,transform.position) <= Distance)
{
renderer.material.color = Color.red; //If he see you his color change to Red
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Player.position - transform.position), Time.deltaTime * Smooth);
//Look At the Player
transform.position = Vector3.MoveTowards(transform.position, Player.position, Speed * Time.deltaTime);
//Move Towards the Player
}
else
{
renderer.material.color = Color.green; //if he don't see you his color change to green
}
}
Comment
If it has a rigidbody you can Lock x and z rotation constraints.
Your answer
Follow this Question
Related Questions
AI code for enemy car to follow the player car 0 Answers
Thief Car AI for 3D Game 0 Answers
Tips/Advice/Guidance on Unity ML Agent Toolkit 1 Answer
Resize Array Based on Value 2 Answers
Help with AI script 0 Answers