- Home /
Enemy Model Floating in the Air
Hello there, I have been bashing my brains out on this problem for quite some time now and still cant figure out what I am doing wrong.
What I am trying to do is this: enemy model rotates to face the player and then proceeds to walk towards the player.
The issue that I have is this, when the enemy walks up to the player it ends up floating in the air.
I am attaching a video of what I am seeing in Unity and the code I am using for enemy AI script.
Video: http://youtu.be/XVzgkocnRZk
Code: using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public float maxDistance;
public Transform myTransform;
void Awake(){
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
maxDistance = 2.5f;
}
// Update is called once per frame
void Update () {
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//Look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform.position) > maxDistance){
//Move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
}
Thank you in advance.
Answer by robertbu · Sep 30, 2013 at 02:04 AM
You need to bring the position you are using for your LookRotation() down to the level of the AI. For example, you can change update to:
void Update () {
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//Look at target
Vector3 v3 = target.position - myTransform.position;
v3.y = 0.0f;
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(v3), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform.position) > maxDistance){
//Move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
I see what you are saying, but when I add the code in Unity gives me the following error
"Assets/Scripts/EnemyAI.cs(32,12): error CS0664: Literal of type double cannot be implicitly converted to type float'. Add suffix
f' to create a literal of this type"
and when I double click it takes me to this line of code
v3.y = 0.0;
Thank you for you quick response.
Edit: I think it might have had something to do with missing an F at the end of the code you gave me so the after adding F like so:
v3.y = 0.0F;
It works beautifully. Thank you very much!
I keep switching back and forth between Javascript and C# and forgetting the 'f'...even when writing my own code.
Thanks @robertbu & @Eggplanet! Still relevant in 5.2 a couple of years later. Worked perfectly.
Replaced
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
With:
Vector3 v3 = target.position - myTransform.position;
v3.y = 0.0f;
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(v3), rotationSpeed * Time.deltaTime);
Thanks again!