- Home /
Other
Player keeps going through the floor when crouching.
Hi all, I got an next problem with JS script: Why my player keeps going through the floor if I crouch? Can't understand what I'm doing wrong or how to prevent this from happening, floor has mesh collider and my player is the basic FPS Controller with character controller and rigidbody.
Here's the movement.js script which includes the code for crouching.
var mainCam : GameObject;
var animObject : GameObject;
var fallAnimGO : GameObject;
var moveSpeed : int = 2;
var crouchSpeed : int = 1;
var runSpeed : int = 5;
var jumpSpeed : int = 10;
var gravity : int = 20;
var maxEnergy : float = 10;
var energyRegenRate : float = 1;
var Smooth : float = 5;
var grounded : boolean = false;
var running : boolean = false;
var energy : float;
private var originalSpeed : int;
private var hitCeiling : boolean = false;
public var moveDirection = Vector3.zero;
private var controller : CharacterController;
controller = GetComponent(CharacterController);
private var IntialPosition : Vector3;
private var InitialHeight : float;
private var CrouchingHeight : float;
private var InitialCenter : Vector3;
private var CrouchingCenter : Vector3;
private var defCamPos : float = 0;
private var bobSpeed : float;
private var bobAmount : float;
private var timer = 0.0;
private var camPos : float;
private var crouching = false;
function Start()
{
originalSpeed = moveSpeed;
energy = maxEnergy;
defCamPos = mainCam.transform.localPosition.y;
camPos = defCamPos;
InitialHeight = controller.height;
CrouchingHeight = InitialHeight - .5f;
InitialCenter = controller.center;
CrouchingCenter = InitialCenter + Vector3.down * 0.1f;
isCrouching = false;
}
function Awake()
{
animObject.animation.wrapMode = WrapMode.Loop;
}
function Update()
{
if(controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= moveSpeed;
if(Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
if(controller.velocity.magnitude > (moveSpeed - 1) && controller.velocity.magnitude < (runSpeed - 1))
{
animObject.animation.CrossFade("Walk");
bobSpeed = 0.25;
bobAmount = 0.005;
}
else if(controller.velocity.magnitude > (runSpeed - 1))
{
animObject.animation.CrossFade("Run");
bobSpeed = 0.29;
bobAmount = 0.01;
}
else
{
animObject.animation.CrossFade("Idle");
bobSpeed = 0;
bobAmount = 0;
}
if(Input.GetKey(KeyCode.LeftShift) && (Input.GetAxis("Horizontal") || Input.GetAxis("Vertical")))
{
if(energy > 0)
{
if(!crouching)
{
Run();
}
else
{
moveSpeed = crouchSpeed;
}
}
else
{
running = false;
if(!crouching)
{
moveSpeed = originalSpeed;
}
else
{
moveSpeed = crouchSpeed;
}
}
}
else
{
running = false;
if(energy < maxEnergy)
{
energy += energyRegenRate * Time.deltaTime;
}
if(!crouching)
{
moveSpeed = originalSpeed;
}
else
{
moveSpeed = crouchSpeed;
}
}
var camHeight : Vector3 = new Vector3(mainCam.transform.localPosition.x, camPos, mainCam.transform.localPosition.z);
var smoothHeight = Vector3.Lerp(mainCam.transform.localPosition, camHeight, Time.deltaTime * Smooth);
mainCam.transform.localPosition = smoothHeight;
if(Input.GetKeyDown("c"))
{
if(!crouching)
{
controller.height = CrouchingHeight;
controller.center = CrouchingCenter;
camPos = defCamPos - .5f;
crouching = true;
}
else
{
controller.height = InitialHeight;
controller.center = InitialCenter;
camPos = defCamPos;
crouching = false;
}
}
waveslice = 0.0;
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
if(Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0) {
timer = 0.0;
}
else
{
waveslice = Mathf.Sin(timer);
timer = timer + bobSpeed;
if(timer > Mathf.PI * 2)
{
timer = timer - (Mathf.PI * 2);
}
}
if(waveslice != 0)
{
translateChange = waveslice * bobAmount;
totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
totalAxes = Mathf.Clamp (totalAxes, 0.0, 1.0);
translateChange = totalAxes * translateChange;
mainCam.transform.localPosition.y = smoothHeight.y + translateChange;
}
else
{
mainCam.transform.localPosition.y = smoothHeight.y;
}
}
else
{
animObject.animation.CrossFade("Idle");
bobSpeed = 0;
bobAmount = 0;
}
moveDirection.y -= gravity * Time.deltaTime;
if(hitCeiling)
{
moveDirection.y -= gravity * Time.deltaTime;
}
var flags = controller.Move(moveDirection * Time.deltaTime);
hitCeiling = ((flags & CollisionFlags.CollidedAbove) != 0);
if(energy > maxEnergy)
{
energy = maxEnergy;
}
}
function Run()
{
moveSpeed = runSpeed;
energy -= energyRegenRate * Time.deltaTime;
running = true;
}
function Jump()
{
moveDirection.y = jumpSpeed;
fallAnimGO.animation.CrossFadeQueued("Jump", 0.1, QueueMode.PlayNow);
}
function OnControllerColliderHit(Hit : ControllerColliderHit)
{
if(!controller.isGrounded && controller.velocity.y < -6)
{
fallAnimGO.animation.CrossFadeQueued("Fall", 0.3, QueueMode.PlayNow);
}
}
Click your character controller and observe the collider when you are crouching.
So now I know it's happening when character should come back up, in other words, when player presses c it goes on crouch, after player presses c again, it drops through the floor as it comes to standing mode again, but still don't know how to fix this?
As I found an fix for this.
It was the rigidbody on fps controller that caused this to happen, I took it off and now it works as it should. But why it doesnt work with rigidbody on? As now I can't anymore push objects.. :(
CC and rigidbody don't work very well together.
There are ways to make it work though, like using Root $$anonymous$$otion animation with a $$anonymous$$inematic rigidbody works with a CC I believe.
Follow this Question
Related Questions
Basic 2d movement problem: glitching in terrain 0 Answers
WoW Movement and Freeze Cursor Location? 1 Answer
Player movement 0 Answers
Best Way to make a character move 1 Answer
How to make a car not go off the screen? 2 Answers