- Home /
Get point in plane where the normal crosses a GameObject in local space
Hello, I am developing a VR game where I want to steer a boat using a steering wheel. Therefore I grab the wheel with a controller and want to turn the wheel on one axis (as wheels do) according to the controller movement.
My plan for it was to create a Vector3 plane in the script, which resembles the plane of the wheel. Then I would take the normal of it and use the Vector3.ProjectOnPlane()-Method to find the point on the plane to resemble the controller. When I have the point, I would rotate the steering wheel to this position. This procedure seems pretty complex and I don't even know how to create a plane in local space (so that it can rotate and move with the ship).
So my question is: Is there a better way of achieving this and if no, how could my solution be implemented?
Answer by Master109 · Mar 20 at 06:03 PM
The following might work assuming the pivot point of the wheel is at the center and the top is facing up at rotation (0, 0, 0):
wheelTrs.up = controllerTrs.position - wheelTrs.position;
The following will work if the previous assumptions were true:
wheelTrs.rotation = Quaternion.LookRotation(wheelTrs.forward, controllerTrs.position - wheelTrs.position);
Please comment if the previous assumptions were false and I will let you know how to proceed.
Answer by dtilgner · Mar 21 at 12:26 PM
Hey @Master109 , thanks for the fast reply :) The pivot point is indeed at the center, but the top isn't facing up. The parent of the wheel is slightly tilted, but the wheel itself where the script is attached has a rotation of (0,0,0). Nevertheless I tried your approach - and it works somehow :D the wheel is rotating around the Z axis - but I need it to be rotated around Y. I somewhat understand what you are doing, but I dont get how to adjust this line to do what I want to do. I tried using wheelTrs.up and wheelTrs.right, but then the wheel is just bugging around. Other adjustments that seemed to be logic didn't do what I wanted either. What am I missing?
Also I have the player grabbing the wheel anywhere and now i want him to start from this position to turn, but I guess I just need to add the angle difference then.
using UnityEngine;
public class Wheel : MonoBehaviour
{
public Transform trs;
public Transform controllerTrs;
bool isGrabbed; // You will need to set this variable
bool previousIsGrabbed;
Vector3 previousGrabPosition;
void Update ()
{
if (isGrabbed)
{
Vector3 grabPosition = controllerTrs.position;
float grabAngle = Vector3.SignedAngle(grabPosition, trs.position, trs.up);
if (!previousIsGrabbed)
previousGrabPosition = grabPosition;
trs.Rotate(Vector3.up * Vector3.SignedAngle(previousGrabPosition - trs.position, grabPosition - trs.position, trs.up));
previousGrabPosition = grabPosition;
}
previousIsGrabbed = isGrabbed;
}
}
Thanks for the response, I tried that solution. But somehow this does not work in my case. I will go on trying and your answer helped me understanding some key mechanics. :) If I find a solution or have a more specific question, I will post it here.
Your answer
Follow this Question
Related Questions
orizontal slider to control a plane's y transform variable 1 Answer
Imparting Physics in only certain planes of motion 1 Answer
Local Euler Angles not Rotating GameObjects 1 Answer
(Steam VR / Vive) Rigidbody moving in only one direction after collision 1 Answer
Assign Object to Unity Event (Select Entered) of Prefabbed Game Object with "XR Grab Interactable" 1 Answer