Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Character Sliding

Discussion in 'Scripting' started by Darvo, Jan 4, 2015.

  1. Darvo

    Darvo

    Joined:
    Jan 3, 2015
    Posts:
    5
    my character is sliding in this script i dont know how to make him walk normally

    Code (JavaScript):
    1. #pragma strict
    2. var playerAcceleration : float = 500;
    3. var cameraObject : GameObject;
    4. var maxSpeed : float = 20;
    5. var horizontalMovement : Vector2;
    6. var deaceleration : float;
    7. var deacelerationX : float;
    8. var deacelerationZ : float;
    9. var jumpSpeed : float = 20;
    10. var maxSlope : float = 60;
    11. var grounded : boolean = false;
    12.  
    13. function Update ()
    14.     {
    15.        horizontalMovement = Vector2(rigidbody.velocity.x,rigidbody.velocity.z);
    16.        if (horizontalMovement.magnitude > maxSpeed)
    17.            {
    18.                horizontalMovement = horizontalMovement.normalized;
    19.                horizontalMovement *= maxSpeed;
    20.            }
    21.        rigidbody.velocity.x = horizontalMovement.x;
    22.        rigidbody.velocity.z = horizontalMovement.y;
    23.      
    24.        if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0)
    25.            {
    26.               rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x,0,deacelerationX, deaceleration);
    27.               rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z,0,deacelerationZ, deaceleration);
    28.            }
    29.        transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLook).CurrentYRotation,0);
    30.        rigidbody.AddRelativeForce(Input.GetAxis("Horizontal")*playerAcceleration * Time.deltaTime,0,Input.GetAxis("Vertical")*playerAcceleration * Time.deltaTime);
    31.        if (Input.GetButtonDown("Jump")&& grounded)
    32.            {
    33.                rigidbody.AddForce(0,jumpSpeed,0);
    34.            }
    35.     }
    36. function OnCollisionStay(collision : Collision)
    37.     {
    38.         for (var contact : ContactPoint in collision.contacts)
    39.              {
    40.                  if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
    41.                      {
    42.                          grounded = true;
    43.                      }
    44.              }
    45.      }
    46. function OnCollisionExit()
    47.     {
    48.         grounded = false;
    49.     }
    50.  
     
  2. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Try to freeze your X, Y and Z rotations in Rigidbody, by:
    1) Click on rigidbody component
    2) Click on "constraints" (should be at the bottom or something)
    3) Tick the lower three ones, a.k.a X, Y and Z.
     
  3. Darvo

    Darvo

    Joined:
    Jan 3, 2015
    Posts:
    5
    Tried that didnt work
     
  4. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Try to delete the slope effect from your script. Slope is basically when the player is sliding when standing on a higher point than zero, and this may be bugged for you. Try to delete everything that has to do with slope in your script.