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 Paradox_01 · Feb 02, 2015 at 11:16 AM · 2drotationmovementflip

Rotate/Flip Image in Unity 2d using Virtual Joystick

I have a problem, I have this right arrow, right? and I have made a virtual joystick using Control Freak 1.1.0. My problem is I can make this image move but the problem is that I want to rotate this image based on the movement I make in the Virtual Joystick

[1]: /storage/temp/39980-arrowhead.png

Here is the code for the Image Movement (it's in Javascript):

var power: float =1500;

var moveSpeed: float = 5;

function Update () {

 if (CFInput.GetAxis("Horizontal") || CFInput.GetAxis("Vertical"))

 {

     var h : float = CFInput.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;

     var v : float = CFInput.GetAxis("Vertical") * Time.deltaTime * moveSpeed;

     transform.Translate(h,v,0);

 }

}

I really need help. I just want to make this image facing based on the direction it was moving. Thanks in advance guys!

arrowhead.png (4.7 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by moonstruck · Feb 02, 2015 at 02:10 PM

Try something like this if you need rotation in the XY plane:

(C# version)

     Vector3 dir = new Vector3( CFInput.GetAxis( "Horizontal" ), CFInput.GetAxis( "Vertical" ), 0 );
     float dirLen = dir.magnitude;
         
     float angle;
     
     if( dirLen < 0.01f ) {
         angle = 0;
     }
     else {
         dir /= dirLen;
     
         float a = Vector3.Dot( dir, Vector3.right );
         Vector3 x = Vector3.Cross( dir, Vector3.right );
         float b = x.magnitude;
     
         angle = Mathf.Sign( Vector3.Dot( Vector3.forward, x ) ) * Mathf.Rad2Deg * Mathf.Atan2( b, a );
     }
     
     transform.rotation = Quaternion.AngleAxis( angle, Vector3.back );

(JS version)

     var dir : Vector3 = Vector3( CFInput.GetAxis( "Horizontal" ), CFInput.GetAxis( "Vertical" ), 0 );    
     var dirLen : float = dir.magnitude;
         
     var angle : float;
     
     if( dirLen < 0.01f ) {
         angle = 0;
     }
     else {
         dir /= dirLen;
     
         var a : float = Vector3.Dot( dir, Vector3.right );
         var x : Vector3 = Vector3.Cross( dir, Vector3.right );
         var b : float = x.magnitude;
     
         angle = Mathf.Sign( Vector3.Dot( Vector3.forward, x ) ) * Mathf.Rad2Deg * Mathf.Atan2( b, a );
     }
     
     transform.rotation = Quaternion.AngleAxis( angle, Vector3.back );

Comment
Add comment · Show 14 · 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 Paradox_01 · Feb 02, 2015 at 04:02 PM 0
Share

what does dir.magnitude do?

avatar image moonstruck · Feb 02, 2015 at 04:05 PM 0
Share

It returns the length of the vector dir.

avatar image Paradox_01 · Feb 02, 2015 at 04:08 PM 0
Share

when I used this code I got an error but its not showing

avatar image moonstruck · Feb 02, 2015 at 04:15 PM 0
Share

Sorry, my bad, it is in C#, adding JS version.

avatar image Paradox_01 · Feb 02, 2015 at 04:26 PM 0
Share

there is some bug i think when i move right its ok when i move left the image will rotate to left but still going right same with up and down sorry im new to unity

Show more comments

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

How Can I Rotate My 2D Top-Down Character Rotation With Mobile Joystick? 0 Answers

Joystick 2D Sprite Rotation and Movement 0 Answers

2D rotation and moving forward 2 Answers

problem with twin stick shooter controls 2 Answers

My sprite is teleporting when I change my scale to -1 to flip it 1 Answer


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