- Home /
Keep current rotation?
Hi there,
I've got this fairly simple script checking how to rotate my crosshair around. Problem is it's only updating the rotation as long as I'm making an input. If I stop moving around the rotation goes back to 0,0,0.
How do I keep the current rotation, whether I'm using the keyboard or controller inputs? Once I stop moving I'd like to keep the same rotation as my latest input.
The script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAim : MonoBehaviour {
private float rotZ;
public bool inputSelection;
void Update()
{
transform.rotation = Quaternion.Euler(0, 0, rotZ);
//Detect if we're using just the keyboard.
if (Input.GetAxis("LeftStickHorizontal") < -0.25f || Input.GetAxis("LeftStickHorizontal") > 0.25f || Input.GetAxis("LeftStickVertical") < -0.15f || Input.GetAxis("LeftStickVertical") > 0.25f)
{
inputSelection = true;
}
//Detect if we're using a Controller.
if (Input.GetAxis("RightStickHorizontal") < -0.25f || Input.GetAxis("RightStickHorizontal") > 0.25f || Input.GetAxis("RightStickVertical") < -0.25f || Input.GetAxis("RightStickVertical") > 0.25f)
{
inputSelection = false;
}
//If using a keyboard:
if (inputSelection)
{
rotZ = Mathf.Atan2(Input.GetAxis("LeftStickVertical"), Input.GetAxis("LeftStickHorizontal")) * Mathf.Rad2Deg;
}
//If using a Controller:
if (!inputSelection)
{
rotZ = Mathf.Atan2(Input.GetAxis("RightStickVertical"), Input.GetAxis("RightStickHorizontal")) * Mathf.Rad2Deg;
}
}
}
Any help is appreciated!
Thanks
Comment
Your answer
Follow this Question
Related Questions
lerp to 0, problem with horizontal value 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Variable continuously add up in Update() 1 Answer
How to Use Key Combinations with the Control-Key? 2 Answers
Rotate object after swipe according to swipe direction 0 Answers