• 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 JayByrd4101 · Oct 24, 2012 at 12:42 AM · crouchfallsprintcharacter motor

Character stand after crouching without falling through floor?

Here is the script I am using:

 var walkSpeed: float = 7; // regular speed 
 var crchSpeed: float = 3; // crouching speed 
 var runSpeed: float = 20; // run speed
 
 private var chMotor: CharacterMotor; 
 private var ch: CharacterController; 
 private var tr: Transform; 
 private var height: float; // initial height
 
 function Start(){ 
     chMotor = GetComponent(CharacterMotor); 
     tr = transform; 
     ch = GetComponent(CharacterController); 
     height = ch.height; 
 }
 
 function Update()
 {
     var h = height;
     var speed = walkSpeed;
 
     if (ch.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
         speed = runSpeed;
     }
     if (Input.GetKey("c")){ // press C to crouch
         h = 0.5 * height;
         speed = crchSpeed; // slow down when crouching
     }
 
     chMotor.movement.maxForwardSpeed = speed; // set max speed
     var lastHeight = ch.height; // crouch/stand up smoothly 
     ch.height = Mathf.Lerp(ch.height, h, 5*Time.deltaTime);
     tr.position.y += (ch.height-lastHeight)/2; // fix vertical position
 }

Can anyone tell me what I can add or change to get the script to work?

Comment
Add comment · Show 6
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 JayByrd4101 · Oct 23, 2012 at 08:58 PM 0
Share

It only happens when the character isn't moving.

avatar image AlucardJay · Oct 24, 2012 at 01:28 AM 0
Share

I just formatted your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window =]

avatar image JayByrd4101 · Nov 18, 2012 at 05:25 AM 0
Share

Also, if it could be smoothed, a smooth stand up ins$$anonymous$$d of just moving the character up in virtual space that would be great.

avatar image sparkzbarca · Nov 20, 2012 at 01:15 AM 0
Share

crouching is normally an animation. Normally you animate the character lower and then you move the camera not the character down. I can tell you how to move your camera down but i can't tell you how to lower your character down so his knees are up to his chest.

I can tell you how to change the colliders height so its smaller during crouching if you want to be able to pass through stuff you were too tall to before.

avatar image AlucardJay · Nov 20, 2012 at 03:56 AM 0
Share

I am imagining the real problem is that when you stand (not press or hold C), the collider returns to the original height, but pushes the character down through the floor?

Possibly raycast from the ch position plus ch height for 2 units, if there is no hit then allow the height lerp to continue.

I guess the best method would be if (Input.Get$$anonymous$$eyUp("c")){ // raycast above my head to check if I can stand here. If so canStand = true; if (canStand) { ch.height = $$anonymous$$athf.Lerp........

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

12 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

Related Questions

How to make character movement like a Half-Life 2? 0 Answers

How do I add a sprint recharge 1 Answer

Achieved Prone & Crouch, but there is a little question in my mind. (Character Controller) 0 Answers

Improved Footsteps system 1 Answer

Cannot get sprint animation to play when crouch works fine? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges