This is a 3-d first person game using WASD and the player is using a rigid-body. When you move the mouse the camera moves with it accordingly. When you go forward it goes forward according to the camera direction which can be a problem. For example if you look down and go backwards you go up essentially flying which is not ideal. I’m not sure whether this is an issue with the mass, drag, or any other physics/gravity component or if I should code my movement/came differently. Since this is a horror game the movement really should not be that complicated. I just need the movement to be based on the y axis, but since I used this code in a different game it is based off of the x and y axis and I need to convert it. If you have any questions just ask I’ll try to respond as fast as possible. Here is my existing code:
Player Cam Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCam : MonoBehaviour
{
public float sensX;
public float sensY;
public Transform orientation;
float xRotation;
float yRotation;
private void Start()
{
// makes cursor locked and invisible
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void Update()
{
// get mouse input
float mouseX = Input.GetAxisRaw("Mouse X") * Time.deltaTime * sensX;
float mouseY = Input.GetAxisRaw("Mouse Y") * Time.deltaTime * sensY;
yRotation += mouseX;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
// rotate cam and orientation
transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
orientation.rotation = Quaternion.Euler(xRotation, yRotation, 0);
}
}
Player Movement Script
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.Animations;
using UnityEngine.Assertions.Must;
public class PlayerMovement : MonoBehaviour
{
[Header("Movement")]
public float moveSpeed;
public float sprintSpeed;
public float originalMoveSpeed;
public float drag;
[Header("Key-binds")]
public KeyCode sprintKey = KeyCode.LeftShift;
public Transform orientation;
float horizontalInput;
float verticalInput;
Vector3 moveDirection;
Rigidbody rb;
private void Update()
{
// handles jumping and vertical & horizontal input
MyInput();
//Controls Sprinting
SprintControl();
// handle drag
Drag();
// handles speed cap
SpeedControl();
}
private void FixedUpdate()
{
MovePlayer();
}
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
}
private void MyInput()
{
horizontalInput = Input.GetAxisRaw("Horizontal");
verticalInput = Input.GetAxisRaw("Vertical");
}
private void MovePlayer()
{
// calculate move direction
moveDirection = orientation.forward * verticalInput + orientation.right * horizontalInput;
rb.AddForce(moveDirection.normalized * moveSpeed * 10f, ForceMode.Force);
}
private void SpeedControl()
{
Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
// limit velocity if needed
if (flatVel.magnitude > moveSpeed)
{
Vector3 limitedVel = flatVel.normalized * moveSpeed;
rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
}
}
private void SprintControl()
{
if (Input.GetKeyDown(sprintKey))
{
moveSpeed = sprintSpeed;
}
if (Input.GetKeyUp(sprintKey))
{
// resets moveSpeed to original moveSpeed from sprintSpeed
moveSpeed = originalMoveSpeed;
}
}
private void Drag()
{
rb.drag = drag;
}
}