Search Unity

Question rotating an object on x axis while moving on z axis

Discussion in 'Navigation' started by jimbrts, Feb 26, 2022.

  1. jimbrts

    jimbrts

    Joined:
    Feb 20, 2022
    Posts:
    2
    Hello!

    I've read some posts here on how to rotate an object on z, but i can't seem to get it to work with the tutorial i'm walking through. Here's my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     public float speed = 4;
    8.     public float speed2 = -4f;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         // sets initial position of player
    13.         transform.position = new Vector3(0, 0, 0);
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         float horizontalInput = Input.GetAxis("Horizontal");
    20.         float verticalInput = Input.GetAxis("Vertical");
    21.         float zLeft = Input.GetAxis("zLeft");
    22.         float zRight = Input.GetAxis("zRight");
    23.  
    24.         // want to roll object slightly on z axis to simulate a ship banking.
    25.         transform.Translate(new Vector3(horizontalInput, verticalInput, zLeft) * speed * Time.deltaTime);
    26.         transform.Translate(new Vector3(0, 0, zRight) * speed2 * Time.deltaTime);
    27.  
    28.        
    29.  
    30.  
    31.  
    32.  
    33.  
    34.     }
    35. }
    36.  

    My goals are as follows - 1) rotate the object on the x axis whenever zLeft and zRight are in use, and when i release the key level out to its original rotation. 2) I'm suspecting there is an easier way to accomplish my last line of code with out defining a negative variable.
     
  2. jimbrts

    jimbrts

    Joined:
    Feb 20, 2022
    Posts:
    2
    I figured this out! nevermind.