Search Unity

Basic Rigidbody Rotation constraint not working*SOLVED

Discussion in '2D' started by knobblez, Feb 22, 2019.

  1. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223
    **SOLUTION**

    transform.rotation = Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, 0f);

    --------------------------------------------------------------------------------------


    I have a Rigidbody on my Main Camera. I checked the Z axis constraint and double-checked that Z is the axis I want to restrict.



    Here is the script I have on my character:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     public float LookTurnSpeed;
    9.     public float turnSpeed;
    10.     private Rigidbody rb;
    11.  
    12.     void Start()
    13.     {
    14.         rb = GetComponent<Rigidbody>();
    15.     }
    16.  
    17.     void FixedUpdate()
    18.     {
    19.         float moveVertical = Input.GetAxis("Vertical");
    20.  
    21.         Vector3 movement = new Vector3(0f, 0f, moveVertical);
    22.  
    23.         rb.AddForce(movement * speed);
    24.  
    25.         Cursor.visible = false;
    26.         var ym = Input.GetAxis("Mouse X") * Time.deltaTime * LookTurnSpeed;
    27.         var xm = Input.GetAxis("Mouse Y") * Time.deltaTime * -LookTurnSpeed;
    28.  
    29.         transform.Rotate(xm, ym, 0f);
    30.     }
    31. }
    32.  

    When I move my mouse the Z axis still moves. All I have in my scene is Terrain, Water, Main Camera and Directional Light.
     
    Last edited: Feb 23, 2019
  2. Geejayz

    Geejayz

    Joined:
    Feb 8, 2016
    Posts:
    73
    Hi,

    Is it that it moves or it rotates?
    I note you have this line of code...
    Code (CSharp):
    1. Vector3 movement = new Vector3(0f, 0f, moveVertical);
    ...and wonder if it should be...

    Code (CSharp):
    1. Vector3 movement = new Vector3(0f, moveVertical, 0f);
     
    Jared_P likes this.
  3. Geejayz

    Geejayz

    Joined:
    Feb 8, 2016
    Posts:
    73
    Sorry, scratch that. Just realised this is 3d and you probably do want to move in the z-axis.
     
  4. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223
    The value of the Z axis Rotation changes even though I have it frozen.

    Basically I'm trying to accomplish moving forward/backward using W/S and turning using the mouse. My camera is getting tilted when I turn, though.