- Home /
Why does the wheel rotate on a different axis?
Hi, I'm coding a robot that is supposed to move forward and sideways. It all goes smoothly until I decide to make a right/left turn. In that moment the wheel's axis orientation changes and, even though it still rotates on the Z axis (as it should). Could someone point me my mistake, please? I've tried changing axis and still nothing.
public class RobotMovement : MonoBehaviour { float rotangle; public GameObject wheel; // Use this for initialization void Start () { Debug.Log ("RobotMovement start"); rotangle = 0; }
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.W)) {
transform.Translate (0, 0, 3 * Time.deltaTime);
transform.Rotate (0, rotangle*Time.deltaTime, 0, Space.World);
if (rotangle<=0){
rotangle=rotangle+20*Time.deltaTime;
}
if (rotangle>=0){
rotangle=rotangle-20*Time.deltaTime;
}
}
if (Input.GetKey (KeyCode.D)&& rotangle <= 180) {
rotangle=rotangle+40*Time.deltaTime;
wheel.transform.Rotate (0,40*Time.deltaTime,0, Space.World);
}
if (Input.GetKey (KeyCode.A)&& rotangle >= -180) {
rotangle=rotangle-40*Time.deltaTime;
wheel.transform.Rotate (0,40*Time.deltaTime,0, Space.World);
}
}
}
Is the wheel a child of the robot object? Or, is it the other way round? And when you say sideways movement you mean the wheel moves first and then the main robot object follows, right?
Yes, it is a child, and yes, that's the idea, but if moving the robot first and making the wheel follow works, then I'm open to that idea.
UPDATE: I just removed the following script, which makes the wheel rotate on it's Z-axis (assignment requisition). I think the problem is in it rather than in the $$anonymous$$ovement script:
public class Wheel : $$anonymous$$onoBehaviour { // Use this for initialization void Start () { Debug.Log ("Wheel start"); }
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W)) {
transform.Rotate (0, 0, -200 * Time.deltaTime, Space.World);
}
}
}
Answer by kaushik-d · Jan 26, 2015 at 05:49 AM
I'm not sure whether this is what you wanted, but this code works. Basically, wheel is still a child of robot's body. We rotate the wheel first and then when the robot translates, we align the robot's body to where its wheel is. I've made my comments inline as well.
void Update () {
if (Input.GetKey (KeyCode.W)) {
transform.Translate (0, 0, 3 * Time.deltaTime);
//Checking whether body is aligned with the wheel
if (!Mathf.Approximately(wheel.transform.localEulerAngles.y, 0f)){
//Since the wheel is attached to the body its rotation in world space will change when body rotates.
//To prevent this we maintain the wheel's rotation in world space.
Quaternion origWheelRot = wheel.transform.rotation;
//Rotate body towards wheel's rotation
transform.rotation = Quaternion.Lerp(transform.rotation, wheel.transform.rotation, Time.deltaTime);
wheel.transform.rotation = origWheelRot;
}
//Don't need to update rotate angle again
// if (rotangle<=0){
// rotangle=rotangle+20*Time.deltaTime;
// }
// if (rotangle>=0){
// rotangle=rotangle-20*Time.deltaTime;
// }
}
if (Input.GetKey (KeyCode.D)&& rotangle <= 180) {
rotangle=rotangle+40*Time.deltaTime;
wheel.transform.Rotate (0,40*Time.deltaTime,0, Space.World);
}
if (Input.GetKey (KeyCode.A)&& rotangle >= -180) {
rotangle=rotangle-40*Time.deltaTime;
wheel.transform.Rotate (0,-40*Time.deltaTime,0, Space.World);//Updated the 40 to -40
}
}