Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Padges · Sep 07, 2010 at 08:23 PM · collisionfpscrouch

Force player to keep crouching

I have a script where my controller shrinks when ctrl is pressed and held, and when it is released then the controller is reset to the original size. The only problem is when I make an instance where player has to crouch to overcome an obstacle, if the crouch is released while in a "vent" or "tunnel" the player stands straight up, clipping through the overhanging wall. Is there a way to resolve this?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Jesse Anders · Sep 07, 2010 at 09:04 PM

You could try using a raycast for this.

Instead of having only two states (crouched and uncrouched), you would add a third state, 'uncrouching'.

When the 'crouch' key is pressed, transition to the 'crouch' state. When the 'crouch' key is released, transition to the 'uncrouching' state. When in the 'uncrouching' state, cast a ray straight up from the player; if there is no hit within a specified distance, transition to the 'uncrouched' state.

Note that your player could still clip into geometry by uncrouching while partially under an obstacle (that is, if the player is partially under an obstacle but the raycast misses the obstacle). You could use multiple raycasts to catch some of these cases, or (perhaps better), instead of using a raycast use a box or capsule trigger collider attached to the player while in crouch mode to determine whether there are any obstacles overhead.

Comment
Add comment · Share
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
avatar image
0

Answer by Peter G · Sep 07, 2010 at 10:28 PM

I would use a trigger. The player can still hit the button, it just won't do anything. Put a box collider or a collection of box colliders fully taking up your tunnel. Then I would do something like this:

var playerScript : PlayerController;

function Start () { playerScript = FindObjectOfType(PlayerController); //Link to the script that //controls playerCrouching. I can't remember if you need a cast in js. //I normally use C# }

function OnTriggerStay (col : Collider) { if(col.tag == "Player") { //Is the collider the player? playerScript.crouched = true; //keep them crouched. } }

While, that would probably work, I actually think this method may be cleaner. All's you need to do is add a boolean to your player script that will cause the crouch button to be overridden with a false. So, when the user clicks control, you would check that it has not be cancelled by overrideCrouch. So this script, turns it on when you enter, and off when you exit. The only problem I can think of is with overlapping triggers. To fix that, either change the arrangement or put playerScript.overrideCrouched = true in OnTriggerStay which would cost you performance, but always work.

var playerScript : PlayerController;

function Start () { playerScript = FindObjectOfType(PlayerController); //Link to the script that //controls playerCrouching. }

function OnTriggerEnter (col : Collider) { if(col.tag == "Player") { //Is the collider the player? playerScript.overrideCrouched = true; //keep them crouched. } }

function OnTriggerExit (col : Collider) { if(col.tag == "Player") { //Is the collider the player? playerScript.overrideCrouched = false; //keep them crouched. } }

and your player script might look something like:

if(Input.GetButtonDown("CrouchButton") {
     if(!overrideCrouch)
         crouched = !crouched;
}
Comment
Add comment · Show 6 · Share
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
avatar image Jesse Anders · Sep 08, 2010 at 11:53 AM 0
Share

Note that this method would require manually placing colliders in all 'crouchable' areas. One advantage of the method I described earlier (assu$$anonymous$$g it works - I haven't tried it myself) would be that no additional design work would be required to support it. You would just build your level geometry as usual, and the rest would be taken care of automatically.

avatar image Peter G · Sep 08, 2010 at 10:48 PM 0
Share

Yes, but this method is more efficient than shooting out multiple rays, and it does not take much effort to place colliders approximately because the messages will be sent no matter how small the trigger collision is.

avatar image Peter G · Sep 08, 2010 at 10:49 PM 0
Share

Edit, and the amount of time it takes to write the raycast script or using Physics.Overlap sphere script, could be used to place the colliders, so the design overhead is negligible.

avatar image Jesse Anders · Sep 09, 2010 at 01:17 AM 0
Share

I would question both those points. Regarding your first point, I highly doubt that performance would be an issue (don't have room here to elaborate though). As for your second point, I think the probability (assu$$anonymous$$g a non-trivial game) of it taking more time to code the system I described than to place colliders manually is low. Plus, there's the burden of having to remember to place a collider in every 'crouch-able' location. But, it doesn't really matter - the OP now has both suggestions, and can use whichever he or she prefers :)

avatar image Peter G · Sep 09, 2010 at 01:54 AM 0
Share

Fair enough. Your points are valid; I'm not sure I completely agree with you, but as you said, the OP has both opinions so whatever works for them.

Show more comments
avatar image
0

Answer by Padges · Sep 11, 2010 at 01:53 PM

well i tried the box collider ontop of the player, and the raycast, but to no avail. Im not so experienced with Unity, and certainly not scripting. for me, well atleast for the time being, it would be easier for me to just make colliders and place them in every crouchable location. But as you said, this would be tedious and it would be so much more effective to have it scripted to the player. Im not so good at scripting, but Ill keep trying, if all else fails, I suppose I could place colliders everywhere. :/

Comment
Add comment · Share
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
avatar image
0

Answer by Proclyon · Oct 28, 2010 at 08:28 AM

You could also try doing a collision check using a projection of the characters true (Idle state) box at the crouch transform position. Or you could mathematically project a fake transform over the positive y axis to determine if increasing the value to that of the original box would create a collision, if so it should maintain the current state rather than return to Idle state.

If you do try this, maybe slightly complex method, be sure you never get conflicting data on which box had what size. Get the information from the source, don't local variable, hardcode or magic number any of them or you will undoubtedly get bugs.

This suggestion puts the work on the script side of the story, not the level generators side, which is a choice you might need to evaluate aswell.

Doing the raycast can be a needle thin line that doesn't match the collision area so you may end up doing again in some thin pipe

Comment
Add comment · Share
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

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

No one has followed this question yet.

Related Questions

Player detecting collision when crouching 1 Answer

Detect collision when changing character controller height 2 Answers

First Person Controller 1 Answer

NavMeshAgent and Collision problem 1 Answer

How to move target when it collides with a bullet 1 Answer


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