Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sheketchecka · Jul 06, 2015 at 12:56 PM · 2dplatformercrouching

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges