- Home /
Can stand up beneath a collider
In my script the character can crouch and stand up, but he can also stand up if he's beneath a collider. I want to make disable standup when there is a collider in the field.
Var crouchSpeed : float = 3;
static var crouched : boolean = false;
static var moving : boolean = false;
private var Speed : float = 1;
private var chMotor: CharacterMotor;
private var ch: CharacterController;
private var tr: Transform;
private var height: float;
function Start(){
chMotor = GetComponent(CharacterMotor);
tr = transform;
ch = GetComponent(CharacterController);
height = ch.height
}
function Update(){
var h = height;
var speed = Speed;
if (Input.GetButton("Jump")){
crouched = false;
}
if (Input.GetKeyDown(KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl)){
crouched = !crouched;
}
if (crouched){
h = 0.5 * height;
speed = crchSpeed;
}
chMotor.movement.maxForwardSSpeed = speed;
chMotor.movement.maxSidewaysSpeed = speed -0.5;
chMotor.movement.maxBackwardsSpeed = speed -1;
var lastHeight = ch.height;
ch.height = Mathf.Lerp(ch.height, h, 5*Time.deltaTime);
tr.position.y += (ch.height-lastHeight)/2
}
Answer by CeejayZSmith · Mar 16, 2013 at 06:57 PM
Here is your code i edited its untested but i think it should work. For a detailed explanation just ask :) to edit how much room you need change the needStandRoom variable.
Var crouchSpeed : float = 3;
static var crouched : boolean = false;
static var moving : boolean = false;
private var Speed : float = 1;
private var chMotor: CharacterMotor;
private var ch: CharacterController;
private var tr: Transform;
private var height: float;
var distFromUp : float;
var canStand : boolean = false;
var needStandRoom : float = 3;
function Start(){
chMotor = GetComponent(CharacterMotor);
tr = transform;
ch = GetComponent(CharacterController);
height = ch.height
}
function Update(){
var h = height;
var speed = Speed;
if (Input.GetButton("Jump") && canStand){
crouched = false;
}
if (Input.GetKeyDown(KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl)){
crouched = !crouched;
}
if (crouched){
h = 0.5 * height;
speed = crchSpeed;
var hit : RaycastHit;
if(Physics.Raycast(transform.position,Vector3.up,hit))
{
if(hit.collider.gameObject)
{
distFromUp = Vector3.Distance(transform.position,hit.point);
if(distFromUp < 3)
{
canStand = true;
}else{
canStand = false;
}
}else{
canStand = true;
}
}
}
chMotor.movement.maxForwardSSpeed = speed;
chMotor.movement.maxSidewaysSpeed = speed -0.5;
chMotor.movement.maxBackwardsSpeed = speed -1;
var lastHeight = ch.height;
ch.height = Mathf.Lerp(ch.height, h, 5*Time.deltaTime);
tr.position.y += (ch.height-lastHeight)/2
}
Your answer
Follow this Question
Related Questions
How having correct bounce on a rotating pinwheel(it's more complicated)? 0 Answers
Detecting what surface character is moving on and restrict movement outside 1 Answer
Wacky helicopter Please Help! 0 Answers
Cannot move FPSController after respawning at a spawn point. 1 Answer
How to change box collider size without falling through the ground 1 Answer