Search Unity

How Can I Find Velocity On The Local Z/X Axis

Discussion in 'Scripting' started by keenanwoodall, Sep 23, 2014.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    598
    I've got a movement script (for a FPS controller) that will lower the velocity on it's individual axis (x and z). If you don't press "W" or "S", it'll SmoothDamp the velocity on the z axis to 0. This also applies to the x axis. This doesn't work though. It's lowering the velocity on the world axis (because velocity is global by default). If I'm facing forward on the z axis, everything works fine because that's the axis that the z velocity is modified from. However, if I face anywhere else and walk forward. Then if I stop walking forward, my velocity will be decreased on the global z axis, not the players forward axis. I need to lower to velocity on the local z and x axis, not global. If you need my code (there's a lot of it) I'll post it below. Also I recorded a video that shows the players velocity. In the video, I never pressed left or right keys, I only walked forward. You'll notice that the velocity sways away from where the player is facing.

    Code (js):
    1.  
    2. @script AddComponentMenu("First Person Player/Player Movement")
    3. @script RequireComponent(Rigidbody)
    4. @script RequireComponent(CapsuleCollider)
    5.  
    6. enum MovementStyle{Snappy, Smooth}
    7. var movementStyle : MovementStyle;
    8. var walkSpeed : float = 3.0;
    9. var runSpeed : float = 5.0;
    10. var crouchSpeed : float = 1.0;
    11. var jumpStrength : float = 3.0;
    12. var acceleration : float = 2000;
    13. var deacceleration : float = 0.15;
    14. var inAirAcceleration : float = 0.1;
    15. var minRoofHeightToStand : float = 1.5;
    16. var standToCrouchTime : float = 0.2;
    17. var crouchHeight : float = 0.4;
    18. var maxJumpableAngle : float = 60.0;
    19. var playerCamera : GameObject;
    20. var debug : boolean;
    21.  
    22. @HideInInspector
    23. var hor : float;
    24. @HideInInspector
    25. var ver : float;
    26. @HideInInspector
    27. var grounded : boolean;
    28.  
    29. private var xzMovement : Vector2;
    30. private var deaccelVelX : float;
    31. private var deaccelVelZ : float;
    32. private var startScaleY : float;
    33. private var contact : ContactPoint;
    34. private var minCeilingHeight : float;
    35. private var canStand : boolean = true;
    36. private var crouching : boolean;
    37. private var crouchScaleY : float;
    38. private var currentCrouchRatio : float = 1;
    39. private var crouchVel : float;
    40. private var capsule : CapsuleCollider;
    41. private var physicMaterial : PhysicMaterial;
    42. private var tdt : float;
    43.  
    44. function Awake ()
    45. {
    46.     capsule = this.gameObject.GetComponent(CapsuleCollider);
    47.     currentCrouchRatio = 1;
    48.     startScaleY = transform.localScale.y;
    49.     crouchScaleY = transform.localScale.y * crouchHeight;
    50. }
    51. function Start ()
    52. {
    53.     physicMaterial = new PhysicMaterial();
    54.     physicMaterial.dynamicFriction = 0;
    55.     physicMaterial.staticFriction = 0;
    56.     physicMaterial.frictionCombine = PhysicMaterialCombine.Minimum;
    57.     capsule.material = physicMaterial;
    58. }
    59. function Update ()
    60. {
    61.     if(debug == true)
    62.     {
    63.         Debug.DrawRay(this.transform.position, Vector3(0, minCeilingHeight, 0), Color.green);
    64.         Debug.DrawRay(this.transform.position, transform.forward, Color.white);
    65.         Debug.DrawRay(this.transform.position, rigidbody.velocity, Color.yellow);
    66.     }
    67.     xzMovement.x = rigidbody.velocity.x;
    68.     xzMovement.y = rigidbody.velocity.z;
    69.     if(movementStyle == 0)
    70.     {
    71.         hor = Input.GetAxisRaw("Horizontal");
    72.         ver = Input.GetAxisRaw("Vertical");
    73.     }
    74.     else
    75.     {
    76.         hor = Input.GetAxis("Horizontal");
    77.         ver = Input.GetAxis("Vertical");
    78.     }
    79.     tdt = Time.deltaTime;
    80.  
    81.     transform.rotation = Quaternion.Euler(0, playerCamera.GetComponent(MouseLook).currentYRotation, 0);
    82.     if(this.transform.localScale.y <= capsule.radius)
    83.     {
    84.         capsule.radius = this.transform.localScale.y;
    85.     }
    86.     else
    87.     {
    88.         capsule.radius = 0.5;
    89.     }
    90.     if (Input.GetButton("Crouch") || !canStand)
    91.     {
    92.         Crouch ();
    93.         crouching = true;
    94.     }
    95.     if(!Input.GetButton("Crouch"))
    96.     {
    97.         CanStand();
    98.         if(canStand)
    99.             Stand();
    100.         else
    101.             Crouch();
    102.     }
    103.     transform.localScale.y = Mathf.Lerp(crouchScaleY, startScaleY, currentCrouchRatio);
    104.     if(crouching)
    105.     {
    106.         //if(xzMovement.magnitude > crouchSpeed)
    107.         //{
    108.             xzMovement.x = Mathf.Clamp(xzMovement.x, -crouchSpeed, crouchSpeed);
    109.             xzMovement.y = Mathf.Clamp(xzMovement.y, -crouchSpeed, crouchSpeed);
    110.         //}  
    111.     }
    112.     else if(Input.GetButton("Run"))
    113.     {
    114.         //if(xzMovement.magnitude > runSpeed)
    115.         //{
    116.             xzMovement.x = Mathf.Clamp(xzMovement.x, -runSpeed, runSpeed);
    117.             xzMovement.y = Mathf.Clamp(xzMovement.y, -runSpeed, runSpeed);
    118.         //}
    119.     }
    120.     else
    121.     {
    122.         //if(xzMovement.magnitude > walkSpeed)
    123.         //{
    124.             xzMovement.x = Mathf.Clamp(xzMovement.x, -walkSpeed, walkSpeed);
    125.             xzMovement.y = Mathf.Clamp(xzMovement.y, -walkSpeed, walkSpeed);
    126.         //}
    127.     }
    128.     if (grounded)
    129.     {
    130.         rigidbody.AddRelativeForce(hor * acceleration * tdt, 0, ver * acceleration * tdt);
    131.         if (Input.GetButtonDown("Jump"))
    132.             Jump();
    133.     }
    134.     else rigidbody.AddRelativeForce(hor * acceleration * inAirAcceleration * tdt, 0, ver * acceleration * inAirAcceleration * tdt);
    135.     if(hor == 0.0)
    136.             xzMovement.x = Mathf.SmoothDamp(xzMovement.x, 0, deaccelVelX, deacceleration);
    137.     if(ver == 0.0)
    138.             xzMovement.y = Mathf.SmoothDamp(xzMovement.y, 0, deaccelVelZ, deacceleration);
    139.     //What I want to happen is the velocity on the local axis needs to be changed. Instead, it is being set to be equal to the xzMovement variable which is
    140.     //set to the velocity in world space.
    141.     rigidbody.velocity.x = xzMovement.x;
    142.     rigidbody.velocity.z = xzMovement.y;
    143. }
    144. function OnCollisionStay (col : Collision)
    145. {
    146.     for (contact in col.contacts)
    147.         if (Vector3.Angle(contact.normal, Vector3.up) < maxJumpableAngle)
    148.             grounded = true;
    149. }
    150. function OnCollisionExit ()
    151. {
    152.     grounded = false;
    153. }
    154. function Crouch ()
    155. {
    156.     currentCrouchRatio = Mathf.SmoothDamp(currentCrouchRatio, 0, crouchVel, standToCrouchTime);
    157.     crouching = true;
    158. }
    159. function Stand ()
    160. {
    161.     currentCrouchRatio = Mathf.SmoothDamp(currentCrouchRatio, 1, crouchVel, standToCrouchTime);
    162.     crouching = false;
    163. }
    164. function CanStand ()
    165. {
    166.     minCeilingHeight = minRoofHeightToStand - this.transform.localScale.y + crouchHeight;
    167.     var pos : Vector3 = this.transform.position;
    168.     if(Physics.Raycast(Vector3(pos.x, pos.y, pos.z), Vector3.up, minCeilingHeight))
    169.         canStand = false;
    170.     else
    171.         canStand =  true;
    172. }
    173. function Jump ()
    174. {
    175.     if(!crouching)
    176.         rigidbody.AddForce(0, jumpStrength * 100, 0);
    177.     else
    178.         rigidbody.AddForce(0, jumpStrength * 50, 0);
    179. }
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    still not using fixedupdate for physics stuff i see... bad kitty.

    why I appreciate the sentiment of writing your own, why dont you just use this one? http://wiki.unity3d.com/index.php?title=RigidbodyFPSWalker

    As to getting local velocity/torque values, the way I did it was capturing the difference between frames of the values.
     
    keenanwoodall likes this.
  3. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    598
    Thanks, I'd like to make my own script though. I get weird about using other peoples stuff. I'll look at the FPS Walker for help though. Btw, "bad kitty", I laughed really hard at that :)
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I can understand that... im generally the same, but every now and then I can use other ppls code, especially if its a short script.

    The fps walker will be a good reference though as it works pretty well (mostly).
     
  5. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    598
    SO I took some notes from the script and figured it out (mostly). I made the xzMovement variable hold the local velocity. I did this by getting the velocity and then throwing it through the InverseTransformDirection function, which returns the vector in local form. Then I applied all my modifications (Clamping and SmoothDamping) to the local vector, then I turned the local Vector back into a global vector and set the velocity equal to it. Its a little bit redundant but works fine:)
    Code (js):
    1.  
    2. @script AddComponentMenu("First Person Player/Player Movement")
    3. @script RequireComponent(Rigidbody, CapsuleCollider)
    4.  
    5. enum MovementStyle{Snappy, Smooth}
    6. var movementStyle : MovementStyle;
    7. var walkSpeed : float = 3.0;
    8. var runSpeed : float = 5.0;
    9. var crouchSpeed : float = 1.0;
    10. var jumpStrength : float = 3.0;
    11. var acceleration : float = 2000;
    12. var deacceleration : float = 0.15;
    13. var inAirAcceleration : float = 0.1;
    14. var minRoofHeightToStand : float = 1.5;
    15. var standToCrouchTime : float = 0.2;
    16. var crouchHeight : float = 0.4;
    17. var maxJumpableAngle : float = 60.0;
    18. var playerCamera : GameObject;
    19. var debug : boolean;
    20.  
    21. @HideInInspector
    22. var hor : float;
    23. @HideInInspector
    24. var ver : float;
    25. @HideInInspector
    26. var grounded : boolean;
    27.  
    28. private var xzMovement : Vector3;
    29. private var deaccelVelX : float;
    30. private var deaccelVelZ : float;
    31. private var startScaleY : float;
    32. private var contact : ContactPoint;
    33. private var minCeilingHeight : float;
    34. private var canStand : boolean = true;
    35. private var crouching : boolean;
    36. private var crouchScaleY : float;
    37. private var currentCrouchRatio : float = 1;
    38. private var crouchVel : float;
    39. private var capsule : CapsuleCollider;
    40. private var physicMaterial : PhysicMaterial;
    41. private var tdt : float;
    42.  
    43. function Awake ()
    44. {
    45.     capsule = this.gameObject.GetComponent(CapsuleCollider);
    46.     currentCrouchRatio = 1;
    47.     startScaleY = transform.localScale.y;
    48.     crouchScaleY = transform.localScale.y * crouchHeight;
    49. }
    50. function Start ()
    51. {
    52.     physicMaterial = new PhysicMaterial();
    53.     physicMaterial.dynamicFriction = 0;
    54.     physicMaterial.staticFriction = 0;
    55.     physicMaterial.frictionCombine = PhysicMaterialCombine.Minimum;
    56.     capsule.material = physicMaterial;
    57. }
    58. function Update ()
    59. {
    60.     if(debug == true)
    61.     {
    62.         Debug.DrawRay(this.transform.position, Vector3(0, minCeilingHeight, 0), Color.green);
    63.         Debug.DrawRay(this.transform.position, transform.forward, Color.white);
    64.         Debug.DrawRay(this.transform.position, rigidbody.velocity, Color.yellow);
    65.     }
    66.     xzMovement = transform.InverseTransformDirection(rigidbody.velocity);
    67.     if(movementStyle == 0)
    68.     {
    69.         hor = Input.GetAxisRaw("Horizontal");
    70.         ver = Input.GetAxisRaw("Vertical");
    71.     }
    72.     else
    73.     {
    74.         hor = Input.GetAxis("Horizontal");
    75.         ver = Input.GetAxis("Vertical");
    76.     }
    77.     tdt = Time.deltaTime;
    78.    
    79.     transform.rotation = Quaternion.Euler(0, playerCamera.GetComponent(MouseLook).currentYRotation, 0);
    80.     if(this.transform.localScale.y <= capsule.radius)
    81.     {
    82.         capsule.radius = this.transform.localScale.y;
    83.     }
    84.     else
    85.     {
    86.         capsule.radius = 0.5;
    87.     }
    88.     if (Input.GetButton("Crouch") || !canStand)
    89.     {
    90.         Crouch ();
    91.         crouching = true;
    92.     }
    93.     if(!Input.GetButton("Crouch"))
    94.     {
    95.         CanStand();
    96.         if(canStand)
    97.             Stand();
    98.         else
    99.             Crouch();
    100.     }
    101.     transform.localScale.y = Mathf.Lerp(crouchScaleY, startScaleY, currentCrouchRatio);
    102.     if(crouching)
    103.     {
    104.         //if(xzMovement.magnitude > crouchSpeed)
    105.         //{
    106.             xzMovement.x = Mathf.Clamp(xzMovement.x, -crouchSpeed, crouchSpeed);
    107.             xzMovement.z = Mathf.Clamp(xzMovement.z, -crouchSpeed, crouchSpeed);
    108.         //}  
    109.     }
    110.     else if(Input.GetButton("Run"))
    111.     {
    112.         //if(xzMovement.magnitude > runSpeed)
    113.         //{
    114.             xzMovement.x = Mathf.Clamp(xzMovement.x, -runSpeed, runSpeed);
    115.             xzMovement.z = Mathf.Clamp(xzMovement.z, -runSpeed, runSpeed);
    116.         //}
    117.     }
    118.     else
    119.     {
    120.         //if(xzMovement.magnitude > walkSpeed)
    121.         //{
    122.             xzMovement.x = Mathf.Clamp(xzMovement.x, -walkSpeed, walkSpeed);
    123.             xzMovement.z = Mathf.Clamp(xzMovement.z, -walkSpeed, walkSpeed);
    124.         //}
    125.     }
    126.     if (grounded)
    127.     {
    128.         if (Input.GetButtonDown("Jump"))
    129.             Jump();
    130.     }
    131.     if(hor == 0.0)
    132.             xzMovement.x = Mathf.SmoothDamp(xzMovement.x, 0, deaccelVelX, deacceleration);
    133.     if(ver == 0.0)
    134.             xzMovement.z = Mathf.SmoothDamp(xzMovement.y, 0, deaccelVelZ, deacceleration);
    135. }
    136. function FixedUpdate ()
    137. {
    138.     if (grounded)
    139.     {
    140.         rigidbody.AddRelativeForce(hor * acceleration * tdt, 0, ver * acceleration * tdt);
    141.     }
    142.     else rigidbody.AddRelativeForce(hor * acceleration * inAirAcceleration * tdt, 0, ver * acceleration * inAirAcceleration * tdt);
    143.     rigidbody.velocity = transform.TransformDirection(xzMovement);
    144. }
    145. function OnCollisionStay (col : Collision)
    146. {
    147.     for (contact in col.contacts)
    148.         if (Vector3.Angle(contact.normal, Vector3.up) < maxJumpableAngle)
    149.             grounded = true;
    150. }
    151. function OnCollisionExit ()
    152. {
    153.     grounded = false;
    154. }
    155. function Crouch ()
    156. {
    157.     currentCrouchRatio = Mathf.SmoothDamp(currentCrouchRatio, 0, crouchVel, standToCrouchTime);
    158.     crouching = true;
    159. }
    160. function Stand ()
    161. {
    162.     currentCrouchRatio = Mathf.SmoothDamp(currentCrouchRatio, 1, crouchVel, standToCrouchTime);
    163.     crouching = false;
    164. }
    165. function CanStand ()
    166. {
    167.     minCeilingHeight = minRoofHeightToStand - this.transform.localScale.y + crouchHeight;
    168.     var pos : Vector3 = this.transform.position;
    169.     if(Physics.Raycast(Vector3(pos.x, pos.y, pos.z), Vector3.up, minCeilingHeight))
    170.         canStand = false;
    171.     else
    172.         canStand =  true;
    173. }
    174. function Jump ()
    175. {
    176.     if(!crouching)
    177.         rigidbody.AddForce(0, jumpStrength * 100, 0);
    178.     else
    179.         rigidbody.AddForce(0, jumpStrength * 50, 0);
    180. }
     
  6. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    598
    So now there's a new problem...I didn't notice this until I started jumping, but the player always has force applied in the negative local z-axis. Whenever I jump up in the air the player shoots up and forward, then as i descend the player falls down and backwards. It almost moves as if on a diagonal line. Oh yeah, and also, if I change the force mode (right now there isn't one) jumping doesn't work at all. The player doesn't seem to have any new forces applied when spacebar is pressed.
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Ok, so had a read through your script.. couple things.

    1. Changing the scale of your transform is gonna give you grief (if its the transform/gameObject with the rigidbody -- if its a child it shouldnt matter). If you can achieve the same result without messing with scale, do it. Scale affects physics, so yep avoid this if possible. This could be causing your odd jump behaviour, although im not 100 convinced. Try not changing the scale and see what result you get.

    2. Changing the forcemode will often require different force values. Ie ForceMode.Impulse often requires very small numbers, while default forcemode takes mass into account. Not seeing any movement probably means it wasnt enough force involved, although this is partially speculation without knowing full setup details. Impulse is great for stopping perfectly (torque/force) if used on the local axis (you simply apply the negative force and it should be the exact amount required to cancel forces on that axis)

    3. Rather than using transform.rotation, use Rigidbody.MoveRotation.

    4. I think for your jump code you should perhaps change it a little and do it before you start Inversing and reverting

    Code (csharp):
    1.  
    2.  
    3. if(Input.GetKey("Jump"))
    4.   Jump();
    5.  
    6. xzMovement = transform.InverseTransformDirection(rigidbody.velocity)
    7.  
    8.  
    9. function Jump()
    10. {
    11.    if(grounded)
    12.      rigidbody.velocity.y = 50;  //i use c#, not sure if this is valid code or not
    13.  
    14. }
    15.  
    That way the localvelocity has the jump power included... the documented way to jump is to adjust velocity this way rather than use forces. http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

    the example they use is the Jump key.
     
  8. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    598
    Wow, that was a great answer. Thanks for the in-depth response, and I can't believe you took the time to read my code! I tried doing what you said for the jumping. Now, though, I don't think its related to the jumping. If I drag the player up in the air and then press play, the player falls down and backward. I think its based on y velocity or something. If the y velocity is positive, the local z velocity is positive. If the y velocity is negative, the local z velocity becomes negative. If you're up for round two and want to look at my changed script, here it is :)
    Code (js):
    1.  
    2. @script AddComponentMenu("First Person Player/Player Movement")
    3. @script RequireComponent(Rigidbody, CapsuleCollider)
    4.  
    5. enum MovementStyle{Snappy, Smooth}
    6. var movementStyle : MovementStyle;
    7. var walkSpeed : float = 3.0;
    8. var runSpeed : float = 5.0;
    9. var crouchSpeed : float = 1.0;
    10. var jumpStrength : float = 3.0;
    11. var acceleration : float = 2000;
    12. var deacceleration : float = 0.15;
    13. var inAirAcceleration : float = 0.1;
    14. var minRoofHeightToStand : float = 1.5;
    15. var standToCrouchTime : float = 0.2;
    16. var crouchHeight : float = 0.4;
    17. var maxJumpableAngle : float = 60.0;
    18. var playerCamera : GameObject;
    19. var debug : boolean;
    20.  
    21. @HideInInspector
    22. var hor : float;
    23. @HideInInspector
    24. var ver : float;
    25. @HideInInspector
    26. var grounded : boolean;
    27.  
    28. private var xzMovement : Vector3;
    29. private var deaccelVelX : float;
    30. private var deaccelVelZ : float;
    31. private var startScaleY : float;
    32. private var contact : ContactPoint;
    33. private var minCeilingHeight : float;
    34. private var canStand : boolean = true;
    35. private var crouching : boolean;
    36. private var crouchScaleY : float;
    37. private var currentCrouchRatio : float = 1;
    38. private var crouchVel : float;
    39. private var capsule : CapsuleCollider;
    40. private var physicMaterial : PhysicMaterial;
    41. private var tdt : float;
    42.  
    43. function Awake ()
    44. {
    45.     capsule = this.gameObject.GetComponent(CapsuleCollider);
    46.     currentCrouchRatio = 1;
    47.     startScaleY = transform.localScale.y;
    48.     crouchScaleY = transform.localScale.y * crouchHeight;
    49. }
    50. function Start ()
    51. {
    52.     physicMaterial = new PhysicMaterial();
    53.     physicMaterial.dynamicFriction = 0;
    54.     physicMaterial.staticFriction = 0;
    55.     physicMaterial.frictionCombine = PhysicMaterialCombine.Minimum;
    56.     capsule.material = physicMaterial;
    57. }
    58. function Update ()
    59. {
    60.     if(debug == true)
    61.     {
    62.         Debug.DrawRay(this.transform.position, Vector3(0, minCeilingHeight, 0), Color.green);
    63.         Debug.DrawRay(this.transform.position, transform.forward, Color.white);
    64.         Debug.DrawRay(this.transform.position, rigidbody.velocity, Color.yellow);
    65.         Debug.Log(ver);
    66.     }
    67.     xzMovement = transform.InverseTransformDirection(rigidbody.velocity);
    68.     if(movementStyle == 0)
    69.     {
    70.         hor = Input.GetAxisRaw("Horizontal");
    71.         ver = Input.GetAxisRaw("Vertical");
    72.     }
    73.     else
    74.     {
    75.         hor = Input.GetAxis("Horizontal");
    76.         ver = Input.GetAxis("Vertical");
    77.     }
    78.     tdt = Time.deltaTime;
    79.    
    80.     transform.rotation = Quaternion.Euler(0, playerCamera.GetComponent(MouseLook).currentYRotation, 0);
    81.     if(this.transform.localScale.y <= capsule.radius)
    82.     {
    83.         capsule.radius = this.transform.localScale.y;
    84.     }
    85.     else
    86.     {
    87.         capsule.radius = 0.5;
    88.     }
    89.     if (Input.GetButton("Crouch") || !canStand)
    90.     {
    91.         Crouch ();
    92.         crouching = true;
    93.     }
    94.     if(!Input.GetButton("Crouch"))
    95.     {
    96.         CanStand();
    97.         if(canStand)
    98.             Stand();
    99.         else
    100.             Crouch();
    101.     }
    102.     transform.localScale.y = Mathf.Lerp(crouchScaleY, startScaleY, currentCrouchRatio);
    103.     if(crouching)
    104.     {
    105.         //if(xzMovement.magnitude > crouchSpeed)
    106.         //{
    107.             xzMovement.x = Mathf.Clamp(xzMovement.x, -crouchSpeed, crouchSpeed);
    108.             xzMovement.z = Mathf.Clamp(xzMovement.z, -crouchSpeed, crouchSpeed);
    109.         //}  
    110.     }
    111.     else if(Input.GetButton("Run"))
    112.     {
    113.         //if(xzMovement.magnitude > runSpeed)
    114.         //{
    115.             xzMovement.x = Mathf.Clamp(xzMovement.x, -runSpeed, runSpeed);
    116.             xzMovement.z = Mathf.Clamp(xzMovement.z, -runSpeed, runSpeed);
    117.         //}
    118.     }
    119.     else
    120.     {
    121.         //if(xzMovement.magnitude > walkSpeed)
    122.         //{
    123.             xzMovement.x = Mathf.Clamp(xzMovement.x, -walkSpeed, walkSpeed);
    124.             xzMovement.z = Mathf.Clamp(xzMovement.z, -walkSpeed, walkSpeed);
    125.         //}
    126.     }
    127.     if(hor == 0.0)
    128.             xzMovement.x = Mathf.SmoothDamp(xzMovement.x, 0, deaccelVelX, deacceleration);
    129.     if(ver == 0.0)
    130.             xzMovement.z = Mathf.SmoothDamp(xzMovement.y, 0, deaccelVelZ, deacceleration);
    131.     xzMovement = transform.TransformDirection(xzMovement);
    132.     rigidbody.velocity = xzMovement;
    133.     if (grounded)
    134.     {
    135.         if (Input.GetButtonDown("Jump"))
    136.             Jump();
    137.     }
    138. }
    139. function FixedUpdate ()
    140. {
    141.     if (grounded)
    142.     {
    143.         rigidbody.AddRelativeForce(hor * acceleration * tdt, 0, ver * acceleration * tdt);
    144.     }
    145.     else rigidbody.AddRelativeForce(hor * acceleration * inAirAcceleration * tdt, 0, ver * acceleration * inAirAcceleration * tdt);
    146. }
    147. function OnCollisionStay (col : Collision)
    148. {
    149.     for (contact in col.contacts)
    150.         if (Vector3.Angle(contact.normal, Vector3.up) < maxJumpableAngle)
    151.             grounded = true;
    152. }
    153. function OnCollisionExit ()
    154. {
    155.     grounded = false;
    156. }
    157. function Crouch ()
    158. {
    159.     currentCrouchRatio = Mathf.SmoothDamp(currentCrouchRatio, 0, crouchVel, standToCrouchTime);
    160.     crouching = true;
    161. }
    162. function Stand ()
    163. {
    164.     currentCrouchRatio = Mathf.SmoothDamp(currentCrouchRatio, 1, crouchVel, standToCrouchTime);
    165.     crouching = false;
    166. }
    167. function CanStand ()
    168. {
    169.     minCeilingHeight = minRoofHeightToStand - this.transform.localScale.y + crouchHeight;
    170.     var pos : Vector3 = this.transform.position;
    171.     if(Physics.Raycast(Vector3(pos.x, pos.y, pos.z), Vector3.up, minCeilingHeight))
    172.         canStand = false;
    173.     else
    174.         canStand =  true;
    175. }
    176. function Jump ()
    177. {
    178.     if(!crouching)
    179.         rigidbody.velocity += Vector3 (0, jumpStrength, 0);
    180.     else
    181.         rigidbody.velocity += Vector3 (0, jumpStrength, 0);
    182. }