Search Unity

Gradual Slowdown

Discussion in 'Scripting' started by gumboots, Jun 4, 2011.

  1. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    Hi guys,

    Really sorry for the really basic question, I know I'm doing something silly.

    I've just begun an isometric shooter engine (Lara Croft and the Guardian of Light), and I'm using my Xbox controller set up in Unity. (Which is inputting perfectly)

    So I can make my player move with the joystick, but I'm looking to have a slight slide when you stop (Length depends on your speed). I've tried a couple of different ways, this is the one I'm working with at the moment, but all of them just seem to stop still. (I think it has to do with the Input value being 0 when the joystick isn't being pushed, but I've tried to counteract that by only setting velocity when it is being pushed..)

    So here's what I've got, any help would be great, or a link in the right direction:

    static var xVelocity : float = 0.0;
    static var zVelocity : float = 0.0;
    static var slowDownSpeed : float = .1;
    static var moveSpeed : float = 3;

    Code (csharp):
    1. function Update ()
    2. {
    3.     if(Input.GetAxis("Horizontal"))
    4.     {
    5.         xVelocity += (Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime) * -1;
    6.     }
    7.  
    8.     if(!Input.GetAxis("Horizontal"))
    9.     {
    10.         xVelocity *= slowDownSpeed * Time.deltaTime;
    11.     }
    12.     else if (!Input.GetAxis("Vertical"))
    13.     {
    14.         zVelocity *= slowDownSpeed * Time.deltaTime;
    15.     }
    16.  
    17.     transform.position.x += xVelocity * Time.deltaTime;
    18.     transform.position.z += zVelocity * Time.deltaTime;
    19.  
    20. }