- Home /
How can I stop a character from rotating around when it reaches a aspecific point?
I am trying to use some AI but there`s a problem which is the character keep looking backward and forward fast when it reaches to the enemy point
Here is what I`ve done using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour {
Transform target;
int rotationSpeed;
int moveSpeed;
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
rotationSpeed = 28;
moveSpeed = 2;
}
// Update is called once per frame
void Update () {
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation (target.position - transform.position),
rotationSpeed );
transform.position += transform.forward *moveSpeed * Time.deltaTime;
}
}
Answer by $$anonymous$$ · Jan 07, 2013 at 12:18 PM
It's not clear from the question whether your problem is with your character or with your enemy, but after trying, probably both. It's because the enemy moves "inside" your player, they are colliding and that leads to weird stuff.
a) One solution is that you set up a follow distance so the enemy doesn't get too close to the player, something like this:
public float followDistance = 5f;
bool canMove = false;
...
void Update() {
// rotation can stay where it is
if (canMove)
transform.position += transform.forward * moveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, player.position) < followDistance)
canMove = false;
else
canMove = true;
}
b) Another solution which may be more sensible if your enemies do melee attack is to use OnCollisionEnter() and OnCollisionExit() and set in a boolean whether the enemy can or can't make additional movement towards the player.
c) Use Trigger colliders on your enemies. You can combine this with solution b) and use OnTriggerEnter() and OnTriggerExit() instead.
Thanks so much buddy , this really helped me and it works so well I would to tell you more than thank you if there is something better to say :D
Answer by Great Alexander · Jan 08, 2013 at 11:12 AM
Thanks buddy, but for the first solution there is a problem which is the followDistance variable can notbe substracted from transform.position because it is a vector and the other is just an integer
Hi There.
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]
Under the answer where it says edit | delete | more , click on more , then convert to comment
Answer by Great Alexander · Jan 08, 2013 at 11:15 AM
There is another problem when I make followDistance a Vector3
When the scene start the enemy keeps goin away from the player
Hi There.
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]
Under the answer where it says edit | delete | more , click on more , then convert to comment
(also you don't have to wait for a moderator to approve a comment).
Please be patient if your question/reply doesn't show straight away.
As a new user, your posts and questions are held in a moderator que until it is approved and then it is displayed. When your karma rises, you'll be able to post questions, comments and answers without waiting for someone to approve it =]
Also : you should edit your question if the situation changes or new information arises, so new readers can find it quickly without reading all the comments.
Wow, you got alot of my generic posts there !
Yeah well I wrote "something like this" in my answer because I just wanted to show you a sketch of a possible solution. Of course you need to check if the enemy's too close and then do something. $$anonymous$$ake followDistance a float, and check if Vector3.Distance(transform.position, player.position) < followDistance and if so, modify the position or disable the movement.
EDIT: I rewrote the code in my original answer, this should work for you, solution a).
Sorry if it was unclear, but usually if I answer, I try to give a thought process rather than exact code, if it's not requested.
Your answer
Follow this Question
Related Questions
Basic AI Locked Axis 1 Answer
my ai wiggles slightly 1 Answer
Random movment, like mobs in wow 1 Answer
Enemy AI don't collide with objects or rotate. 0 Answers
object pauses at the each way points for a vary short time 1 Answer