Hi!
I need to keep the arrow, which is a child of the sphere, facing forward. The sphere is linked to the player via the code.
void Update()
{
Vector3 directionToPlayer = playerBody.position - transform.position;
transform.forward = -directionToPlayer;
}
The problem is that when the player gets too close the arrow goes into the ground.
I’ve tried a handful of different things but I don’t know how to get it to work the way I need it to. I think if I could get the ball to only rotate on the y-axis, that would work, but I’m unsure how to do that and still have the player movement decide the sphere’s movement.
Don’t make the arrow the child of the sphere. Make them both siblings of a parent object. The arrow can follow the sphere’s position, and be rotated towards the target.
Sorry I think I didn’t explain what I needed well, or I’m misunderstanding your solution, I need the arrow to rotate around the ball like this:

The problem is when I get close and when the player passes over the sphere the arrow goes under the ground like this:

The expected behaviour of the arrow when the player is passing over it is that it quickly rotates around the y-axis of the ball and is facing 180 degrees from where it was pointing.
Right, I didn’t realise the ‘player’ is the camera.
You just need to flatten your direction, which is super simple:
Vector3 directionToPlayer = playerBody.position - transform.position;
directionToPlayer.y = 0f;
transform.forward = -directionToPlayer;
Yeah I realized and ended up with this
void Update()
{
Vector3 playerVecNoY =
new Vector3(playerBody.position.x, 0.0f, playerBody.position.z);
Vector3 ballVecNoY =
new Vector3(transform.position.x, 0.0f, transform.position.z);
Vector3 directionToPlayer = playerVecNoY - ballVecNoY;
transform.forward = -directionToPlayer;
}
which is the same but 10x the code, yikes. Thank you so much!
1 Like