I’ve been having a lot of trouble with this recently. I know it sounds very simple but any help would be really nice. I am trying to move a game object in the direction it is facing using w. It should also turn left and right with the A key and D key. This all needs to be compatible with rigidbody. Thank you.
This script does what you want:
using UnityEngine;
public class RigidbodyController : MonoBehaviour
{
public float movementSpeed = 1;
public float rotationSpeed = 10;
Rigidbody rigidbodySelf;
void Start()
{
rigidbodySelf = GetComponent<Rigidbody>();
rigidbodySelf.constraints = RigidbodyConstraints.FreezeRotation;
}
void Update()
{
float move = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime * 100;
float rotation = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime * 100;
rigidbodySelf.MovePosition(transform.position + move * transform.forward);
rigidbodySelf.MoveRotation(transform.rotation * Quaternion.Euler(0, rotation, 0));
}
}