x


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
}
more ▼

asked Jun 11 '12 at 01:24 AM

Chuckler gravatar image

Chuckler
20 2 5 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jun 11 '12 at 01:59 AM

Mortoc gravatar image

Mortoc
915 2 7 14

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

Jun 11 '12 at 04:53 PM Chuckler

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! :)

Jun 11 '12 at 05:10 PM Chuckler
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x804
x410
x388
x40
x19

asked: Jun 11 '12 at 01:24 AM

Seen: 572 times

Last Updated: Jun 11 '12 at 05:10 PM