- Home /
Question by
HashinBrownsR · Nov 03, 2013 at 07:02 PM ·
crouch
How to stop head from going through stuff?
So I have a crouch script but for some reason whenever I go under something and then stand up my head goes through the object I am supposed to be standing under. Any help here would be amazing, also I am not using a rigidbody controller so anything that deals with that is probably out of the question. Thanks in advance, please help!
var crouchSpeed : float = 3;
var charController : CharacterController;
var Player : Transform;
private var charHeight : float;
private var pos : Vector3;
function Start (){
Player = transform;
charController = GetComponent(CharacterController);
charHeight = charController.height;
}
function Update (){
var h : float= charHeight;
if (Input.GetKey(KeyCode.C))
{
h = 1;//charHeight*.05f;
}
var lastHeight : float= charController.height;
charController.height = Mathf.Lerp(charController.height, h, Time.deltaTime * 5);
pos.x = Player.position.x;
pos.z = Player.position.z;
pos.y = Player.position.y + (charController.height - lastHeight) / 2;
Player.position = pos;
}
Comment
Answer by Zaeran · Nov 03, 2013 at 08:36 PM
Raycast upwards when you go to un-crouch. If there's something there, stay down.