Camera offset when crouching
Im trying to make my character crouch when i press left shift.
Heres the code for the player movement
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] GameObject cameraHolder;
[SerializeField] GameObject crouchGFX;
[SerializeField] GameObject playerGFX;
[SerializeField] float jumpForce, mouseSensitivity, smoothTime;
float walkSpeed;
const float startWalkSpeed = 5;
float verticalLookRotation;
bool grounded;
bool canJump;
Vector3 smoothMoveVelocity;
Vector3 moveAmount;
Rigidbody rb;
void Awake()
{
walkSpeed = startWalkSpeed;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
canJump = true;
rb = GetComponent<Rigidbody>();
}
void Update()
{
Move();
Jump();
Look();
Crouch();
}
void Move()
{
Vector3 moveDir = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized;
moveAmount = Vector3.SmoothDamp(moveAmount, moveDir * walkSpeed, ref smoothMoveVelocity, smoothTime);
}
void Jump()
{
if(Input.GetKeyDown(KeyCode.Space) && grounded && canJump)
{
rb.AddForce(transform.up * jumpForce);
}
}
void Look()
{
transform.Rotate(Vector3.up * Input.GetAxisRaw("Mouse X") * mouseSensitivity);
verticalLookRotation += Input.GetAxisRaw("Mouse Y") * mouseSensitivity;
verticalLookRotation = Mathf.Clamp(verticalLookRotation, -90f, 90f);
cameraHolder.transform.localEulerAngles = Vector3.left * verticalLookRotation;
}
void Crouch()
{
if(Input.GetKey(KeyCode.LeftShift))
{
playerGFX.SetActive(false);
crouchGFX.SetActive(true);
canJump = false;
walkSpeed = startWalkSpeed/2;
}
else
{
playerGFX.SetActive(true);
crouchGFX.SetActive(false);
canJump = true;
walkSpeed = startWalkSpeed;
}
}
void FixedUpdate()
{
rb.MovePosition(rb.position + transform.TransformDirection(moveAmount) * Time.fixedDeltaTime);
}
public void SetGroundedState(bool _grounded)
{
grounded = _grounded;
}
}
i cant seem to figure out how to get my camera to move along with the crouching and how to make it smooth
my regular character model is a capsule with scale 1, 1, 1 my crouched character model is a capsule with scale 1, 0.8, 1 with its y pos at -0.2 off the reuglar character model
how do i do this?
Comment