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 . 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.