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 Charles_Gams · Mar 26, 2013 at 03:47 PM · controllerjumpheight

Change character controller height when Jumping

I'm trying to change the character controller height when jumping which works fine however it doesn't reset to its original height when it touches the ground again after Jumping.

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 Unitraxx · Mar 26, 2013 at 04:42 PM

 var jumpHeight : float;
 var standardHeight : float;
 
 standardHeight = controller.height;
 jumpHeight = controller.height/2;

Take these 4 lines out of the update() function and put them together with the other variables that aren't in any function.

Edit : or place these 2 lines

 var jumpHeight : float;
 var standardHeight : float;

alone outside the function, and place these 2 lines

 standardHeight = controller.height;
 jumpHeight = controller.height/2;

in start()

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 Charles_Gams · Mar 26, 2013 at 05:26 PM 0
Share

Thanks alot !!

avatar image
1

Answer by fabio1955 · Mar 26, 2013 at 03:50 PM

You should be more precise. Why don't you attach the code you are using?

Comment
Add comment · Show 2 · 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 Jessy · Mar 26, 2013 at 04:04 PM 0
Share

This should be a comment, not an answer.

avatar image Charles_Gams · Mar 26, 2013 at 04:09 PM 0
Share

This is the code of how it stands

avatar image
0

Answer by Charles_Gams · Mar 26, 2013 at 04:21 PM

var speed : float = 50.0; var rotateSpeed : float = 3.0; var jumpSpeed : float = 30.0; var doublejumpSpeed: float = 20.0; var gravity : float = 30.0; var target:Transform; var bouncespeed:float = 10.0; private var crouching : boolean = false; var jumpRate = 0.8; private var nextlength = 0.0;

private var moveDirection : Vector3 = Vector3.zero;

 function Update () 
 {
     var controller : CharacterController = GetComponent(CharacterController);
     var jumpHeight : float;
     var standardHeight : float;
      crouching = false;
  
     standardHeight = controller.height;
     jumpHeight = controller.height/2;
     // Rotate around y - axis
     transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
     
     // Move forward / backward
     var forward : Vector3 = transform.TransformDirection(Vector3.forward);
     var curSpeed : float = speed * Input.GetAxis ("Vertical");
     
     controller.SimpleMove(forward * curSpeed);
     
     
        if(controller.isGrounded)
        {
            controller.height = standardHeight ;
          
         if (Input.GetButtonDown ("Jump")) 
         {  
             moveDirection.y= jumpSpeed;
             controller.height = jumpHeight;
             controller.center.y=0.06;
            
            
         }
         
         }
         
        
         
         
         if( jumpSpeed == 30.0 && !controller.isGrounded )
         
          {
               if(Input.GetButton("Fire2"))
               {
                
                   var step = bouncespeed * Time.deltaTime;
     
     // Move our position a step closer to the target.
                   transform.position = Vector3.MoveTowards(transform.position, target.position, step);
                    }
              
          
           }
           
          
       
           
               
               
               
         
        
        
        
         
    
     
     
     
     controller.Move(moveDirection * Time.deltaTime);
     moveDirection.y -= gravity * Time.deltaTime;
 }
 





/// This is the code of how it stands

Comment
Add comment · Show 9 · 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 Unitraxx · Mar 26, 2013 at 04:39 PM 0
Share

If you jump multiple times, does your controller get smaller and smaller?

avatar image Charles_Gams · Mar 26, 2013 at 04:40 PM 0
Share

Yes the character controller does get smaller

avatar image Unitraxx · Mar 26, 2013 at 04:43 PM 0
Share

See my answer (and accept it, if it works)

avatar image fabio1955 · Mar 26, 2013 at 04:44 PM 0
Share

I think the problem should be here:

  jumpHeight = controller.height/2;

and later (GetButtonDown) you write

 controller.height = jumpHeight;

so the height will be shorter eand shorter. I think you should clean up the code for example setting the standardheight in a Start function and declaring it as a private variable..etc...

avatar image Unitraxx · Mar 26, 2013 at 04:46 PM 0
Share

Yeah, that's exactly what I thought. So the variables should be placed outside the update function as I showed in my answer.

Show more comments

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

13 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

Related Questions

Smoothing out jumping? 1 Answer

I also used to check is grounded raycast and OnCollisionEnter2D but still there was such a problem. 0 Answers

MMD How to export model and animations to Unity as 3rd person controller? 2 Answers

Player appears to teleport instead of adding force 2 Answers

Not able to jump when running!Where did I go wrong? 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