Question by
kaden1023 · Dec 21, 2021 at 03:22 AM ·
movementtransformmovement scripteuleranglestransform.translate
Limit movement to just forward/backward and left/right
I am making a script:
using UnityEngine;
public class player : MonoBehaviour
{
public float moveSpeed;
public float strafeSpeed;
public GameObject playerCam;
public float viewSpeedH = 2.0f;
public float viewSpeedV = 2.0f;
public float viewYaw = 0.0f;
public float viewPitch = 0.0f;
void Start()
{
moveSpeed = 5.0f;
strafeSpeed = 2.5f;
}
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
var forward = playerCam.transform.forward;
var right = playerCam.transform.right;
forward.y = 0f;
right.y = 0f;
forward.Normalize();
right.Normalize();
var desiredMoveDirection = forward * v + right * h;
transform.Translate(desiredMoveDirection * moveSpeed * Time.deltaTime);
}
void Update()
{
viewYaw += viewSpeedH * Input.GetAxis("Mouse X");
viewPitch -= viewSpeedH * Input.GetAxis("Mouse Y");
transform.eulerAngles = new Vector3(viewPitch, viewYaw, 0.0f);
}
}
How would I go about as to make it so the character can only move left/right and forward/backward?
Comment