- Home /
Player Movement in unity3d
Hi,
I have been working on a first person 3d player movement system on a off for 3ish months now. I still feel relatively unsatisfied with my result. Currently, I am using rb.velocity and setting it to a movement vector. I have implemented friction to stop the player, per-axis acceleration, and tanh curve acceleration. Still the result I'm getting feels "cheap" and unsatisfying. I might be doing this the entirely wrong way but I chose rigidbody movement because of its freedom.
I know I'm asking a lot, but please please help and if you need it here is my very messy coed at its current state: https://pastebin.com/i6y977Ns.
Answer by UnityPlum · Dec 13, 2021 at 04:41 PM
Definitely use CharacterController. It is so beautiful, simple and customizable. I highly recommend you do some research and check it out. Here is a simple movement script I made in 5 minutes (And it is perfect):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPController : MonoBehaviour
{
[Header("Camera")]
public Camera camera;
public float sensitivity;
public bool invertY;
[Header("Movement")]
public float moveSpeed;
public float gravity;
public float yMovement;
public float jumpSpeed;
public CharacterController cc;
private float cameraRotation;
private float playerRotation;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
HandleRotation();
HandleMovement();
}
void HandleMovement()
{
float forward = Input.GetAxisRaw("Vertical");
float right = Input.GetAxisRaw("Horizontal");
yMovement -= gravity * Time.deltaTime;
bool isGrounded = cc.isGrounded;
if(isGrounded) yMovement = 0;
if(Input.GetKey(KeyCode.Space) && isGrounded)
{
yMovement = jumpSpeed;
}
Vector3 motion = new Vector3(0, yMovement, 0);
motion += transform.forward * forward;
motion += transform.right * right;
motion *= moveSpeed * Time.deltaTime;
cc.Move(motion);
}
void HandleRotation()
{
float xMouse = Input.GetAxisRaw("Mouse X");
float yMouse = Input.GetAxisRaw("Mouse Y");
yMouse *= invertY ? -1: 1;
cameraRotation += yMouse * sensitivity * Time.deltaTime;
playerRotation += xMouse * sensitivity * Time.deltaTime;
camera.transform.localEulerAngles = new Vector3(cameraRotation, 0, 0);
transform.localEulerAngles = new Vector3(0, playerRotation, 0);
}
}