Search Unity

Dribbling on escalator

Discussion in 'Scripting' started by jo6x, Feb 27, 2017.

  1. jo6x

    jo6x

    Joined:
    Jan 2, 2016
    Posts:
    113
    Hallo,

    I made an escalator and use a script to make my player (FPS) go up with the same speed as the escalator.
    But when going up my player is dribbling a lot.
    When changing the head-bob (on the FPSController) from on to off it's a bit better but not what I really want.
    Does someone have a solution for this.

    My code:
    Code (CSharp):
    1.     public Transform target3b;
    2.     public float speed;
    3.     public GameObject Target3a;
    4.     bool moving = false;
    5.  
    6.     void OnTriggerEnter(Collider col)
    7.     {
    8.         if (col.gameObject == Target3a)
    9.         {
    10.             moving = true;
    11.         }
    12.     }
    13.  
    14.     void Update ()
    15.     {
    16.         if (moving)
    17.         {
    18.             float step = speed * Time.deltaTime;
    19.             transform.position = Vector3.MoveTowards (transform.position, target3b.position, step);
    20.         }
    21.     }
     
  2. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Is your escalator moving by means of keyframe animation? The problem with keyframed objects is that their Animator component is updated at a different rate than Update() or FixedUpdate() function. So if you try to match the position of the animated object in one of the update functions you may experience the jitter.
    One way to deal with it is to use transform.SetParent to parent the character to the escalator step when the character is on it.
     
  3. jo6x

    jo6x

    Joined:
    Jan 2, 2016
    Posts:
    113
    Thanks for the reply DimitriX89.
    I just find out something to make it work just fine: https://forum.unity3d.com/threads/d...on-newcameraposition-in-fps-controler.459310/ Maybe you can answer that question / thread.
    I allready tried parenting, also parenting with a step instaed of the escalator, but maybe you can tell me what part is necessary when using parenting.
     
  4. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    412
    You can also look at lateupdate. It happens after the animations are updated.
     
  5. jo6x

    jo6x

    Joined:
    Jan 2, 2016
    Posts:
    113
    Thanks daxiongmao,
    I'm going to try that.