- Home /
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
}
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.
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
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
![](https://koobas.hobune.stream/wayback/20220613072614im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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