Search Unity

precise movement

Discussion in 'Scripting' started by MindSpyke, Jul 3, 2015.

  1. MindSpyke

    MindSpyke

    Joined:
    Sep 13, 2013
    Posts:
    14
    hey I've got this code to move around my 'player' but the movement doesn't feel precise .. it feels slippery
    any solutions to fix this in a decent way(mainly through code) ? Any help is much appreciated :)

    // right now it adds force every frame to the object's rigidbody until the object reaches the max speed
    //this is left and right movement
    // _movespeed is 35f and _maxspeed is also 35f

    void Update () {

    if(_rigid.velocity.magnitude < _maxspeed)
    {

    if(Input.GetKey("d"))
    {
    _input = new Vector3(1f,0f,0f);

    _rigid.AddForce(_input*_movespeed);

    Debug.Log(_input);
    }

    if(Input.GetKey("q"))
    {
    _input = new Vector3(-1*1f,0f,0f);

    _rigid.AddForce(_input*_movespeed);

    Debug.Log(_input);
    }
    }



    }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You might want to check out the CharacterController, controlling the player as a rigidbody is always going to feel a little "off".
     
  3. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    If you are using forces, you probably want to do that inside FixedUpdate, so that the physics can react correctly. Forces will most often be a bit slippery/laggy. You might be better off setting the actual velocity yourself, then you have more control over the movement.
     
  4. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987