I have 2 scripts for my character the first one is for my capsule and the second one is for my camera.
I tried it but it won’t work as intended it just glitches and doesn’t really feel smooth, any improvements?
using System;
using UnityEngine;
public class PlayerScript : MonoBehaviour
{
public float mouseSensitivity = 100.0f;
public float clampAngle = 80.0f;
private float rotY = 0.0f;
private float rotX = 0.0f;
private float walkSpeed = 1.5f;
private float runMult = 1f;
private CapsuleCollider collideCollider;
void Start ()
{
Vector3 rot = transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
collideCollider = GetComponent<CapsuleCollider>();
}
void Update ()
{
float mouseX = Input.GetAxis("Mouse X");
rotY += mouseX * mouseSensitivity * Time.deltaTime;
Quaternion localRotation = Quaternion.Euler(0.0f, rotY, 0.0f);
transform.rotation = localRotation;
bool isWalk = Input.GetKey(KeyCode.W);
bool isRun = Input.GetKey(KeyCode.LeftShift);
if (isRun) {
runMult = 2;
}
if (isWalk) {
transform.position += transform.forward * Time.deltaTime * walkSpeed * runMult;
}
}
}
The second script is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraScript : MonoBehaviour
{
private float rotX;
public float mouseSensitivity = 100f;
public float clampAngle = 40f;
void Update()
{
float mouseY = -Input.GetAxis("Mouse Y");
rotX += mouseY * mouseSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler(rotX, 0.0f, 0.0f);
transform.rotation = localRotation;
}
}
I would really like some improved solution.