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 killtez · May 26, 2021 at 06:21 PM · transformcharacter controllerheightcrouching

Character Controller Crouch With transform.localScale Problem.

I am trying to make my first person character crouch, I first attempted by changing the height value, but it didn't bring the rest of the objects attached to my character such as the camera, down with it and it caused it to fall through terrain.

Now I am trying to use transform.localScale, after having moved the center of my Character Controller object down to its feet it scales correctly on the y axis in scene view when using the inspector menu.

However when I attempt to use transform.localScale in the actual game view via script, it scales on both ends of the y axis, which is not what I want to happen.

The problem that bugs me the most is, as you can see in the video how my character falls off ledges when "un-crouching".

I have included a video and my code, if anyone would be able to help I would greatly appreciate it :).

Code https://pastebin.com/Jav1HKw8

Video https://www.youtube.com/watch?v=x9ocuFOibBc

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
1

Answer by Ermiq · May 27, 2021 at 09:18 AM

First of all, please post the code here instead of using external resources or screenshots. Use the 101 010 button in the post input field to format the code to get it displayed like this

 SomeMethod() {
     Some Code;
 }

There's no guarantee that the code pasted on an external resource will be available in the future, so if someone will google a question similar to yours, they won't be able to see the actual code snippets if we all use pastbin or anything else to show the code.


Now to the question.
Yeah, when you change a transform y scale it's center point doesn't change and the transform is getting scaled on top and bottom simultaneously. That's just how it works and you can't change this behavior. And you shouldn't adjust the center point as you did, because it will produce more problems than it solves in the future.
To fix your problem you could adjust the capsule position manually to keep its bottom point on the floor right after you change the scale.
A bit of simple math:
You have a capsule with height 1 that is standing on the floor.
Now you change the capsule y scale from 1 to 2. And the distance between the capsule's bottom point and the floor is now equals to halfOfNewHeight - halfOfOriginalHeight = (2 / 2) - (1 / 2) or (newHeight - originalHeight) / 2.
And to keep the capsule on the floor in the next frame when the engine will render it with new scale, you need to change the character's position by that difference distance from the floor right after you change the scale.
So, do something like this:

 float originalScale = transform.localScale.y;
 transform.localScale = new Vector3(0.66f, 0.7f, 0.66f);
 float difference =
     (transform.localScale - originalScale) / 2f;

This is the distance difference. Note that it might have a negative value when you scale the character down from 1 to 0.7 ( 0.7 - 1) / 2 = -0.15. That's good, because this allows us to use the same formulas to adjust the character position in both cases: when it scaled down and needs to be moved down, or it scaled up and needs to be moved up. Now you can use it to put the character's bottom to the same position as before the scaling:

 transform.position += Vector3.up * difference;

This code means: use the Vector3.up which is the world space vector that points up and it's length is 1 unit, multiply it by the difference we got to get a vector that represents some change in position with the given length/distance along the world up direction. So, if we got 'difference = 0.15` then this code will move the transform.position up for 0.15 units. And if difference = -0.15 then the position will be moved down by 0.15 units.

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 killtez · May 29, 2021 at 02:56 PM 0
Share

Thank you for your help. I was unable to get your code to work because I am incompetent. However you did make me realize that transform.position will only work for my project if "Auto Sync Transforms" is enabled in the project settings.

My code now looks like this and works "flawlessly" so far:

     if (Input.GetButtonDown("left ctrl"))
     {
         transform.localScale = new Vector3(0.66f, 0.7f, 0.66f);
         transform.position += Vector3.down * 0.6f;
     }
     if (Input.GetButtonUp("left ctrl"))
     {
         transform.localScale = new Vector3(0.66f, 1f, 0.66f);
         transform.position += Vector3.up * 0.6f;
     }

As you can see I am completely new to Unity and C#, however this works the way I want it to and I am happy you could help me find a solution for my problem, thank you.

If you have any improvements or further tips I would be grateful.

Kind regards.

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

155 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 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 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 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 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

Difference between scale and height in a gameobject with charactercontroller 1 Answer

Root motion in child not moving with parent 2 Answers

character controller height transition 0 Answers

CharacterController does not transform as expected 1 Answer

Movement Script Bug 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