• 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
6
Question by anosmicAnimator · Nov 21, 2013 at 02:05 AM · 2drotationsprite

Trying to rotate a 2D sprite

I've got this code attached to a 2D sprite that I want to rotate. So far I'm working on making it rotate left when the left arrow key is pressed. I get no parsing errors, but when I run the game, the code doesn't work. Any idea what I'm doing wrong?

 using UnityEngine;
 using System.Collections;
 
 public class Rotate : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetButtonDown ("left"))
             RotateLeft();
     }
 
     void RotateLeft () {
         Quaternion theRotation = transform.localRotation;
         theRotation.z *= 270;
         transform.localRotation = theRotation;
     }
 }
 
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
14
Best Answer

Answer by Eric5h5 · Nov 21, 2013 at 03:50 AM

 void RotateLeft () {
    transform.Rotate (Vector3.forward * -90);
 }
Comment
Add comment · Show 9 · 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 anosmicAnimator · Nov 21, 2013 at 04:04 AM 0
Share

Looks like I have another problem now...

 UnityException: Input Button left is not setup.
  To change the input settings use: Edit -> Project Settings -> Input
avatar image prof · Nov 21, 2013 at 04:29 AM 0
Share

Input

avatar image Eric5h5 · Nov 21, 2013 at 04:31 AM 1
Share

Did you do what the error message says?

avatar image anosmicAnimator · Nov 21, 2013 at 04:38 AM 0
Share

Alright, it works now. Thanks for your help, everybody!

avatar image aman_jha · Oct 13, 2014 at 02:37 PM 2
Share

$$anonymous$$ark this answer as correct, then.

Show more comments
avatar image
2

Answer by epicpython · Sep 18, 2016 at 02:41 PM

Instead of subtracting 90 from the forward direction, you can use Vector3.forward and vector3.back:

 //rotate counterclockwise
 transform.Rotate (Vector3.forward);
 
 //rotate clockwise
 transform.Rotate (Vector3.back);

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 WarriorGecko · Oct 30, 2020 at 10:21 AM 0
Share

Where do I put this code?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

28 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

Related Questions

2D Sprite Rotation 0 Answers

Rotating a Sprite to face mouse / target? 2 Answers

Having Issues Rotating 2d Sprites to face another 2d Object 3 Answers

Sprite is a little of when rotating 1 Answer

Sprite changing position during rotation...bug? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges