- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
Master-Antonio · Jul 31, 2017 at 12:34 PM ·
follow playerfloating
Follow and Levitation around a object
Hello guys. I would like create a companion AI for my player. This companion must follow the player and at the same time must levitate around the player. I did this script but don't work. The effect is irrealistic.
1)https://i.gyazo.com/4615f4c5438205c4546fffb577d03efd.mp4 2)https://i.gyazo.com/32045e4f0526fc8d5878d3ccebcda7c4.mp4
The problem of the second video is due to this part of the code.
if (Vector3.Distance(transform.position, target.position)>2)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
Complete Script
public Transform target;
public float speed;
float originalY;
public float floatStrength = 1;
void Start() {
this.originalY = this.transform.position.y;
}
void FixedUpdate() {
float step = speed * Time.deltaTime;
if (Vector3.Distance(transform.position, target.position)>2)
{
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
transform.position = new Vector3(transform.position.x + 0.01f ,
target.position.y + ((float)Mathf.Sin(Time.time) * floatStrength),
transform.position.z);
}
Comment
Your answer