- Home /
force player to crouch when under an object with collisio
Ok so i have a crouching script to which i followed from a tutorial. But what i want to do is that when the player is crouching, if they try to uncrouch and there is an object with collision on them top of them theyre forced to stay crouched.. if you get what i mean.. any help would be greatly apreciated.
private var crouchHeight : float;
private var standardHeight : float;
private var crouching : boolean = false;
private var controller : CharacterController;
private var mainCamera : GameObject;
var GameObj : GameObject;
function Start () {
controller = GetComponent(CharacterController);
mainCamera = gameObject.FindWithTag("MainCamera");
standardHeight = controller.height;
crouchHeight = controller.height/2;
crouching = false;
}
function Update () {
if (Input.GetButtonDown ("Crouch")){
if(crouching){
controller.height = standardHeight ;
controller.center = Vector3 (0, 0, 0);
mainCamera.transform.localPosition.y += crouchHeight;
crouching = false;
return;
}
if(!crouching)
crouch();
}
}
function crouch() {
controller.height = crouchHeight;
controller.center = Vector3 (0, -0.5, 0);
mainCamera.transform.localPosition.y -= crouchHeight;
crouching = true;
}
Answer by jorjdboss · Jan 06, 2014 at 10:44 AM
This is just to add the sparkzbarca's answer but being more specific with implementation. This is untested code so please edit this answer in case of any corrections.
function Update ()
{
if (Input.GetButtonDown ("Crouch"))
{
if(crouching)
{
// Assuming that the pivot of the character is always at the center, raycasting from the edge of the top of your controller upwards till max height
// This ensures that the character can only stand up when there is more clearance than the standardheight
var startPos = transform.position + new Vector3( 0, crouchHeight - (standardHeight *0.5f), 0);
var length = (standardHeight - crouchHeight);
// Because you raycast from inside the capsule collider, it wont detect the character's collider
if( !Physics.Raycast( startPos , Vector3.up, length) )
unCrouch();
}
else
{
crouch();
}
}
}
function unCrouch()
{
controller.height = standardHeight ;
controller.center = Vector3 (0, 0, 0);
mainCamera.transform.localPosition.y += crouchHeight;
crouching = false;
}
function crouch()
{
controller.height = crouchHeight;
controller.center = Vector3 (0, -0.5, 0);
mainCamera.transform.localPosition.y -= crouchHeight;
crouching = true;
}
Answer by sparkzbarca · Jan 01, 2014 at 09:34 AM
if(input.getbutton("tries to uncrouch button"))
{
//raycast up
distance = vector3.distance (player.transform, raycasthit.point);
if distance is really small
dont actually call the stand script otherwise do
}
alternatly
on collsion enter
{
if crouched
bool UncrouchAllowed = false;
}
if(input.getbuttondown("button to uncrouch") && UncrouchAllowed)
//uncrouch
take your pick
the first one to me seems more robust, just check to see if there is something above you, the thing is you shuold just know how far down crouching moves your player
so lets say your transform was at 1 and you crouch and its Y goes to .5
well you need to account for the body and the head so your model is 2 which makes sense for having its center Y at 1
so that means if your model needs the contact point to be at least 1.5 higher than the transform so you cast up if there is nothing in 1.5 meters you allow themt o stand
you can actually short circuit the distance check i just realized.
just do
clearanceHeight = 1.5f;
if(physics.linecast(player.transform.start, player.transform.start + player.transform.up * clearanceHeight)
right away you know if you hit anything at all then you cant stand.
you dont even need to store what you hit or anything, just cast and if you hit you go ok dont if you didnt ok stand.
wait a $$anonymous$$ute are you saying you dont know how to raycast or what?
I mean im not generally willing to literally write the code for you, it's just not helpful. If I do that you'd just be back in a $$anonymous$$ute wanting me to write the next bit of code, if you can't code you need to practice and learn. This is an easy bit to script and I wrote the pseduo code out for you.
I wrote an implementation in code. if you can't figure it out, first look at the script reference for Physics.Raycast().
Inside update try not using return like the way you used, because if you put some code that's not related to crouching after this block, it will be skipped when crouch key is pressed.