This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
[Header("Movement")]
public float MoveSpeed;
public Transform orientation;
float HorizontalInput;
float VerticalInput;
Vector3 MoveDirection;
Rigidbody rb;
private void start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
private void update()
{
MyInput();
}
private void FixedUpdate()
{
MovePlayer();
}
private void MyInput()
{
HorizontalInput = Input.GetAxisRaw("Horizontal");
VerticalInput = Input.GetAxisRaw("Vertical");
}
private void MovePlayer()
{
//Calculate Movement Direction
MoveDirection = orientation.foward * VerticalInput + orientation.right * HorizontalInput;
rb.AddForce(MoveDirection.normalized * MoveSpeed * 10f, ForceMode.Force);
}
}