- Home /
Orienting multiple objects on the same line
Hello, I am trying to create a mini golf game. I am having a bit of trouble figuring out an issue. When the golf ball is at rest and has stopped moving, I would like the golf club to re-orient itself a short ways behind the ball. However, I would like it to be positioned in such a way that it is auto-aiming for the player, facing the hole. So, imagine if a line is placed from the center of the hole to the center of the ball, the golf club would be on that same line, just a bit behind the ball and facing the ball. I have looked into using Vector3.Angle and several other methods, but I am still a bit lost. Any help would be appreciated. Thanks a lot!
-Andrew
Answer by robertbu · Jun 09, 2013 at 05:25 AM
You can get placement like this:
var v3 = (ball.transform.position - hole.transform.position).normalized;
club.transform.position = hole.transform.position + v3 * ball_distance;
'ball_distance' is the distance you want the club away from the ball. Assuming you have the club constructed so that positive 'z' is forward, and the ball is rolling on the X/Z plane, you can do this to rotate the club:
var v3 = hole.transform.position;
v3.y = club.transform.y;
club.transform.LookAt(v3);
Oh, thank you so much, robertbu! This seems to work just fine :)
Please click on the checkmark next to the question to close it out. Thanks.
Your answer
Follow this Question
Related Questions
Rotate an object around another object at an angle from the X axis? 1 Answer
Make an object move to a given point by rotating to the correct direction first. 1 Answer
Imported model is rotated 90 degrees off of straight, can't use any transforms 1 Answer
How to make Y-Axis face away from a position? 2 Answers
how to scale the transport? 1 Answer