Hello guys i started with unity 2 months ago and i want to make my camera / player movement smoother but i don’t know how and it doesn’t feel that good.
Here is my Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player_Movement : MonoBehaviour
{
[Header("Movement")]
private CharacterController cc;
public Animator animator;
public Transform groundCheck;
public Transform orientation;
public LayerMask groundMask;
Vector2 dir;
Vector3 velocity;
bool isGrounded;
bool isWalking;
public float moveSpeed = 5.5f;
public float gravity = -9.81f;
public float groundDistance = 0.1f;
[Header("Camera Movement")]
public Transform cam;
private Transform playerbody;
public float mouseSensitivity;
private float x,y,xRotation;
void Awake()
{
cc = GetComponent<CharacterController>();
playerbody = this.transform;
}
public void MoveThePlayer(InputAction.CallbackContext ctx)
{
dir = ctx.ReadValue<Vector2>();
if(dir.x >0 || dir.x <0 || dir.y >0 || dir.y <0)
{
isWalking = true;
}
else
{
isWalking = false;
}
}
public void MoveTheCamera(InputAction.CallbackContext ctx)
{
Vector2 camDir = ctx.ReadValue<Vector2>();
x = camDir.x * mouseSensitivity;
y = camDir.y * mouseSensitivity;
}
void Update()
{
if(Hello_World.Instance.paused == false)
{
PlayerMove();
CameraMove();
animator.SetBool("Walking", isWalking);
}
}
void PlayerMove()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y <0)
{
velocity.y = -0.5f;
}
Vector3 moveDir = orientation.right * dir.x + orientation.forward * dir.y;
velocity.y += gravity * Time.deltaTime;
cc.Move(moveDir * moveSpeed * Time.deltaTime);
cc.Move(velocity * Time.deltaTime);
}
void CameraMove()
{
xRotation -= y;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
cam.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerbody.Rotate(Vector3.up * x);
}
}