Search Unity

help needed: moving smoothly in 4 directions + 1 constant forward acceleration ?

Discussion in 'Scripting' started by hanso23, Mar 25, 2013.

  1. hanso23

    hanso23

    Joined:
    Jul 22, 2012
    Posts:
    16
    hey everyone! i need some help with a simple moving script problem. Just before we go into details, I'm rookie, so detailed "instructions" are welcome ;)

    what i want to acchive:

    a spaceship which can fly into 4 directions ( up, down, left, right and for diagonally) and a constant "forward" acceleration. it a firstperson view game. Maybe the movement from starfox is an good example, basically an on-rail shooter with this movement.

    right now it's looking like this:

    1 gameobject,

    our shipour ship forward acceleration (js):
    PHP:
    var moveSpeed 10.0;
    var 
    sideMovement 10.0;
    var 
    upMovement 10.0;
    function 
    Update ()
    {
    var 
    sideMovement Input.GetAxis("Horizontal") * moveSpeed;
     
    sideMovement *= Time.deltaTime;
     
    transform.Translate(sideMovement,0,moveSpeed);

    var 
    upMovement Input.GetAxis("Vertical") * moveSpeed;
     
    upMovement *= Time.deltaTime;
     
    transform.Translate(upMovement,0,moveSpeed);
    }
    our ship movement:

    PHP:
    // The horizontal speed of the keyboard controls. A higher value will 
    // cause the object to move more rapidly.
    var keyboardSpeed 20.0;
     
    // FixedUpdate is a built-in unity function that is called every fixed framerate frame.
    // According to the docs, FixedUpdate should be used instead of Update when dealing with a
    // Rigidbody.
    // See http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.FixedUpdate.html 
    // for more information.
    function FixedUpdate () {
        
    // This is where we move the object.
     
        // Get input from the keyboard, with automatic smoothing (GetAxis instead of GetAxisRaw).
        // We always want the movement to be framerate independent, so we multiply by Time.deltaTime.
        
    var keyboardX Input.GetAxis("Horizontal") * keyboardSpeed Time.deltaTime;
        var 
    keyboardY Input.GetAxis("Vertical") * keyboardSpeed Time.deltaTime;
     
        
    // Calculate the new position based on the above input.
        // If you want to limit the movement, you can use Mathf.Clamp
        // to limit the allowed range of newPos.x or newPos.y.
        
    var newPos rigidbody.position Vector3(keyboardXkeyboardY0.0);
     
        
    // Move the object.
        
    rigidbody.MovePosition(newPos);
    }
     
    // Require a Rigidbody component to be attached to the same GameObject.
    @script RequireComponent(Rigidbody)

    THE PROBLEM:

    if we move our ship in any direction (especially in the diagonal ones) the moving animation looks kinda laggy or "not smooth").
    how can we solve this? any script snips pieces are welcome ;)

    EDIT: this was just our first way to do it, i have no problem with replacing the code with an other one!
     
    Last edited: Mar 25, 2013
  2. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    "Laggy" and "Not Smooth" are two completely different things.

    I'm guessing you meant "not smooth", and the likely cause is a mismatch between Update, ie: draw frames, ie: what you *see*, and FixedUpdate, ie: physics frames, ie: what is happening behind the scenes in your code. Basically, physics are not guaranteed (and will not!) run the same number times between draw calls, so the time that has passed between visual snapshots will vary, making the movement *look* "jittery".

    One potential fix would be to set rigidbody.interpolation = RigidbodyInterpolation.Interpolate and apply forces in FixedUpdate using rigidbody.AddForce() instead of directly moving your object with rigidbody.MovePosition(). This will *visually* take into account a "partial" physics frame when drawing to smooth out your movement, at the cost of a tiny bit (up to 1 physics frame) of visual lag.
     
    Last edited: Mar 25, 2013