Question by
thepizzadude · Mar 28, 2017 at 06:48 PM ·
c#booleanfps controllercrouchcrouching
[C#] playerHeight not working for FPS Controller
Hey, I've got this script running for my FPS controller:
using UnityEngine;
using System.Collections;
public class SimpleController : MonoBehaviour
{
public float speed = 2f;
public float sprintMult = 2f;
public float sensitivity = 2f;
CharacterController player;
public GameObject eyes;
float moveFB;
float moveLR;
float rotX;
float rotY;
float vertVelocity;
public float jumpDist = 5f;
public int jumpTimes;
public float playerHeight = 2f;
private bool isCrouching;
private bool canJump;
// Use this for initialization
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
player = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("escape")) Cursor.lockState = CursorLockMode.None;
moveFB = Input.GetAxis("Vertical") * speed;
moveLR = Input.GetAxis("Horizontal") * speed;
rotX = Input.GetAxis("Mouse X") * sensitivity;
rotY -= Input.GetAxis("Mouse Y") * sensitivity;
rotY = Mathf.Clamp(rotY, -90f, 90f);
Vector3 movement = new Vector3(moveLR, vertVelocity, moveFB);
transform.Rotate(0, rotX, 0);
eyes.transform.localRotation = Quaternion.Euler(rotY, 0, 0);
//eyes.transform.Rotate (-rotY, 0, 0);
movement = transform.rotation * movement;
player.Move(movement * Time.deltaTime);
if (player.isGrounded == true)
{
canJump = true;
jumpTimes = 0;
}
if (jumpTimes < 1)
{
if (Input.GetButtonDown("Jump"))
{
vertVelocity += jumpDist;
jumpTimes += 1;
canJump = false;
}
}
if (Input.GetButtonDown("Crouch"))
{
if (!isCrouching && canJump)
{
speed = 2f;
player.height = 1f;
isCrouching = true;
}
}
if (Input.GetButtonUp("Crouch"))
{
if (isCrouching && canJump)
{
speed = 5f;
player.height = 2f;
isCrouching = false;
}
}
if (Input.GetButtonDown("Sprint"))
{
speed *= sprintMult;
}
if (Input.GetButtonUp("Sprint"))
{
speed /= sprintMult;
}
if (!canJump && !isCrouching)
{
vertVelocity += Physics.gravity.y * Time.deltaTime;
}
else
{
vertVelocity = 0f;
}
}
}
What happens is that when I press crouch the player's speed decreases, the boolean isCrouching turns true but his height continues the same. What I found out is that if I swap !canJump with !player.isGrounded in the last if, it works: I can crouch perfectly. The problem there is that the jumping only works sometimes and I have to spam the spacebar in order to jump.
So what's going on here? Doesn't make any sense to me.
Comment
Answer by BloodRayne89 · Apr 12, 2017 at 05:00 PM
Hey, just got messing with your script and I think I might found a solution. Why don't you translate the eyes transform? You can change how high or low the eyes would be.
if (Input.GetButtonDown("Crouch"))
{
if (!isCrouching && canJump)
{
speed = 2f;
player.height = 1f;
isCrouching = true;
eyes.transform.Translate(0, -0.4F, 0);
}
}
if (Input.GetButtonUp("Crouch"))
{
if (isCrouching && canJump)
{
speed = 5f;
player.height = 2f;
isCrouching = false;
eyes.transform.Translate(0, +0.4F, 0);
}
}