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 /
  • Help Room /
avatar image
0
Question by Drksteel · Nov 17, 2017 at 06:48 PM · joystickrotate object

Rotate object to joystick direction

What i want to do is take an object along the Y axis and rotate it to whatever direction the joystick is rotating towards. I have not been able to find a suitable answer. I got it to point forward, left and right, but only the absolute left and right, and i can't point the object backwards at all

Here's the code for reference.

     public float speed;
     private Vector3 Target;
 
     private float target;
 
     private void Update()
     {
         target = Input.GetAxis("Right Stick L/R") + Input.GetAxis("Right Stick U/D");
         Target.Set(target, 90, 0);
         Quaternion rotation = Quaternion.LookRotation(Target);
         transform.rotation = rotation;
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by JustinC · Jan 20, 2018 at 05:46 PM

@Drksteel,

I know this is an older post, don't know if you have figured it out yet or not. If i understand your question, you want your object to rotate in the same orientation as your joy stick. I have a bit of code that I am using right now with the virtual joystick in the standard assets. works pretty well for me.

  `void Twist ()
     {
         h1 = CrossPlatformInputManager.GetAxis("Horizontal"); // set as your inputs 
         v1 = CrossPlatformInputManager.GetAxis("Vertical");
         if (h1 == 0f && v1 == 0f) { // this statement allows it to recenter once the inputs are at zero 
             Vector3 curRot = twistPoint.transform.localEulerAngles; // the object you are rotating
             Vector3 homeRot;
             if (curRot.y > 180f) { // this section determines the direction it returns home 
                 Debug.Log (curRot.y);
                 homeRot = new Vector3 (0f,359.999f, 0f); //it doesnt return to perfect zero 
             } else {                                                                      // otherwise it rotates wrong direction 
                 homeRot = Vector3.zero;
             }
             twistPoint.transform.localEulerAngles = Vector3.Slerp (curRot, homeRot, Time.deltaTime*2);
         } else {
             twistPoint.transform.localEulerAngles = new Vector3 (0f, Mathf.Atan2 (h1, v1) * 180 / Mathf.PI, 0f); // this does the actual rotaion according to inputs
         }
     }`

Let me know if this is what you are looking for

Comment
Add comment · Show 3 · 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 areejyahya · Oct 07, 2018 at 04:06 PM 0
Share

Plz where I can put this code for rotation my joystick

avatar image Mikenekro areejyahya · Oct 12, 2018 at 02:54 AM 0
Share

@areejyahya

If you don't understand how to use this code, I highly suggest you learn more about program$$anonymous$$g and Unity in general. You will be struggling to do basic things if you do not know how the above code works. Watch a lot of youtube videos or sign up for some classes at a local college.

To answer your question, this function can go anywhere you want it to. All you need is a public class level variable (of type GameObject) in the script named "twistPoint". Then either make sure you have the CrossPlatformInput$$anonymous$$anager or just change that to Unitys default input system. You can call the function from Update(). Once your script is ready, make sure you assign a GameObject to the "twistPoint" variable in the inspector.

If that didn't make sense to you, try looking up some of the things I mentioned. Not phrases but specifics. Search for things like: "GameObject", "Class Level Variable", "Unity C# Syntax", "Unity Input $$anonymous$$anager" just to name a few.

avatar image lleirbag · May 15, 2020 at 07:24 PM 0
Share

A$$anonymous$$AZINNNNNG. Just what I was looking for for so many days. Thanks! You've posted an answer months later the topics posting and helped a lot. We're in 2020 and it saved my project. ^^ thanks a lot.

avatar image
0

Answer by areejyahya · Oct 07, 2018 at 04:06 PM

Plz where I can put this code for rotation my joystick

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

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

my 2d Object dont rotate smoothly with joystick 1 Answer

Game pad controller (submarine rotation) 0 Answers

Rotate object to joystick and position direction 0 Answers

Rotate Arm in Direction of Joystick 1 Answer

how to fix automatic camera rotation when using joysticks? 0 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