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 /
avatar image
0
Question by shepsaus000 · Nov 18, 2015 at 05:45 PM · scripting problem

Crouching help needed

I'm trying to get my player to crouch and I'm having some difficulty doing so. You see, I tried making it so that my game object would simply translate downwards, but it refuses to go through the floor (surprisingly). I would like to point out that the player has a character controller component on as well as the following code for crouching:

     public GameObject player;
 
     void Update () {
 
         //crouching
 
         if (Input.GetButton ("Fire1")){
             player.transform.Translate(0, -0.5f, 0);
         }


By the way, I made sure that the button associated with Fire1 is the same as in the input manager.

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

3 Replies

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

Answer by Dave29483 · Nov 18, 2015 at 05:57 PM

@shepsaus000

You won't be able to simply move the character controller down as it has a physics component to it. Also this would cause you to fall through geometry. The best way is to adjust the height of the character controller via Lerp.

If you do this in FixedUpdate, the controller won't jiggle or fall through collision hulls.

You may want to consider using raycast to determine if your current location has enough clearance for you to stand up again, before allowing it.

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 Dinosaurs · Nov 18, 2015 at 06:03 PM

This question is a little vague, and it's unclear what the desired outcome of the crouch is, but in most FPS games the goal would be to make your player collider smaller to fit behind cover. In that case, I'd suggest scaling down the players Y scale down rather than moving them into the floor.

It's not surprising that the character controller won't move into the floor; the character controller shouldn't be able to walk through floors or walls, so naturally trying to move into the floor will cause it to collide and stop.

http://docs.unity3d.com/ScriptReference/CharacterController.html

It's also worth noting that you should probably be performing the player translation in FixedUpdate rather than Update; because the character controller will interact with rigidbodies in the collision system, this will help ensure that it collides smoothly with objects in the world.

http://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html

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 Slendyberkay · Nov 18, 2015 at 08:40 PM

You can probably use 2 different collides for your character. Move the camera down by 0.9 tiles and disable top part of collider. Use raycasting to check if player can get up. Etc

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

34 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

Related Questions

How to create a script that displays a text box when the user collects an item?,How to create a script that displays a text box when the user collects an object? 1 Answer

Script error 1 Answer

Scripting relating to FIRST PERSON SHOOTER 1 Answer

Animation script gravity problem 0 Answers

Why does this give a "nullReferenceException"? 2 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