Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Addyarb · Jul 06, 2016 at 02:45 PM · rotationjoystick

Rotating a sphere with a joystick

Hi Unity!

So I'm working on a small game where your player is essentially stationary on a small planet, as the planet moves beneath you. I'm stumped on how to translate my joystick inputs (normalized 0-1 x and y inputs) into 3D rotation. I've tried quite a few different methods.

What I'm essentially looking for is for the ball to roll (without actually moving) towards the direction I'm pushing the joystick. For instance: From a dead stop, with an arbitrary starting rotation, if I push the joystick to the right (Horizontal input axis ==1) then the ball should spin counter-clockwise on the y axis. Likewise for the x axis for up and down, and any combination of the two.

Here's the code I've tried that I've gotten closest with:

     void Update()
     { 
         float moveHorizontal = CnInputManager.GetAxis("Horizontal");
         float moveVertical = CnInputManager.GetAxis("Vertical");
 
         //Regular rotation approach. Loses proper axis as soon as
         //both X and Y are greater than (or less than) zero at the same time
         transform.Rotate(moveHorizontal, moveVertical, 0);
     }

Here's a diagram of what I'm looking for:

alt text

ballrotation.png (98.9 kB)
Comment
Add comment
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

3 Replies

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

Answer by npoguca · Jul 06, 2016 at 04:53 PM

That's a piece from my app that kinda does that. Look into World and Local rotation though

   float p=  CrossPlatformInputManager.GetAxis("Vertical") / body.velocity.magnitude*500;
             float h = CrossPlatformInputManager.GetAxis("Horizontal") / body.velocity.magnitude*500;
             body.transform.Rotate(transform.forward, -h, Space.World);
             body.transform.Rotate(transform.right,p,Space.World);






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 Addyarb · Jul 06, 2016 at 06:21 PM 0
Share

This wasn't exactly what I needed - but it did lead me to the solution. I ended up using :

 transform.Rotate(moveVertical, moveHorizontal, 0, Space.World);


In other words, using the Space.World parameter corrected the issue. I forgot all about that overload! $$anonymous$$ostly because it doesn't prove useful that often.

Thanks!

avatar image
0

Answer by fbecker · Jul 06, 2016 at 05:52 PM

Try:

  float speed = 1f;
 
      void Update()  { 
          float moveHorizontal = CnInputManager.GetAxis("Horizontal");
          float moveVertical = CnInputManager.GetAxis("Vertical");
      
          //Regular rotation approach. Loses proper axis as soon as
          //both X and Y are greater than (or less than) zero at the same time
          transform.Rotate(new Vector3(moveHorizontal,moveVertical,0)
     *speed* Time.deltaTime);  }

I've add a speed value to better control the rotation.

Comment
Add comment · 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
0

Answer by Glurth · Jul 06, 2016 at 04:35 PM

 transform.Rotate(moveHorizontal, moveVertical, 0);

This function will ADD to the transform's current rotation, about each local axis, the euler angles you provide. It is important to note that the objects local AXIS (transform.up and transform.right) will CHANGE as you do this. Calls to transform.Rotate will be around these local axis.

It sounds like you want to rotate about the WORLD Y-axis and WORLD X-axis, rather than the transform's current axis's. Perhaps something like this:

 transform.rotation *= Quaternion.AngleAxis(moveHorizontal,Vector3.up); //add a rotation around world Y axis, to objects current rotation
 transform.rotation *= Quaternion.AngleAxis(moveVertical,Vector3.right); //add a rotation around world X axis, to objects current rotation

Not positive, but i think this would also be equivalent to:

  transform.rotation *= Quaternion.Euler(moveVertical,moveHorizontal,0);
Comment
Add comment · 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

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

56 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate Locally Player Towards Joystick/Axis Input 1 Answer

No movement to negative z-axis. 1 Answer

advanced problem for me regarding transfering rotation from the gizmo to the object 0 Answers

stay the rotation in character 1 Answer

Making my Game Object face according to direction of mobile joystick 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