- Home /
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
Best Answer
Answer by Eric5h5 · Nov 21, 2013 at 03:50 AM
void RotateLeft () {
transform.Rotate (Vector3.forward * -90);
}
Looks like I have another problem now...
UnityException: Input Button left is not setup.
To change the input settings use: Edit -> Project Settings -> Input
Alright, it works now. Thanks for your help, everybody!
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);
Your answer
Follow this Question
Related Questions
2D Sprite Animation - Rotate animation on axis 1 Answer
Ground checking with a frequently rotating player 2 Answers
2D LookAt not working as intended 1 Answer
2 fingers sprite rotation 1 Answer