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 Teku-Studios · Nov 21, 2012 at 04:13 PM · rotationinputspriteangle2dtoolkit

Convert player input to angle/rotation?

Hi there, I will try to be as specific as possible.

I am developing a 2D sidescrolling game with sprites (2DToolkit), and I am stucked with this issue. First of all I need to make clear one thing: in order to change the sprite's transform without affecting the controls, I am storing it under a different game object which is child to the player main object.

Now, when I have my character swimming, I can make him dive so the player can control him with fully 360º freedom. I am trying to do something as in Rayman Origins, I want the sprite to rotate towards the player direction: when my character is diving down, I want the sprite to rotate and face the bottom of the screen, when he turns left I want the sprite to rotate and face left... and so on.

Is there any way to convert player input to an angle or rotation value? Or a way to compare both things? For now, what I have is this:

 var playerAngle : float = 0.0;
 
 
 for (var child : Transform in transform)
 {
     playerAngle += Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;
        
     //When Input             
     child.transform.Rotate(Vector3(0, 0, 1) * playerAngle);
                     
 }

But that is a very, very simple and rough code. I want the rotation of the sprite to stop when it matches the Player direction. Is there any quick way to do this or must I try it differently?

By the way, Input comes via WASD.

Thanks for your time.

Comment
Add comment · Show 1
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 Tim-Michels · Nov 23, 2012 at 09:48 AM 0
Share

Well, you must have some kind of variable that defines the current speed of your character. That's basically the only thing you need to know in order to calculate the player's rotation. I'd be happy to help you further, but I need to know how you're moving the character around.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Teku-Studios · Nov 23, 2012 at 08:00 PM

I kind of made it work. In case you guys are interested on it, here's my current code:

 //======================Diving===========================
 
 if(diving)
     {
         gravity = 0.0;
         
         moveDirection = Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
         
         if (Input.GetKey(KeyCode.RightArrow) && !facingRight)
         {
             script = gameObject.GetComponentInChildren(tk2dSprite);
             script.FlipX();
             facingRight = true;
         }
         
         else if (Input.GetKey(KeyCode.LeftArrow) && facingRight)
         {
             script = gameObject.GetComponentInChildren(tk2dSprite);
             script.FlipX();
             facingRight = false;
         }
         
         else if (Input.GetKey(KeyCode.DownArrow))
         {
             if(!facingRight)
             {
             script = gameObject.GetComponentInChildren(tk2dSprite);
             script.FlipX();
             script.FlipY();
             upsideDown = true;
             facingRight = true;
             }
         }
         else if (Input.GetKey(KeyCode.UpArrow))
         {
             if(!facingRight)
             {
             script = gameObject.GetComponentInChildren(tk2dSprite);
             script.FlipX();
             script.FlipY();
             upsideDown = true;
             facingRight = true;
             }
         }
         
         if(Input.GetKey(KeyCode.LeftArrow) && upsideDown)
         {
             script.FlipY();
             upsideDown = false;
         }
         else if(Input.GetKey(KeyCode.RightArrow) && upsideDown)
         {
             script.FlipY();
             upsideDown = false;
         }
         
         if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.LeftArrow))
         {
                 for (var child : Transform in transform)
                 {
                     var moveVector = Vector3.zero;
 
                     moveVector.x = Input.GetAxis("Horizontal");
 
                        moveVector.y = Input.GetAxis("Vertical");
                        
                     if(Input.GetKey(KeyCode.LeftArrow))
                     {
                         moveVector.x = -Input.GetAxis("Horizontal");
                         moveVector.y = -Input.GetAxis("Vertical");
                     }
 
                     var angle = Vector3.Angle(moveVector, Vector3.right);      
 
                     var cross = Vector3.Cross(moveVector, Vector3.right).normalized;
                     
                     child.transform.rotation = Quaternion.Euler(Vector3(0, 0, angle * -cross.z));
                 }
         }    
     }

Now I just have some weird issues to solve involving the sprite (it makes odd Flips sometimes, but I think I just messed it up with excessive "facingRight" and "upsideDown" coding).

Anyway, the rotation of the sprite works fine.

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

10 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

Related Questions

Angle of parametric path 1 Answer

Rotate angle between two points 0 Answers

Shooting in direction of the character movement 2 Answers

Rotating sprite through touch (storing current rotation) 0 Answers

Rotate object after swipe according to swipe direction 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