How would I be able to control the lateral movement of a game object (table) using another rotating game object (steering wheel)? I currently can control the rotation of the steering wheel but am having trouble figuring how I would make the connections for the table to move to the right when the steering wheel is turned clockwise and to the left when turned counter clockwise .
Answer by EpiFouloux
Feb 28 at 07:48 AM
May you bring a picture of the project? I'm not sure about what you are asking.
I think you just need to parent the table to the steering wheel (Drag the table inside the steering wheel)
Then, whenever the wheel turns, the table will also do it.
Warning Parenting is a useful tool, and you probably already know it, but for rotations don't use non-uniform scaled objects.
If parenting is not the solution, You should then go through scripting, and I'll be glad to help.
My understanding of parenting is that the child object would just move in the same direction which isn't what I am going for so I think scripting would be the way. If you look at the attached picture I am trying to control the horizontal movement of the white table object to the left or right by rotating the purple wheel below it. So if I rotate the wheel clockwise the table moves to the right and if I rotate the wheel counter clockwise it moves to the left along a the x axis. Appreciate the help!
Yes parenting is not the solution you need to go scripting, with transforming the rotation value of the wheel to translate value for the box object !
That means it will work only in play mode, is that fine with you?
Yeah it only working in play mode is fine. Appreciate the help!
Answer by Drigomen
Mar 16 at 04:53 AM
You could create a "ControlledByWheel" component an attach it to your table. So, you must reference your wheel in the inspector and it should (barely) work.
public class ControlledByWheel : MonoBehaviour
public Transform wheel; // reference to the object you want be controlled by
public float factor = 0.1f; // how much "fast" you move
body = GetComponent<Rigidbody>();
lastRotation = wheel.eulerAngles.z;
if (wheel == null) return;
float deltaRotation = wheel.eulerAngles.z - lastRotation; // EVIL!!
deltaRotation = ClampedRotation(deltaRotation);
if (deltaRotation != 0f)
// Move the attached Rigidbody to Left/Right matching the change in
// rotation of the wheel object
var deltaPosition = Vector3.left * (factor * deltaRotation);
body.MovePosition(transform.position + deltaPosition);
lastRotation += deltaRotation;
// A silly try to compensate EVILNESS of transform.eulerAngles
// Only works for deltaRotation < 180
float ClampedRotation(float angle)
angle %= 360f;
if (angle >= 180f)
angle -= 360f;
else if (angle <= -180f)
angle += 360f;
Note 1: transform.eulerAngles is EVIL! It accepts and/or returns angles clamped/non-clamped by 360, but never the way you expect or desire.Note 2: I assumed you want a RigidBody in your object in order to move it (recommended). In this case, consider making it kinematic too. But if you want to, you can use increase "transform.position".
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
8 People are following this question.
How to make camera position relative to a specific target.
Multiple Cars not working
Distribute terrain in zones
Null Reference Exception Help
Make sound play at same time as object moves