- Home /
How can I rotate a character 180 degrees by D, and -180 by A
I try to move a 3d character like a 2d character in a platform game. What I mean is; when I press D, the character rotate and move to the right , and when I press A, the character rotate and move to the left . I was thinking to reference the local axis of the character to the world axis && press A,D than rotate 180 degrees, else - stay your current direction . I dont know if its the way to do it, and I dont know how to code it. Im new to c#/ Thank you
Answer by killer-zog · Nov 12, 2015 at 11:13 PM
just wrote a quick demo for you. Basically it takes two keyboard input: 1. when user press A and B it uses transform.Rotate
method, while passes a vector as it's rotation direction and speed.
using UnityEngine;
using System.Collections;
public class rotate : MonoBehaviour {
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.A))
{
rotateMove(-1);
}
else if (Input.GetKey(KeyCode.D))
{
rotateMove(1);
}
}
private void rotateMove(int dir)
{
gameObject.transform.Rotate(0, dir, 0);
}
}
Answer by Asafl14 · Nov 15, 2015 at 10:00 AM
Thanks a lot, realy appreciate your answer, I come from the world of 3d, not scripting, if you need anything - help with modeling ,texturing...whatever I can help, just ask.
well ins$$anonymous$$d of a new answer you could just use Add Comment
link under my answer. But anyway, glad to help. I am also from $$anonymous$$aya, didn't had any scripting knowledge whatsoever just learned couple months ago, now Unity seems a lot EASIER!
If it did help you might wanna accept my answer as accepted answer. :)
Your answer
Follow this Question
Related Questions
Make a gameobject with a character controller inivisble 2 Answers
how can I make a character fly when holding down the jump button? 1 Answer
Character strafing problem (javascript) 0 Answers
Character Movement not moving how i want 2 Answers
Moving along relative z axis with character controller? 1 Answer