So I have a car object and basically the player will select a part on it and be able to look at the description and other things. What I currently want to do is when the user clicks on one of these parts that the car rotates so that the part is looking at the player so to speak. My problem is I am terrible with Vector math lol. I have a script that basically gets the child(part) object, the parent(car) object, and the player/camera object when ever the user clicks on a part. I feel as though my math is wrong here but I just can't figure it out. What I am doing in the script below is making the positions into two dimensions because I only want to rotate on the y axis of the parent object.

Vector3 hitObjPos = new Vector3(hitObject.transform.position.x, 0, hitObject.transform.position.z);
Vector3 baseObjPos = new Vector3(baseObj.transform.position.x, 0, baseObj.transform.position.z);
Vector3 playerPos = new Vector3(transform.position.x, 0, transform.position.z);

Vector3 childHeading = hitObjPos - baseObjPos;
Vector3 playerHeading = playerPos - baseObjPos;

float angle = Vector3.Angle(childHeading, playerHeading);

if(angle > 75 || angle < -75)
    baseObj.transform.rotation = Quaternion.Euler(0, baseObj.transform.eulerAngles.y+ angle, 0);


There are a couple of problems, first off I guess I need to create some type of check on the angle requirement so that when I click on say an object that is already perpendicular to the player, like a car door when viewing from the side, that it doesn't rotate the baseObj. However, another problem I am having is the rear items rotate fine and I can see them great but, the front objects rotate away from the user and I am basically looking at the rear of the object.


1 Answer 1


Since i don't have enough Reputation i can't comment: Whats the problem? Since you didn't say what the problem actually is i can only guess. The part is on the wrong side? (180 degrees) Change

childHeading = hitObjPos - baseObjPos


childHeading = baseObjPos - hitObjPos

If you just don't know how to apply that angle, then you can put it into the transform as the rotation.

  • \$\begingroup\$ I have updated the question, and some of the source code as I have edited a little since posting. As I stated above, half of the child objects rotate in the correct direction but the other half do not. \$\endgroup\$
    – Dtb49
    Commented Apr 11, 2018 at 19:26
  • \$\begingroup\$ I do understand why you want to check that angle. Can you maybe post a picture of the car (in a way: how it is and how it should be), that way i have an easier time imagining what is going on \$\endgroup\$
    Commented Apr 13, 2018 at 10:12
  • \$\begingroup\$ Lol I probably need to update this again as I continue to make progress. I figured out that Vector3.Angle was only giving a positive output which is why it only rotated one direction. \$\endgroup\$
    – Dtb49
    Commented Apr 13, 2018 at 12:54
  • \$\begingroup\$ Its probably not the most efficient way to do it, but you might want to use the cross product to determine if its a negative angle or not. By that i mean check the y coordinate of the normal vector. It should be negative when mathemtically negative and positive when mathematically positive. \$\endgroup\$
    Commented Apr 13, 2018 at 16:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .