I used a Youtube tutorial to code a game, but I wish to make my character taller, but doing so makes my main characters camera to start stuttering, I’m not smart when it comes to coding (Barely understand certain concepts) to I’m hoping someone can help explain how to fix it, thank you.
public class PlayerMotor : MonoBehaviour
{
private CharacterController controller;
private Vector3 playerVelocity;
public float speed = 2f;
private bool isGrounded;
public float gravity = -9.8f;
bool lerpCrouch = false;
bool crouching = false;
bool sprinting = false;
float crouchTimer;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
isGrounded = controller.isGrounded;
if (lerpCrouch)
{
crouchTimer += Time.deltaTime;
float p = crouchTimer / 1;
p *= p;
if (crouching)
controller.height = Mathf.Lerp(controller.height, 1, p);
else
controller.height = Mathf.Lerp(controller.height, 2, p);
if (p > 1)
{
lerpCrouch = false;
crouchTimer = 0f;
}
}
}
public void Crouch()
{
crouching = !crouching;
crouchTimer = 0;
lerpCrouch = true;
if (crouching && sprinting)
speed = 1;
if (crouching)
speed = 1;
else
speed = 2;
}
public void Sprint()
{
sprinting = !sprinting;
if (sprinting)
speed = 4;
else
speed = 2;
if (crouching && sprinting)
speed = 1;
}
public void ProcessMove(Vector2 input)
{
Vector3 moveDirection = Vector3.zero;
moveDirection.x = input.x;
moveDirection.z = input.y;
controller.Move(transform.TransformDirection(moveDirection) * speed * Time.deltaTime);
playerVelocity.y += gravity * Time.deltaTime;
if (isGrounded && playerVelocity.y < 0)
playerVelocity.y = -2f;
controller.Move(playerVelocity * Time.deltaTime);
Debug.Log(playerVelocity.y);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerLook : MonoBehaviour
{
public Camera cam;
private float xRotation = 0f;
public float xSensitivity = 65f;
public float ySenitivity = 65f;
public void ProcessLook(Vector2 input)
{
float mouseX = input.x;
float mouseY = input.y;
xRotation -= (mouseY * Time.deltaTime) * ySenitivity;
xRotation = Mathf.Clamp(xRotation, -80f, 80f);
cam.transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
transform.Rotate(Vector3.up * (mouseX * Time.deltaTime) * xSensitivity);
}
}
Bonus, I seem to phase through a wall every time I crouch and uncrouch under it, I asked for help but was never able to fix it, maybe you guys can help, thanks again.
You may be struggling with a weird quirk of a character controller that causes it to jitter when only moving on the Y axis. For example, when moving upwards on a platform or when standing up. I just took a peek into a character controller that I once made that needed a crouch ability. And it seems my solution was to just move the character slightly while standing up.
Here’s the crouching part:
void Croucher()
{
if (Input.GetKeyDown(KeyCode.Space))
headOffset=-headOffset; // Flips between 0.75f and -0.75f
if (headOffset>0 && cc.height<2f) // wanting to stand up?
{
if (Physics.SphereCast(transform.position,0.2f,Vector3.up,out RaycastHit hit,0.9f)) // something above us?
return;
velocity.x+=0.01f; // prevents jitter when standing (don't ask)
}
cc.height=Mathf.Lerp(cc.height,1.25f+headOffset,0.1f);
}
I just got everything running, but just like my previous code, me uncrouching under an object still cause me to phase through it, what can I do to stop it?
Only the character controller’s x,y and z movement is restricted by collisions. It’s height isn’t restricted at all. So when your script increases the controller’s height it has to prevent the controller’s head from penetrating a ceiling or anything above. Check out the example I posted earlier, it uses SphereCast to detect for anything above and prevents the controller from “standing up” if there’s anything present.
Well the height increase happens on line 9 of the lerpCrouch section, so you’ll need to insert a test there. That whole crouch code looks very messy and so I’m not touching it.