- Home /
Rotating Wrong
when the AI moves towards me it rotates forward for some reason, same as when it sees my player if I move it'll tilt the model forward a little and then when he chases me it doesn't work very well it just stays where it rotated to and ends up flipping everywhere, Ive added a ridgidbody.. but it still hasn't worked. Is they a way I can make it not rotate? I've froze the rotation using the ridgidbody.
My code is below.
var Distance;
var Target : Transform;
var lookAtDistance = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Daping = 6.0;
var myTrans : Transform;
private var anim : Animator;
function Start () {
anim = GetComponent(Animator);
}
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
anim.SetBool("Walking", false);
Debug.Log("Looking at Player!");
lookat ();
}
if (Distance > lookAtDistance)
{
Debug.Log("Looking For Player!");
}
if (Distance < attackRange)
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
//anim.SetBool("Walking", true);
Debug.Log("Attacking");
}
}
function lookat () {
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Daping);
}
function attackPlayer () {
//transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
Answer by theLucre · Nov 30, 2014 at 02:03 AM
You're pointing the object at the target transform in the lookat() method. Remove that code and it shouldn't never rotate at all.
This line?
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Daping);
When I remove the code above it walks off in a different direction, I put + ins$$anonymous$$d of $$anonymous$$us and it runs away from my player doing the weird rotating thing, I've attached a image of what I'm on about as I don't feel I've explained it in good enough depth for anyone to understand my problem. I got the AI working before but lost code as had to reformat haven't a clue why it's being an idiot. Starting to dislike Unity to be fair :/
Your answer
Follow this Question
Related Questions
How to reach multiple GameObjects' value , enemy AI 1 Answer
Make player not be seen by AI, when player in foilage and shadows. 1 Answer
Enemy AI C# 0 Answers
Enemy Avoid Terrain 0 Answers