- Home /
How do I rotate a game object in 45 degree steps per keystroke?
I'd Like to start by first admitting that I am extremely new to any kind of scripting and this is quite literally the first time I have ever used c#. In my scene/script I have an empty game object which is the parent of my camera and thus is the focal point for my camera. Through my script I am able to move this around having my camera follow from a fixed position. After hours of struggling and much referring to the documentation, I was able to make the camera rotate in 45 degree increments around the y-axis of the focal point in both directions at the the touch of a key. However, the issue I'm running into now is that the object continually rotates (quite fast). Ideally I would like the object to only rotate 45 degrees per keystroke. Here is an example of what I have so far:
using UnityEngine;
using System.Collections;
public class cameraController : MonoBehaviour {
public float speed;
public GameObject camera;
private Vector3 currentPos;
private Vector3 currentRotation;
void Update () {
//Keep the camera focused on the target
currentPos = transform.position;
camera.transform.LookAt (currentPos);
//currentRotation = transform.rotation.eulerAngles;
}
void LateUpdate () {
//Horizontal Movement
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0, moveVertical);
transform.position = currentPos + movement * Time.deltaTime * speed;
}
void FixedUpdate () {
//Camera rotation around the focal point
float rotateFloat = Input.GetAxis("RotateY");
int rotateY = Mathf.RoundToInt(rotateFloat);
Quaternion currentRotation = transform.rotation;
if (rotateY > 0) {
transform.rotation = currentRotation * Quaternion.Euler(0, 45.0f, 0);
Debug.Log("1");
}
if (rotateY < 0) {
transform.rotation = currentRotation * Quaternion.Euler(0, -45.0f, 0);
Debug.Log("2");
}
}
}
Answer by mhtraylor · Feb 28, 2014 at 06:31 AM
Use an Input.GetButton
or Input.GetButtonDown
method for this. The Input.GetAxis
is for continuous movement, but the situation you have described is for the movement to take place in discrete steps at the press of a button or key. For instance, per the docs, Input.GetButtonDown
will only return true during the frame that the button was pressed.
The problem with getButtonDown is that it won't allow me to differentiate between the positive and negative version of the button unless I explicitly state which keys exactly. This causes the unfortunate side-effect of making key bindings non-customizable for the end user which is not an acceptable outcome for me.
Answer by NoahConstable · Mar 08, 2014 at 09:14 AM
Just call Input.GetAxisRaw("AxisName"), but add some extra code...
C#:
private bool usingAxis = false;
void Update(){
if( Input.GetAxisRaw("AxisName") != 0){
if(usingAxis == false){
// Do your axis action here
usingAxis = true;
}
}
if( Input.GetAxisRaw("AxisName") == 0){
usingAxis = false;
}
}
This should work (I'll explain why in the comment section). If it does, please mark this answer as correct so others can see it and learn as well, ask their own questions, and grow the Unity community.
Happy Coding!
Noah.
Here is the explanation for the script (by the way, "AxisName" is the name of the axis you want to use)
C#:
//A boolean to see if we are using the axis
private bool usingAxis = false;
//Update is called every frame
void Update(){
//If we get the axis "AxisName" and it is not 0 (or otherwise known as being used)
if( Input.GetAxisRaw("AxisName") != 0){
//Set our boolean to false
if(usingAxis == false){
// Do your axis action here
//Set our boolean to true again
usingAxis = true;
}
}
//If the axis ("AxisName") is equal to zero (or otherwise known as not being used)
if( Input.GetAxisRaw("AxisName") == 0){
//Set the boolean to false
usingAxis = false;
}
}
Any questions you have, please post them. I'll be monitoring this post until someones answer is marked as correct (or you ingore for a couple of days).
Happy Coding!
Noah.
Your answer
Follow this Question
Related Questions
Rotation like in editor 2 Answers
instant rotate an object 3 Answers
How do I rotate a quaternion with euler angles? 0 Answers
Quaternion.Slerp problem... 1 Answer
Rotation with a slider 2 Answers