- Home /
My EnemyAI is flying towards me
can someone look at my code and tell me what i did wrong? because i really don't understand what i did wrong. My enemy should be walking on the ground but instead it's hovering in the air don't really know why.
here is my code:
i hope someone can help me!
using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour { public Transform target; public int moveSpeed; public int rotationSpeed;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag ("Player");
target = go.transform;
}
// 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);
// move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
Answer by DaOnlyOwner · Dec 13, 2015 at 11:54 PM
If you want to move your enemy, don't use your own move methods. Doing so restricts you in terms of physics interaction. Attach a rigidbody component and try to move this around by calling Unity defined methods. For example, See the shooter tutorial for that.
Your answer
![](https://koobas.hobune.stream/wayback/20220612052243im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Play sound multiple times upon collision 1 Answer
cancle invokeReapeating not working 0 Answers
Unity UFO tutorial question about code 0 Answers