Artificial Intelligence
Please fill the required field.

unity 3d c# spin object horizontally


I have a object with a spin script that rotates it around it's y axis on hold and release of left arrow key. What I want is to make it spin horizontally on hold and release of right arrow key. To achieve this, you can modify your existing spin script to detect the right arrow key press and rotate the object around its z or x axis instead of the y axis. Here's an example of the modified spin script: using UnityEngine; public class SpinObject : MonoBehaviour { [SerializeField] private float rotationSpeed = 100f; //adjust this to change the speed of rotation private bool isRotating = false; private string rotationInput = "Horizontal"; void Update() { //detect left or right arrow key press if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow)) { //set isRotating to true isRotating = true; //change the rotation direction based on which arrow key is pressed if (Input.GetKeyDown(KeyCode.LeftArrow)) { rotationInput = "Horizontal"; //rotate around z or x axis } else if (Input.GetKeyDown(KeyCode.RightArrow)) { rotationInput = "Vertical"; //rotate around y axis (default) } } //detect when the arrow key is released if (isRotating && Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.RightArrow)) { //set isRotating to false isRotating = false; } } void FixedUpdate() { //rotate the object continuously while the arrow key is held down if (isRotating) { //get the value of the arrow key (left or right) to determine the direction of rotation float rotationInputValue = Input.GetAxis(rotationInput); //rotate the object around the chosen axis transform.Rotate(new Vector3(0, 0, 1), rotationInputValue * rotationSpeed * Time.deltaTime); } } } You can adjust the rotation speed and axis of rotation in the inspector, or you can modify the script to accept user input for those values.