- Home /
C# Crouch script, player's head lowers rather then picking up feet
Here's my simple crouch script:
float ultScale = myTransform.localScale.y;
Vector3 tmpScale = myTransform.localScale;
Vector3 tmpPosition = myTransform.position;
// Crouch!
if (Input.GetButton ("Crouch")) {
vScale = 0.5f;
}
tmpPosition.y = tmpPosition.y + ( rayDistance * (myTransform.localScale.y - ultScale) );
myTransform.position = tmpPosition;
tmpScale.y = Mathf.Lerp (myTransform.localScale.y, vScale, crouchTime * Time.deltaTime);
myTransform.localScale = tmpScale;
In-game the player is ducking his head rather then picking up his feet. Say my player is 2 meters tall. I can jump 0.5 meters vertically, and crouch down 1 meter. If I'm able to crouch 1 full meter and jump a half meter, I should be able to jump up 1 meter, pick up my legs by pressing crouch, allowing for me to reach the extra 0.5 meters to get up on a block that's 1.5 meters tall.
How could I modify my crouch script to behave like this?
Answer by koray1396 · Mar 18, 2014 at 11:55 AM
Do not use raydistance, I assume pivot point is at the center of myTransform, so as well as scaling, move your object down by 0.25*height of the object. Then you can define a second function for pulling the legs up, then scale and movement the object up by 0.25*height. Ground state would determine which function to use.
Okay so here's what I came up with
// How fast player will crouch
public float crouchTime = 20.0f;
// Define transform
private Transform myTransform;
// Default scale, local scale, and world position
float vScale = 1.0f;
Vector3 tmpScale = myTransform.localScale;
Vector3 tmpPosition = myTransform.position;
// Crouch!
if (Input.GetButton ("Crouch")) {
// If player is crouched, set vScale = localScale / 4
vScale = myTransform.localScale.y / 4;
// However much the player's scale is decreased by is the same number used to increase the player's position.y
tmpPosition.y = tmpPosition.y + vScale;
}
// Update player position
myTransform.position = tmpPosition;
// Update player scale
tmpScale.y = $$anonymous$$athf.Lerp (myTransform.localScale.y, vScale, crouchTime * Time.deltaTime);
myTransform.localScale = tmpScale;
Now my only question is how could I make my code more efficient, or perhaps there's any bugs I may encounter in the future? It's working at the moment with simply jumping onto a single block that's taller then the player's jump height, though pressing crouch while in the air will allow the player to "pick up his legs" and get on top of the block
Your script doesn't work! I deleted everything in the default scrip then i copy pasted that and it said it had errors! It says: A namespace can only contain types and namespace declarations
Your answer
Follow this Question
Related Questions
Help with player script. 1 Answer
Making a cube crouch 1 Answer
I want to make a character jump and crouch without using boolean 0 Answers
What is the "jump"/ Y axis? How It's called in scripting? 1 Answer
I need help with animations. 0 Answers