Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 PowerCore2000 · Aug 02, 2015 at 08:08 AM · rotation

2D sprite won't rotate

Hi, I have this script attached to a 2D sprite that is meant to take rotate to a different position based on which arrow theu ser presses. But in the vertical section, instead of rotating it to the direction the code specifies, it just set the sprite's z rotation to 180. What did I do wrong?

 function ChooseShotDirection(){
 
 
     
     if(Input.GetAxis("Fire Horizontal") > 0){
 
         transform.rotation.z = 0;
         transform.rotation.y = 180;
 
 Debug.Log("Shot Right");
 
 
     }
             
     else if(Input.GetAxis("Fire Horizontal") < 0){
 
         transform.rotation.z = 0;
         transform.rotation.y = 0;
 
 Debug.Log("Shot left");
 
     }                        
                                     
                                                 
                                                             
     else if(Input.GetAxis("Fire Vertical") > 0){
 
         transform.rotation.z = 270;
         transform.rotation.x = 0;
         transform.rotation.y = 0;
 
 Debug.Log("Shot Up");
     }
             
     else if(Input.GetAxis("Fire Vertical") < 0){
 
         transform.rotation.z = 90;
         transform.rotation.x = 0;
         transform.rotation.y = 0;
 
 Debug.Log("Shot Down");
 
 
     }                        
                                                                                 
                                                                                     
 
 }
 
    
 
 
 
 
 
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 PowerCore2000 · Aug 02, 2015 at 02:32 AM 0
Share

Sorry, this is my first post so i'm kinda new to it. I believe I formatted it correctly now.

2 Replies

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

Answer by PowerCore2000 · Aug 13, 2015 at 09:59 AM

I manged to fix the issue like this. I set the sprite to its up position by default and saved it in the UpPosition var. Then for the horizontal rotations I did it conventional, but the vertical ones I referred to the UpPosition var and inverted it for down.

 function ChooseShotDirection(){
 
 
     
     if(Input.GetAxis("Fire Horizontal") > 0){
 
         transform.rotation.z = 0;
         transform.rotation.y = 180;
         transform.rotation.x = 0;
     
         var TheShotR = Instantiate(Ammo, Right.position,  Quaternion.identity);        
         TheShotR.GetComponent(Transform).rotation = Quaternion.Euler(0, 0, 270);
         
 //Debug.Log("Shot Right");
 
 
     }
             
     else if(Input.GetAxis("Fire Horizontal") < 0){
 
         transform.rotation.z = 0;
         transform.rotation.y = 0;
         transform.rotation.x = 0;
         
         var TheShotL = Instantiate(Ammo, Left.position, Quaternion.identity);        
         TheShotL.GetComponent(Transform).rotation = Quaternion.Euler(0, 0, 90);
         
 //Debug.Log("Shot left");
 
     }                        
                                     
                                                 
                                                             
     else if(Input.GetAxis("Fire Vertical") > 0){
 
 
         transform.rotation = UpPosition;
         transform.rotation.x = 0;
     
         var TheShotU = Instantiate(Ammo, Up.position, Quaternion.identity);
         //TheShotU.GetComponent(Transform).rotation = Quaternion.Euler(0, 0, 180);
 
 //Debug.Log("Shot Up");
     }
 
             
                         
                                                 
     else if(Input.GetAxis("Fire Vertical") < 0){
         transform.rotation = UpPosition;
         transform.Rotate(180,180,0);
     
 
         var TheShotD = Instantiate(Ammo, Down.position,  Quaternion.identity);
         TheShotD.GetComponent(Transform).rotation = Quaternion.Euler(0, 0, 180);
 //Debug.Log("Shot Down");
 
 
     }                        
                                                                                 
                                                                                     
 
 }
 
 
 
 
 
 
 
 
 

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 shay4545 · Aug 02, 2015 at 03:57 PM

not sure what the problem is, but I don't think you should be able to set the rotations correctly like that if you are using c#. Try this instead of setting the values separately.

 transform.eulerAngles = new Vector3(xValue, yValue, zValue);

or

 transform.rotation = Quaternion.Euler(new Vector3(xValue, yValue, zValue));


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 boxed_9k · Aug 02, 2015 at 04:49 PM 0
Share

Unity documentation says transform.rotation takes a Quaternion. I think you meant transform.Rotate Docs for rotation

avatar image shay4545 · Aug 02, 2015 at 05:04 PM 0
Share

Yes my bad, I fixed it

avatar image PowerCore2000 · Aug 03, 2015 at 06:35 AM 0
Share

Im using javascript though...

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Movement based on rotation 1 Answer

How do I rotate a character with joystick input in an isometric view? 1 Answer

Flip over an object (smooth transition) 3 Answers

How do I make my player face the direction its moving. 0 Answers

Problems caused by non-unique Euler angle solutions 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