- Home /
Question by
chibisnowflake · Dec 10, 2013 at 04:02 AM ·
follow playerrotating
Stop rotating in the Follow AI script
I have a basic follow AI on some items my character gains by killing the enemy (it spawns and then it just glides to them) but the problem is that the item itself just orbits around the player. I'm new to Unity3D so I'm unsure how to remedy this. Can anyone help me figure out how to make the object just fly directly to the player and not rotate around it?
var target : Transform;
var moveSpeed = 20;
var rotationSpeed = 5;
var myTransform : Transform;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
Comment
Best Answer
Answer by robertbu · Dec 10, 2013 at 04:09 AM
Replace line 26 with:
myTransform.position = Vector3.MoveTowards(myTransform.position, target.position, moveSpeed * Time.deltaTime);
Alternate solution:
myTransform.position += (target.position - myTransform.position).normalized * moveSpeed * Time.deltaTime;
Your answer
Follow this Question
Related Questions
Friction issue with rotating sphere 1 Answer
Changing Z-Axis of camera 1 Answer
Keyboard rotating - left is stopping object 1 Answer
Rotate transform.forward at local x axis 0 Answers
Add posititons relative to transform 2 Answers