Search Unity

2D Mouse Movement Bouncing.

Discussion in '2D' started by Gasric, Feb 17, 2019.

  1. Gasric

    Gasric

    Joined:
    Sep 8, 2013
    Posts:
    66
    Hi Folks,

    What i am trying to do is click a location and have the player move to that clicked location. This does this but has side effects. The issue is with the sprite constantly bouncing at the start or when it moves up a ramp.

    I understand that it has something to do with the code in FixedUpdate() constantly being checked but if i remove the code into a function i.e. MoveToPos() it will only move a very small distance and not to the mouse position.

    Code (CSharp):
    1.  
    2. public Vector2 targetPos;
    3.     public float speed = 5f;
    4.  
    5.     private void Start()
    6.     {
    7.  
    8.     }
    9.     void Update()
    10.     {
    11.         if (Input.GetMouseButtonDown(0))
    12.         {
    13.             targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);  
    14.         }
    15.  
    16.  
    17.     }
    18.  
    19.     private void FixedUpdate()
    20.     {
    21.  
    22.         transform.position = Vector2.MoveTowards(transform.position,
    23.             targetPos, Time.deltaTime * speed);
    24.  
    25.     }
    26.  
    27. /*
    28.     void MoveToPos()
    29.     {
    30.      
    31.  
    32.         transform.position = Vector2.MoveTowards(transform.position,
    33.         targetPos, Time.fixedDeltaTime * speed);
    34.     }*/
    35.  
    I know i a missing something but its not coming to me. Any Ideas?
    Thanks.
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    you are likely using colliders and have them set up incorrectly causing your character to 'bounce back' when hitting the ramp rather than climb on top of it. alternatively your 'target position' is too low in the ground so your character is trying to walk into the ground but unable and bouncing back
     
  3. Gasric

    Gasric

    Joined:
    Sep 8, 2013
    Posts:
    66
    What is the best way to solve this issue then please?
     
  4. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    firstly determine if this is the actual issue. you can try this with placing you character inside blocks or setting gravity to something large or just general testing stuff to determine where the issue/error sits. once you determine what the issue is, you can either finetune your parameters so the special scenario can't happen (eg. can never move fast enough to clip through a wall), or you can add code to handle special cases (eg if clipped through a wall). most likely though just resizing the collider and its settings should fix it though.
     
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @Gasric

    You pretty much didn't tell anything about your setup...

    "The issue is with the sprite constantly bouncing at the start or when it moves up a ramp."

    In your code you have:

    transform.position = Vector2.MoveTowards(transform.position, targetPos, Time.deltaTime * speed);

    Because you are talking about ramp, I take it that you have a side view i.e. you move up along y-axis.

    If you only define movement between points A and B, how would you expect your object behave when there is an obstacle along that straight line between points A and B?

    Also, you are moving you object directly using transform - not rigidbody? So basically you are ignoring Physics system. If you want to use Physics system, then move object using rigidbody.
     
  6. Gasric

    Gasric

    Joined:
    Sep 8, 2013
    Posts:
    66
    What you have spoken about was correct and I went back and started reading more into how and the what. I know have a completely different system using a A* style but very simple to map the movements correctly.

    I have also removed physics as this was not required in the end and my approach was way off.

    Thanks anyway.