- Home /
make enemy stop at distance and play animation
I want to keep the enemy above ground script.I like how it works.I want to add the STOP script to it.Combining the 2 scripts isn't an easy task for me.I get error after error.
enemy above ground script first
var LookAtTarget:Transform;
var speed : float = 3;
var controller:CharacterController;
function Start()
{
animation.Play("idle 1000");
controller = gameObject.GetComponent(CharacterController);
}
function Update()
{ /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
transform.LookAt(Vector3(LookAtTarget.position.x, transform.position.y, LookAtTarget.position.z));
controller.SimpleMove(speed*transform.forward);
animation.Play("walk 1000"); //You play the animation
}
here is the Stop script
var target : Transform;
var moveSpeed = 0;
var rotationSpeed = 3;
var player : Transform;
var distance:float = 5.8;
var distanceAttack:float = 2.6;
var myTransform : Transform;
function Awake() {
myTransform = transform;
}
function Start() {
animation.Play("idle 1000");
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
if(Vector3.Distance(transform.position, player.position) < distance){
if(Vector3.Distance(transform.position, player.position) >distanceAttack){
moveSpeed = 5;
animation.Play("walk 1000");
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
I added this snippet
transform.LookAt(Vector3(LookAtTarget.position.x, transform.position.y, LookAtTarget.position.z));
into the above ground script.this helped with the player following the terrain on the y axis.
i cant seem to get enemy to follow me on y axis and stop at distance to play the animation that works in the STOP script.
I could not get the rest of the code to format correctly sorry. my first time trying to use the format thingy.I left it where it could be edit if someone can fix this. Thanks dimwood
Just a question. What seems to be the problem? If the "STOP" script you have works why not use that rather than using both?
the enemy will not follow the terrain properly.he goes through the ground. any ideas?
Answer by deltamish · Jan 31, 2013 at 04:12 PM
HI I have answered a similar question Here
thanks for the code deltamish.It is good code,but same problem is that the enemy goes through the ground.this is why i want to combine the 2 codes,or other suggestions would be appreciated.like setting up the collider for enemy not to go through the ground. thanks!!!I am not sure what i am missing.
HI,Have you checked if terrian colider is added or not or if you are using a plane or a box then check if the colider has bbem added ot it or not.If you still have the problem then try removing the terrain and do it from begining.Have you added collider to the enemy character himself.Above all these if you face same problem
then
Create a empty gameobject and add a box colider to it and place it directly below the enemy character and make it a child of the enemy character
the foot collider works with the enemy above ground script, but the STOP script he goes right through hills in ground.
You might want to take out LookAtTarget.position.z cause i think (I have not opened unity yet)it controls forward rotation .This my completely turn your object which might be causing your objectto fall
I ended up using the stop script only.I got rid of the character controller, and just used a capsule for the enemy.This worked.I think the two scripts were fighting each other.I gave enemy a rigidbody with contraints on the X Y Z rotation.I hope this helps others. TY Dimwood
Your answer
Follow this Question
Related Questions
Speed based on Distance 3 Answers
Enemy Attacks not lining up properly, why? 1 Answer
A* Pathfinding AIFollow in JavaScript? 0 Answers
How to reach multiple GameObjects' value , enemy AI 1 Answer
AI Field of vision 1 Answer