Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RoyDe · Jan 26, 2015 at 03:53 AM · movementrotation axiswheel

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);
     }
 }

}

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kaushik-d · Jan 26, 2015 at 04:56 AM 0
Share

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?

avatar image RoyDe · Jan 26, 2015 at 04:58 AM 0
Share

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.

avatar image RoyDe · Jan 26, 2015 at 05:01 AM 0
Share

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);
     }
 }

}

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RoyDe · Jan 28, 2015 at 02:42 AM 0
Share

Works perfectly, thanks a lot!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Bike Control 0 Answers

How to turn a character frame independently? 1 Answer

how to get slip angle when moving car left or right while moving in forward direction or get a realistic look without adding any wheel colliders? 0 Answers

2d character moves on the x and y-axis while only rotating at x-axis with joystick 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges