- Home /
How to make player crouch smoothly, PLZ HELP !!!!
Hi. I have done the code for my player to crouch when I press the C button and stand up when press C again. The problem is my player does this instantly and I dont know to put my crouchingSpeed variable and time.deltatime to my code to make my crouch be smooth. Here's the code. @dcordoba PLZ HELP !!
public bool isCrouched = false;
public CharacterController controller;
public float crouchDistance = 0.9f;
public float crouchingSpeed = 0.7f;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
if(isCrouched == false)
{
isCrouched = true;
//transform.position = (transform.position + new Vector3(0, -crouchDistance , 0));
transform.Translate(new Vector3(0, -crouchDistance , 0));
}
else if(isCrouched == true)
{
isCrouched = false;
//transform.position = (transform.position + new Vector3(0, crouchDistance , 0));
transform.Translate(new Vector3(0, crouchDistance, 0));
}
}
}
3ps or fps?
if is 3ps, its a humanoid right?, you need to animate crouching, to be consistent I think you must use the same animator to walking, crouching and runing, also if you player fall, the falling, jumping etc animations
you can manage the animations trough conditions on the animator, and please put a bool here, that block the inputs while you are animating some things, like crouching or falling, so you cant walk(displace the character) while crouching or falling, just after you are crouched disable the bool, in short, enable the bool at the beggining of each animation, and disable it when animation end
if is fps and you never see the character you can just lerp camera to crouched position, put a counter int busyCount
and increase it each frame, use that to calculate the lerp position, when you reach the max count you have crouched, then stop the lerp, enable the other controls and reset the counter to future usage, the same on the other direction to stand up
@DCordoba Thanks for your reply, $$anonymous$$y game is FPS and I'm a beginner and need help. Can you explain more about the code I should add ??
Answer by MaxLevelNoob · Apr 23, 2020 at 04:29 AM
My bad, I rushed the code previously and did not think you'd like to just change the transform Y-Position. Here is a revised one, done with more effort. Enjoy yourself
public bool isCrouched = false;
public CharacterController controller;
public float crouchDistance = 0.9f;
public float crouchingSpeed = 0.7f;
public bool isChangingPosition = false;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
//True if Key Pressed and Not changing positions currently
if (Input.GetKeyDown(KeyCode.C))
StartCoroutine(Crouch());
}
private IEnumerator Crouch()
{
//True if we're already changing position, then end this function
if (isChangingPosition)
yield break;
//Acknowledge that we are changing position so above criteria will keep out multiple calls
isChangingPosition = true;
//Get our Starting Height
float StartingHeight = transform.position.y;
//Find our End Height
float EndHeight;
if (isCrouched)
EndHeight = StartingHeight + crouchDistance; //We are crouched, hence we are trying to stand up (Go Up / Increase)
else
EndHeight = StartingHeight - crouchDistance; //We are standing, hence we are trying to crouch (Go Down / Decrease)
Debug.Log("E" + EndHeight);
//Keep changing the Y Value until we reach the end height
while (transform.position.y != EndHeight)
{
//Here we check what action we're doing (Standing/Crouching)
//Then we move according to the action
//And finally we check if we have reached the End
if (isCrouched)
{
//We are crouched, hence we are trying to stand up (Go Up / Increase)
transform.Translate(Vector3.up * crouchingSpeed * Time.deltaTime);
//True if we reached our goal
if (transform.position.y >= EndHeight)
{
//Make sure we are EXACTLY at the EndHeight
transform.position = new Vector3(transform.position.x, EndHeight, transform.position.z);
Debug.Log("COMPLETE!");
}
}
else
{
//We are standing, hence we are trying to crouch (Go Down / Decrease)
transform.Translate(Vector3.down * crouchingSpeed * Time.deltaTime);
//True if we reached our goal
if (transform.position.y <= EndHeight)
{
//Make sure we are EXACTLY at the EndHeight
transform.position = new Vector3(transform.position.x, EndHeight, transform.position.z);
Debug.Log("COMPLETE!");
}
}
yield return null;
}
//We have reached the end!
//Flip the Action as we have succeeded in changing now
isCrouched = !isCrouched;
//We're finished changing!
isChangingPosition = false;
yield return null;
}
@$$anonymous$$axLevelNoob Thank you so much !!! It worked perfectly !!! Now I should mix these codes with walking, sprinting and jumping code, which I hope wont be that hard. How many points should I reward you ??
Don't know about any points. So it's all good, stay safe and happy developing :)
Answer by Kwel · Apr 22, 2020 at 08:46 PM
You need to apply a time factor to the translation. You can use Vector3.Lerp
as mentioned in this post:
https://answers.unity.com/questions/444978/c-smoothing-out-transformtranslate.html
Maybe you could also make a basic animation for that through the editor and customize there the speed curve of the movement graphically. Also you can then blend animations through the Animator controller's transitions.
Your answer
Follow this Question
Related Questions
Need help with crouching,Problem with crouching 1 Answer
Camera jitters when getting up from croutch 1 Answer
How can i stop my Third person character from crouching from a weapon i placed in his hand? 0 Answers
Character Controller Crouching Jitter Issue 2 Answers
Horror Crouch Script Help 1 Answer