- Home /
Change game objects single axis value based on rotation of another gameobject
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, 2017 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.
$$anonymous$$y 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 transfor$$anonymous$$g 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, 2017 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.
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
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
float lastRotation;
Rigidbody body;
void Start()
{
body = GetComponent<Rigidbody>();
lastRotation = wheel.eulerAngles.z;
}
void Update()
{
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;
}
return angle;
}
}
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".
Your answer
