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 Drakmyth · Dec 30, 2012 at 08:30 PM · collisioncharactercontrollercomponentcrouch

Detect collision when changing character controller height

I am using the standard first person character controller and wanted to enable crouching. I added the following script to it, based on one I found in another question on this site.

 using UnityEngine;
 using System.Collections;
 
 public class CrouchController : MonoBehaviour
 {
     private CharacterController character;
     private float initialHeight;
 
     void Awake()
     {
         character = GetComponent<CharacterController>();
         initialHeight = character.height;
     }
 
     void Update()
     {
         float newHeight = Input.GetButton("Crouch") ? 0.5f * initialHeight : initialHeight;
         float currentHeight = character.height;
         character.height = Mathf.Lerp(currentHeight, newHeight, 5 * Time.deltaTime);
         transform.position = transform.position + new Vector3(0, (character.height - currentHeight) / 2, 0);
     }
 }

This works perfectly except for one small detail. If I crouch and move beneath a collider, then release crouch, I will return to full height and pass through the collider. I would like the player to simply stop when this kind of collision occurs, but I'm having a hard time figuring out how to do this. It doesn't seem like I can place a script directly on the CharacterController component to detect this (nor do I know how I would filter such collisions out from normal ones) and detecting a collision with the character controller component from within this script doesn't appear to be possible. Any thoughts on how I can go about solving this dilemma?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by TomPendergrass · Dec 30, 2012 at 09:19 PM

I just recently added this functionality to my video game (arxcatalyst.weebly.com) Although I'm programming my character control script from scratch and haven't really used the character controller, the same technique applies I'm sure. When my character wants to stand I do a sphere cast upwards. It's distance is up to the height the character would be standing normally. if it collides with something, I don't allow the function to run that makes the character stand. The spherecast has the same radius as the capsule collider on the character.

Comment
Add comment · Show 1 · 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 Drakmyth · Dec 31, 2012 at 02:54 AM 0
Share

Worked like a charm. Thanks!

avatar image
1

Answer by whydoidoit · Dec 30, 2012 at 09:22 PM

You could simply have a trigger box/sphere collider attached to a child of the player that doesn't change its position during the crouch and then maintain a boolean when that trigger is entered and reset it when it exits. If the boolean is set, the character should not try to stand up.

Comment
Add comment · Show 3 · 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 Drakmyth · Dec 31, 2012 at 02:59 AM 0
Share

I had the same idea and tried doing this initially, but since that script moves the entire object, it would have involved placing the entire character controller GameObject inside an empty for the explicit purpose of tracking crouching. Feels a little hacky to me since the trigger would be detecting collisions even when I'm not crouching (jumping against the ceiling for instance).

avatar image whydoidoit · Dec 31, 2012 at 10:29 AM 0
Share

Sure - but the performance of a sphere cast is significantly slower than a trigger collision - still as its for your player - it shouldn't be too onerous.

I would be turning that child object off (SetActive(false)) when not crouching BTW

avatar image TomPendergrass · Dec 31, 2012 at 04:22 PM 0
Share

Would you $$anonymous$$d showing me the facts that prove a sphere cast projected only when the Input is Released is slower than a trigger that must adjust it's state through every collision? I do many sphere casts for characters and my own Artificial Pathfinding. The pathfinding script completed about 40 spherecasts in 2 milliseconds last I checked.

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

10 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

Related Questions

Character Controller - My movement speed changes dont stick. 1 Answer

Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer

Character Controller meets Rigidbody 1 Answer

How Can One Collider Recognize Contact With Another? 0 Answers

Problem with character controller and collisions 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