- Home /
Question by
Jonathon82931 · Jul 19, 2013 at 09:15 PM ·
ai problems
Enemy AI problem
the script works but incorrectly the object moves away from the player at the wrong rotation
using UnityEngine;
using System.Collections;
public class EnemieAI : 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 () {
//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;
}
}
Comment
also if it helps any my game object is from blender
//move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
(add to the current position), where you have -= (subtract from the current position)
but then I run into the problem of the enemy being backwards headed towards me :(
Best Answer
Answer by Josh1231 · Jul 20, 2013 at 08:09 AM
since you said that its facing the other direction when it is coming towards you you have to rotate the object in blender
Answer by Petumies · Jul 20, 2013 at 12:18 PM
using UnityEngine;
using System.Collections;
public class EnemieAI : 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 () {
//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;
}
}
This should work :) cheers
it dose work but my game object is backwards when it is moving towards me. :(