Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question MoveTowards changes speed after one finished movement

Discussion in 'Scripting' started by masternully, Dec 25, 2020.

  1. masternully

    masternully

    Joined:
    Dec 22, 2018
    Posts:
    13
    Why the first time everything is okay, movement works as need, and after this movement, speed is up. I do nothing to change speed, how it works? I use SpriteRenderer on an island and Camera movement that moves to the desired object.
     
    Last edited: Dec 25, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Is something changing your speed variable? To find out, make it a
    const
    and see if the problem goes away.

    Also, it's difficult to see code in a video, so instead please use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Generally, to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  3. masternully

    masternully

    Joined:
    Dec 22, 2018
    Posts:
    13
    The first time it runs 120 times, the second (when the speed is faster) 5 times. I made const var and it doesn't help.
    I tried a lot of solutions, it's not a typical error, I don't know how to fix it. Probably the reason is that I'm using Vector3.MoveTowards when works in 2D. But why the speed is increasing?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    No, it has nothing to do with that. Vector3D can handle 2D motion too.

    Hard to say until you post some readable relevant code using code tags.
     
  5. masternully

    masternully

    Joined:
    Dec 22, 2018
    Posts:
    13
    I fixed it by using Vector2.MoveTowards. My Camera and Sprite have different Z positions, maybe it was a reason. The first time it moved slowly because it also needed to move along Z, and when it on Z, it moves faster.
     
    Last edited: Dec 25, 2020
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Okay, I stand corrected... that's probably it if you were starting from way up by the camera and then moving into the scene, yeah, 3D vs 2D makes a difference with move-towards. My bad.