- Home /
Question by
Rakeshchatra · Mar 11, 2013 at 07:56 AM ·
instantiatelookattransforms
How to make an instantiated enemy to translate to a point first and then to lookAt the player ???
hi, i want the enemies to instantiate at a point and then move towards a defined target and after reaching that point i want it to lookAt player and also to follow it and attack. I want it to suddenly attack the player by appearing in front of it.. please guide to achieve this.
Comment
$$anonymous$$gest you learn about Invoke() for simple ti$$anonymous$$g needs
for many beginner articles try unityGE$$anonymous$$S.com
Best Answer
Answer by Auggie · Mar 12, 2013 at 05:18 AM
this might help.
void Update(){
//this will make the enemy move forward to the desired target
transform.LookAt( desiredTarget );
this.gameObject.transform.position += this.gameObject.transform.forward*MovementSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position, desiredTarget.position) <= 0)
//you are now at the desired target.
{
transform.LookAt(Player);
this.gameObject.transform.position += this.gameObject.transform.forward*AttackingSpeed*Time.deltaTime;
//you are now moving towards the player
if(Vector3.Distance(transform.position,Player.position) <= minDist)
{
//you are now at the player's feet or somewhere close depending on your minDist.
//if you want it to teleport or something, you can add something here like:
this.gameObject.transform.position =new Vector3( Player.position.x, Player.position.y, Player.position.z - 1.0f );
// just tweak this to your desired effect.
}
}
}
i hope this helps...
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Instantiated object not appearing. 1 Answer
Instantiate OnDestroy 1 Answer
C# Replace objects using if's 2 Answers
Scripting within a area in the game 1 Answer