- Home /
How to make an object follow another object in a certain way
So I'm using the unity rollerball prefab and want an object to follow right behind it no matter which way it goes. For example: if the ball is going north/up, the object would be behind/ down, relative to the ball. If I'm going up and left the object would be down and right in comparison to the ball. I tried using,
public Vector3 offset; public Rigidbody player; public float distance; public GameObject trigger;
void Update () {
offset = -player.velocity * distance;
trigger.transform.position = player.transform.position + offset;
}
This gets the right effect, but because it is based on velocity, the faster the ball goes the farther the object lags behind. Also if the object stops moving then the object just appears inside the ball because the velocity is 0. Any suggestions how I could make this work? Anything is appreciated. I guess i'm trying to make a game similar to the Mario party game where you try to pop the balloon on the back of a go-kart where the balloon is always behind the kart.
Answer by MuffinMan64 · Jun 05, 2018 at 05:24 AM
@parkmaster15 I would have a desired goto position that would be located in the back of the ball. This could be either an empty gameObject or a gameObject that has a rigidbody without gravity and with kinematic set to true. You may need to play with the freeze x,y, and/or z constraints, if the ball actually rolls (you don't want your desired position to roll too!).
Lets call the desired object that follows the ball, ball_follower as a gameObject. Lets call the ball object, ball as a gameObject. Lets call the desired location, goto_spot as a child gameObject of ball. Lets assume that the following script is on ball.
If you want only one object to be behind the ball from the start:
void Start()
{
transform.parent = goto_spot.transform;
}
If you want only one object to be behind the ball and gradually go to the desired location:
float speed = 1f;
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, goto_spot.transform.position, speed);
}
Try it and let me know!
Your answer
Follow this Question
Related Questions
Partner follow the player 0 Answers
Instantiating Texture2D Object get non-readable error about AssetBundle 3 Answers
How to use individual Particles as objects 1 Answer
Get ClosestPointOnBounds when inside bounds? 2 Answers
How to create a Path 1 Answer