character controller code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThridPersonMovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public float speed = 6f;
public float gravity = -10.00f;
public float turnSmoothTime = 0.1f;
public float turnSmoothVelocity = 1f;
public float jumpHeight = 2f;
public Transform GroundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(GroundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float Vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(0f, 0f, Vertical).normalized;
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;controller.Move(velocity * Time.deltaTime);
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan(direction.x) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
}
}
camera controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamController : MonoBehaviour
{
private Transform _xForm_Camera;
private Transform _xForm_Parent;
private Vector3 _LocalRotation;
public float MouseSensitivity = 4f;
public float OrbitDampening = 10f;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity = 1f;
public bool CameraDisabled = false;
// Start is called before the first frame update
void Start()
{
if (_xForm_Parent)
{
float dist = Vector3.Distance(this._xForm_Parent.position, transform.position);
print("Distance to _xForm_Parent: " + dist);
}
Cursor.lockState = CursorLockMode.Locked;
this._xForm_Camera = this.transform;
this._xForm_Parent = this.transform.parent;
}
// Update is called once per frame, ater Update() on every game object in the scene
void LateUpdate()
{
if (Input.GetKeyDown("c"))
{
CameraDisabled = !CameraDisabled;
}
if (!CameraDisabled)
{
//Rotation of the Camera based one Mouse Coordinates
if (Input.GetAxis("Mouse X") != 0)
{
_LocalRotation.x += Input.GetAxis("Mouse X") * MouseSensitivity;
}
}
Quaternion QT = Quaternion.Euler(0, _LocalRotation.x, 0);
this._xForm_Parent.rotation = Quaternion.Lerp(this._xForm_Parent.rotation, QT, Time.deltaTime * OrbitDampening);
}
}