- Home /
In 2D I'm having no luck crouching using Raycasts and transform.localScale, transform.localPosition and a for loop. Help?
Hi all, I'm working on a sidescroller and I've implemented basically everything that I want to related to the player except for crouching which I couldn't figure out before, and I can't figure out now. I'm using raycasts to detect all my states. No rigidbodies for me.
The code that I finally got to work for making my character shrink to his desired crouch size is:
if (grounded && input.y > -0.25f) {
transform.localScale = new Vector3(transform.localScale.x, standSize, transform.localScale.z);
}
if (grounded && input.y < -0.25f) {
transform.localScale = new Vector3(transform.localScale.x, crouchSize,transform.localScale.z);
}
However, when I press down, my character shrinks from his center point causing him to fall to the ground. Then when I let go of holding down he return back to normal size from his center point but clips through the ground and slowly rises back up the ground's surface. Obviously I don't want either of those to happen.
I have also tried changing his localPosition to try and move him down to the ground as he shrinks so there is no falling down. No luck here.
if (grounded && input.y > -0.25f) {
transform.localScale = new Vector3(transform.localScale.x, standSize, transform.localScale.z);
}
if (grounded && input.y < -0.25f) {
transform.localScale = new Vector3(transform.localScale.x, crouchSize,transform.localScale.z);
gameObject.transform.localPosition = new Vector3 (transform.position.x, transform.localPosition.y - 0.1f, transform.position.z);
}
For the entire duration you hold "down", he falls (clips?) through the floor and any obstacles plummeting into Out of Bounds Hell.
I then figured let's try a for loop, limiting him to changing his localPosition only once:
if (grounded && input.y > -0.25f) {
transform.localScale = new Vector3(transform.localScale.x, standSize, transform.localScale.z);
}
if (grounded && input.y < -0.25f) {
transform.localScale = new Vector3(transform.localScale.x, crouchSize,transform.localScale.z);
for (int i = 0; i < 1; i++) {
gameObject.transform.localPosition = new Vector3 (transform.position.x, transform.localPosition.y - 0.1f, transform.position.z);
}
}
I don't know if I'm not using the for loop correctly as I'm still new to coding, but it didn't seem to work or even effect anything at all. He still clips through any and all platforms/obstacles.
I'm about at my wits end here before I just say "SCREW IT! I GUESS MY PLAYER DOESN'T GET TO CROUCH!" Any ideas or pointers that you could give me?
Thanks in advance!
PS. On a side note, I also want to make sure that my player cannot crouch jump. Just some extra info for all you coding junkies to chew on.
Your answer
Follow this Question
Related Questions
2D platformer - character clips into the ground 1 Answer
Player sinking into walls and floor with Raycasts 1 Answer
2D Platformer - Sidekick Follow Player 0 Answers
How do i make a level editor for a 2d platformer game? 0 Answers
A* Pathfinding issues, player node is always off (converting 3d to 2d) 0 Answers