Search Unity

Can not move and rotate at the same time

Discussion in 'Scripting' started by spynterr, Aug 26, 2019.

  1. spynterr

    spynterr

    Joined:
    Jan 26, 2019
    Posts:
    2
    Hello, I have a code to move and rotate my object but when I move it, I can't rotate it. It just rotates when Vertical Input value equals to 0.
    It should has a very simple solution but i couldn't figure it out. Also, I need to use moveRotation and movePosition, transform.Translate or etc doesn't fit with my game.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float movementSpeed = 15;
    8.     public float rotationSpeed = 4;
    9.     private Rigidbody rb;
    10.  
    11.     void Start()
    12.     {
    13.         rb = GetComponent<Rigidbody>();
    14.     }
    15.  
    16.     private void FixedUpdate()
    17.     {
    18.  
    19.         rb.MovePosition(rb.position + transform.forward * movementSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
    20.  
    21.         float turn = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;
    22.         Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
    23.         rb.MoveRotation(rb.rotation * turnRotation);
    24.  
    25.  
    26.     }
    27. }
    28.  
    [/ICODE]
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Hi and welcome!
    First and foremost, you should use FixedUpdate() only for physics (Rigidbody) related code, so the Inputs should be caught and saved in Update(). That's not causing the problem here tho. I got it to work with the following code:
    Code (CSharp):
    1.     public float movementSpeed = 15;
    2.     public Vector3 rotationSpeed = new Vector3(0,40,0);
    3.     private Rigidbody rb;
    4.     private Vector2 inputDirection;
    5.  
    6.     void Start()
    7.     {
    8.         rb = GetComponent<Rigidbody>();
    9.     }
    10.  
    11.     private void Update()
    12.     {
    13.         Vector2 inputs = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    14.         inputDirection = inputs.normalized;
    15.     }
    16.  
    17.     private void FixedUpdate()
    18.     {
    19.         Quaternion deltaRotation = Quaternion.Euler(inputDirection.x * rotationSpeed * Time.deltaTime);
    20.         rb.MoveRotation(rb.rotation * deltaRotation);
    21.         rb.MovePosition(rb.position + transform.forward * movementSpeed * inputDirection.y * Time.deltaTime);
    22.     }
    Hope that helps. Also, your rotation speed was insanely low, so i bumped it up a bit.
     
  3. spynterr

    spynterr

    Joined:
    Jan 26, 2019
    Posts:
    2
    Thanks! It helped me a lot.
     
  4. FiveFans

    FiveFans

    Joined:
    Oct 21, 2015
    Posts:
    10
    Just a quick reminder that for using delta time in FixedUpdate you should use:

    Code (CSharp):
    1. Time.fixedDeltaTime
     
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Actually, that does not matter, as Time.deltaTime will automatically return the correct value depending on whether it's used from within Update or FixedUpdate.
    https://docs.unity3d.com/ScriptReference/Time-fixedDeltaTime.html
     
  6. FiveFans

    FiveFans

    Joined:
    Oct 21, 2015
    Posts:
    10
    Super. Thanks for letting me. Didn't know that. :)
     
  7. elite051

    elite051

    Joined:
    Sep 11, 2013
    Posts:
    1
    This has helped me a lot with smooth rigidbody movement. My only issue is that the object in question will continue moving briefly after releasing the input. Am I doing something wrong?

    edit: The issue was with the smoothing of the input returned by Input.getAxis. Resolved by changing Input.getAxis to Input.getAxisRaw.
     
    Last edited: Mar 17, 2021