Hello,
I’ve been trying this script I’ve only slightly modified for my use. I just want my object to follow my player, and stop when in range. Now it all works fine when I don’t have a Rigidbody on my following object. It stops fine but the object slowly floats and transforms to what seems to be the middle of my player character.
To counteract this I added a rigidbody, it jitters along the floor (As I imagine because it’s trying to float whilst the gravity from the ridigbody is trying to pull it down) When it stops, it’s very jittery.
I’ve tried to disable the rigidbody whilst not moving, but still has an issue.
Could someone offer any guidance on what I could try? Thanks!
var target : Transform;
var rotationSpeed = 3.0;
var moveSpeed = 3.0;
var maxAttackDistance = 0.0;
var isFree : boolean = true;
private var myPosition : Transform;
function Awake()
{
myPosition = transform;
}
function Start()
{
var go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
}
function Update()
{
if(isFree == true)
{
PlayerDetected();
}
else
{
animation.Play("Idle");
}
}
function PlayerDetected()
{
myPosition.rotation = Quaternion.Slerp(myPosition.rotation, Quaternion.LookRotation(target.position - myPosition.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myPosition.position) >= maxAttackDistance)
{
myPosition.position += transform.forward * moveSpeed * Time.deltaTime;
animation.Play("Run");
}
else
{
animation.Play("Idle");
}
}