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 Chuckler · Jun 11, 2012 at 01:24 AM · playerscalechildcrouchdistort

Crouch in FPS changes player scale not controller height

I have been using UNITY for almost 3 months now. I have a crouch script working that activates when I press LEFT SHIFT...Everything works fine, except everything parented to the player controller shrinks and warps as well via scale. When looking up and down, I have a camera (equipped) that is now suspended in front of the player and warped vertically..! :o So I know that it has to do with the scale command and that I should call directly the player controller HEIGHT variable instead but so far I haven't figured out the code for it...

Many times i would fall right through the floor when crouching but i did manage to fix that by modifying the controller radius.

Here is my current script for crouch and run. (I am going to try and learn as much JAVA as I can as well!I really enjoy learning this...custom everything!) I am basically trying to avoid distorting children items (or scale altogether)

//RUN.js

var walkSpeed: float = 7; // regular speed var crchSpeed: float = 3; // crouching speed var prnSpeed: float = 1; // crouching speed var runSpeed: float = 20; // run speed *high for saving testing time

private var chMotor: CharacterMotor; private var tr: Transform; private var dist: float; // distance to ground

function Start(){ chMotor = GetComponent(CharacterMotor); tr = transform; var ch:CharacterController = GetComponent(CharacterController); dist = ch.height/2; // calculate distance to ground }

function Update(){

 var vScale = 1.0;
 var speed = walkSpeed;
 
 if (Input.GetKey("left ctrl") || Input.GetKey("right ctrl")){
     speed = runSpeed;
 }
 if (Input.GetKey("left shift")){
     vScale = 0.55;
     speed = crchSpeed; // slow down when crouching
 }
 if (Input.GetKey("left alt")){
     vScale = 0.27;
     speed = prnSpeed; // slow down when prone
 }
 chMotor.movement.maxForwardSpeed = speed; // set max speed
 var ultScale = tr.localScale.y; // crouch/stand up smoothly 
 tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
 tr.position.y += dist * (tr.localScale.y-ultScale); // fix vertical position

}

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
Best Answer

Answer by Mortoc · Jun 11, 2012 at 01:59 AM

Yes, as you've guessed, scale is not the correct way to do this. You need to get a reference to your CharacterController (possibly as a public variable on your script that you set, or by using GetComponent). Then you can modify the height of the CharacterController from script just fine.

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 Chuckler · Jun 11, 2012 at 04:53 PM 0
Share

Ahhh, GetComponent...I see. ok i will try this...I did try the script with a var (not private) and couldn't get it to fly, but now maybe i can get that controller to respond with GetComponent...hmm Thanks i will check, brb

avatar image Chuckler · Jun 11, 2012 at 05:10 PM 0
Share

nope, not working...i want to change all the Scale references with Height but that won't do it...i have no idea , obviously I need to learn the syntax better. This script which i cut and pasted and am trying to reverse engineer in terms of understanding, works but squeezes everything down...i tried to use a dummy object, empty game object to link both controller and weapons to but no go so far... Im really more of a level designer but i'd like to get this scripting down! :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to change child's rotation whithout affecting it's scale? 1 Answer

Moving Platforms & Scale (Parent/Child) 1 Answer

Object scale/rotation changes when parented to flipped object 0 Answers

change/scale child rectTransform with Parent (with video) 0 Answers

Geometry distortion on child objects 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