Search Unity

Slides while walking and Turning.... How To Fix?

Discussion in 'Physics' started by Elder4Ever, Dec 18, 2015.

  1. Elder4Ever

    Elder4Ever

    Joined:
    Dec 17, 2015
    Posts:
    2
    So I was trying to make a character move and now when I go to turn while walking I start sliding in a circle.... how do I fix that so I can make the character turn with the slide kinda like a call of duty walk?

    <code>


    #pragma strict


    var playerAcceleration : float = 500;

    var cameraObject : GameObject;

    var maxSpeed : float = 20;

    var horizontalMovement : Vector2;

    var deacceleration : float;

    var deaccelerationX : float;

    var deaccelerationZ : float;

    var jumpSpeed : float = 500;

    var maxSlope : float = 60;

    var grounded : boolean = false;

    function Update ()


    {

    horizontalMovement = Vector2(GetComponent.<Rigidbody>().velocity.x,GetComponent.<Rigidbody>().velocity.z);

    if (horizontalMovement.magnitude > maxSpeed)


    {

    horizontalMovement = horizontalMovement.normalized;

    horizontalMovement *= maxSpeed;

    }

    GetComponent.<Rigidbody>().velocity.x = horizontalMovement.x;

    GetComponent.<Rigidbody>().velocity.z = horizontalMovement.y;

    if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0)


    {

    GetComponent.<Rigidbody>().velocity.x = Mathf.SmoothDamp(GetComponent.<Rigidbody>().velocity.x,0,deaccelerationX, deacceleration);

    GetComponent.<Rigidbody>().velocity.z = Mathf.SmoothDamp(GetComponent.<Rigidbody>().velocity.z,0,deaccelerationZ, deacceleration);

    }



    transform.rotation = Quaternion.Euler(0,cameraObject.GetComponent(MouseLook).CurrentYRotation,0);

    GetComponent.<Rigidbody>().AddRelativeForce(Input.GetAxis("Horizontal")*playerAcceleration * Time.deltaTime,0,Input.GetAxis("Vertical")*playerAcceleration * Time.deltaTime);

    if (Input.GetButtonDown("Jump")&& grounded)


    {

    GetComponent.<Rigidbody>().AddForce(0,jumpSpeed,0);

    }



    }

    function OnCollisionStay(collision: Collision){

    for(var contact : ContactPoint in collision.contacts){

    if(Vector3.Angle(contact.normal,Vector3.up) < maxSlope){

    grounded = true;


    }

    }

    }

    function OnCollisionExit(){

    grounded = false;


    }


    </code>
     
  2. Lee7

    Lee7

    Joined:
    Feb 11, 2014
    Posts:
    137
    Character Controller.