- Home /
still have some problems with c# AI
posted before and got some help but still have a couple of problems. just trying to get the AI follow my player. these are the errors im getting:
Assets/AIfollow.cs(25,51): error CS1501: No overload for method Slerp' takes
1' arguments
Assets/AIfollow.cs(26,36): error CS0117: UnityEngine.Quaternion' does not contain a definition for
rotation'
here is my script:
using UnityEngine;
using System.Collections;
public class AIfollow : MonoBehaviour {
private Transform myTransform; //current transform data of this enemy
private Transform target; //the enemy's target
private int moveSpeed = 3; //move speed
private int rotationSpeed = 3; //speed of turning
void Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
void Start()
{
target = GameObject.FindWithTag("AIattack").transform; //target the player
}
void Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation);
Quaternion.rotation((target.position - myTransform.position), (rotationSpeed*Time.deltaTime));
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
Answer by getyour411 · Aug 29, 2013 at 11:44 PM
I think this
myTransform.rotation = Quaternion.Slerp(myTransform.rotation);
Quaternion.rotation((target.position - myTransform.position), (rotationSpeed*Time.deltaTime))
should be this
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation((target.position - myTransform.position), (rotationSpeed*Time.deltaTime));
ok so i fixed it to: myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime); it lets me hit play and move around but the AI just sits there and i get these errors:
UnityException: Tag: AIattack is not defined! UnityEngine.GameObject.FindWithTag (System.String tag) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:321) AIfollow.Start () (at Assets/AIfollow.cs:19)
NullReferenceException UnityEngine.Transform.get_position () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:26) AIfollow.Update () (at Assets/AIfollow.cs:25)
Your new Error is telling you that it's not finding anything with that Tag. The null reference exception is because you never check to see if you actually found an object.
Are you sure your object actually has that tag?
yeah im positive that it has the same name as the object i put it on
You have this
target = GameObject.FindWithTag("AIattack").transform; //target the player
which seems like a roundabout way of getting the player, change "AIattack" to "Player"
Your answer
Follow this Question
Related Questions
Advice on my Ship AI... 4 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How can I more optimize My AI Script 0 Answers
Help with Enemy AI 1 Answer