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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float movementSpeed = 15;
public float rotationSpeed = 4;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + transform.forward * movementSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
float turn = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
rb.MoveRotation(rb.rotation * turnRotation);
}
}
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:
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.