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 Crayz · Mar 18, 2014 at 09:22 AM · jumpcrouch

C# Crouch script, player's head lowers rather then picking up feet

Here's my simple crouch script:

 float ultScale = myTransform.localScale.y;
 Vector3 tmpScale = myTransform.localScale;
 Vector3 tmpPosition = myTransform.position;

 // Crouch!
 if (Input.GetButton ("Crouch")) {
     vScale = 0.5f;
 }
 
 tmpPosition.y = tmpPosition.y + ( rayDistance * (myTransform.localScale.y - ultScale) );
 myTransform.position = tmpPosition;
 
 tmpScale.y = Mathf.Lerp (myTransform.localScale.y, vScale, crouchTime * Time.deltaTime);
 myTransform.localScale = tmpScale;

In-game the player is ducking his head rather then picking up his feet. Say my player is 2 meters tall. I can jump 0.5 meters vertically, and crouch down 1 meter. If I'm able to crouch 1 full meter and jump a half meter, I should be able to jump up 1 meter, pick up my legs by pressing crouch, allowing for me to reach the extra 0.5 meters to get up on a block that's 1.5 meters tall.

How could I modify my crouch script to behave like 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by koray1396 · Mar 18, 2014 at 11:55 AM

Do not use raydistance, I assume pivot point is at the center of myTransform, so as well as scaling, move your object down by 0.25*height of the object. Then you can define a second function for pulling the legs up, then scale and movement the object up by 0.25*height. Ground state would determine which function to use.

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 Crayz · Mar 18, 2014 at 10:20 PM 0
Share

Okay so here's what I came up with

 // How fast player will crouch
 public float crouchTime = 20.0f;
 
 // Define transform
 private Transform myTransform;
 
 // Default scale, local scale, and world position
 float vScale = 1.0f;
 Vector3 tmpScale = myTransform.localScale;
 Vector3 tmpPosition = myTransform.position;
 
 // Crouch!
 if (Input.GetButton ("Crouch")) {
     // If player is crouched, set vScale = localScale / 4
     vScale = myTransform.localScale.y / 4;
     // However much the player's scale is decreased by is the same number used to increase the player's position.y
     tmpPosition.y = tmpPosition.y + vScale;
 }
 
 // Update player position
 myTransform.position = tmpPosition;
 
 // Update player scale
 tmpScale.y = $$anonymous$$athf.Lerp (myTransform.localScale.y, vScale, crouchTime * Time.deltaTime);
 myTransform.localScale = tmpScale;

Now my only question is how could I make my code more efficient, or perhaps there's any bugs I may encounter in the future? It's working at the moment with simply jumping onto a single block that's taller then the player's jump height, though pressing crouch while in the air will allow the player to "pick up his legs" and get on top of the block

avatar image FoddyFogHorn · Sep 28, 2014 at 09:56 AM 0
Share

Your script doesn't work! I deleted everything in the default scrip then i copy pasted that and it said it had errors! It says: A namespace can only contain types and namespace declarations

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

21 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

Related Questions

Help with player script. 1 Answer

Making a cube crouch 1 Answer

I want to make a character jump and crouch without using boolean 0 Answers

What is the "jump"/ Y axis? How It's called in scripting? 1 Answer

I need help with animations. 0 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