Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ArtNinJackson · Feb 11, 2020 at 01:27 PM · 2d-platformerraycastingcrouchcrouching

Unity 2D - Need Help Staying Crouched while Under Something

Howdy!

So my question is simple; I'm building a 2D platformer and I'm trying to simply implement some functionality that will help players have a better game feel, and possibly prevent bugs. All I'd like to do is to check if something is above my character and keep him crouching if I'm underneath something. The moment I come out from under said thing, I'd like to stand back up (if not holding crouch).

Normally, I have players hold Left Control to crouch, and the character stands when it's released. I'd like to know how to keep the character crouched if under something, and especially if under something with the button released. Then simply spring upward when out from under said thing.

Any help is appreciated and I'll include my character controller for reference. Oh, and I guess it'd also be helpful to mention that I'm using Unity 2019.1.4f1. If I need to upgrade, I can; just thought I'd clarify.

Thanks guys!

using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerPlatformerController : PhysicsObject { public float maxSpeed = 10; public float jumpTakeOffSpeed = 20; public float fallMultiplier = 2.5f; //from tutorial; jumping feels wonky because gravity isn't speeding up fall. public float lowJumpMultiplier = 2f; //for when you don't jump all the way BoxCollider2D playerBoxCollider; ParticleSystem particles; Rigidbody2D rb; ParticleSystem ps; private SpriteRenderer spriteRenderer; private Animator animator; // Use this for initialization void Awake() { playerBoxCollider = GameObject.Find("PlayerObject").GetComponent<BoxCollider2D>(); particles = GameObject.Find("PlayerParticles").GetComponent<ParticleSystem>(); spriteRenderer = GetComponent<SpriteRenderer>(); animator = GetComponent<Animator>(); rb = GetComponent<Rigidbody2D>(); } protected override void ComputeVelocity() { Vector2 move = Vector2.zero; move.x = Input.GetAxis("Horizontal"); if (Input.GetButtonDown("Jump") && grounded) { velocity.y = jumpTakeOffSpeed + 3f; } else if (Input.GetButtonUp("Jump")) { if (rb.velocity.y < 0) { rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime; } if (velocity.y > 0) { velocity.y = velocity.y * 0.5f; rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime; } } bool flipSprite = (spriteRenderer.flipX ? (move.x > 0.0f) : (move.x < 0.0f)); if (flipSprite) { spriteRenderer.flipX = !spriteRenderer.flipX; maxSpeed = 10; } animator.SetBool("grounded", grounded); animator.SetBool("crouchEngaged", crouchEngaged); animator.SetFloat("velocityX", Mathf.Abs (velocity.x) / maxSpeed); targetVelocity = move * maxSpeed; if ((move.x != 0.0f) && !grounded) //if set as (move.x > 0) it works only running left, but not right { particles.Play(true); } else if (velocity.x == 0.0f) { particles.Play(false); } if (Input.GetKey(KeyCode.LeftControl)) { playerBoxCollider.enabled = false; maxSpeed = 4; crouchEngaged = true; } else if (Input.GetKeyUp(KeyCode.LeftControl)) { crouchEngaged = false; playerBoxCollider.enabled = true; maxSpeed = 10; } if (Input.GetKey(KeyCode.LeftShift)) { if(maxSpeed < 20) { maxSpeed += 3 * Time.deltaTime; } } if (Input.GetKeyUp(KeyCode.LeftShift)) { maxSpeed = 10; } Debug.Log(maxSpeed); } // research needs to be done on this function. When going under, it will remain crouched (which is good) // however, once you need to "stand" the animation never happens. /*bool Stand() { //work in progress }*/ }

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

207 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Crouching not nearly smooth enough 0 Answers

player can only jump or move during crouch, not both 0 Answers

[C#] playerHeight not working for FPS Controller 2 Answers

Vertical Rays rapidly changing between "detecting ground" to "not detecting ground" 0 Answers

Camera offset when crouching 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