- Home /
Getting enemy to move towards player untill they collide problem...
So here is the problem I want the enemy to move towards the player until there is a collision here is the important part of my enemy script:
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;
}
Now everything is setup to detect the collision and react properly but with the code above the enemy and player NEVER collide the enemy gets close to the player and starts to spin around the player without ever colliding...
Is the code above causing the problem?
Any solutions?
If the player moves too far on each step the colliders might jump past eachother without ever having the chance to detect collision.
This script can help. http://wiki.unity3d.com/index.php?title=DontGoThroughThings
Answer by Alex808 · Mar 08, 2013 at 02:14 AM
The player can completely stop moving the enemy will move forward towards the player and within a certain distance the enemy will start to orbit around the player instead of colliding with the player ..
Answer by BenKurdziel · Mar 08, 2013 at 02:13 AM
You don't have a catch for collision in that code. So it will do that no matter what, even if you collide. Put a Box Collider around the enemy and set it to IsTrigger. Check OnTriggerEnter (other : Collider) to see if that other object is the player. If it is, set your speed to 0 (assuming you want them to stop moving).
Another option is to test their distance from each other and if their distance is less than a number you choose, you stop the NPC.
Everything is already in place to detect the collision and that part works..But with the code above the enemy never comes close enough collides with the player to collide with it.. It goes forward towards the player and at a certain distance before colliding with the player the enemy starts orbiting around the player. I did the test if I remove the code above that makes the enemy follow and move towards the player and i run the player into the enemy the collision IS detected.
Your answer
Follow this Question
Related Questions
Enemy Nudging Towards Player 1 Answer
Can anyone tell me whats wrong with my MoveTowardsEnemy part of my script? 1 Answer
Player Health 3 Answers
How can I make player move with cursor?,How can I make player move with mouse? 1 Answer
Teleporting Script Doesn't Work With Anything But The Player 1 Answer