I’m trying to create a follow effect between an object that hovers slowly about the player. I cant go with the parenting method because the child needs to have total freedom in its rotation and localScale. It works independently from the player. I tried multiple methods,(lerp,slerp,moveTowards), but I keep getting some slight jerky movement when the object following the player is right above the player and trying to keep the same pace. Parenting does fix all the problems, but I don’t want to go that route. Does anyone have any suggestions for very smooth movement?
I’ve also tried doing this in the FixedUpdate, and Update step.
Create a GameObject with this hierarchy:
-[Follower]
–[Orbiter]
—[Fairy]
Then attach them this scripts. Follower:
public class SmoothFollow : MonoBehaviour {
public Transform Parent;
public float RotationSpeed = 3;
public float ForwardSpeed = 2;
public float MinDistance = .5f;
//Components
private Transform ThisTransform;
// Use this for initialization
void Start () {
ThisTransform = transform;
}
// Update is called once per frame
void Update () {
float distance = Vector3.Distance(ThisTransform.position, Parent.position);
if (Parent && distance > MinDistance)
{
float rotstep = RotationSpeed * Time.deltaTime;
Quaternion ND = Quaternion.LookRotation(Parent.position - ThisTransform.position);
//Rotate to target
ThisTransform.rotation = Quaternion.Slerp(ThisTransform.rotation, ND, rotstep);
//Move to target
ThisTransform.Translate(Vector3.forward * ForwardSpeed * Time.deltaTime);
}
}
}
Orbiter:
public class Orbiter : MonoBehaviour {
private float X;
private float Y;
private float Z;
private Transform ThisTransform;
// Use this for initialization
void Start () {
ThisTransform = transform;
StartCoroutine("newrotation");
}
IEnumerator newrotation()
{
yield return new WaitForSeconds(Random.Range(2, 5));
X = Random.Range(.3f, 1);
Y = Random.Range(.3f, 1);
Z = Random.Range(.3f, 1);
StartCoroutine("newrotation");
}
// Update is called once per frame
void Update () {
ThisTransform.Rotate(X, Y, Z * Time.deltaTime);
}
}