Search Unity

2D sprite: Move to new location

Discussion in 'Scripting' started by masterpug13, Apr 17, 2019.

  1. masterpug13

    masterpug13

    Joined:
    Mar 31, 2019
    Posts:
    19
    Hey all!

    Having sprite trouble (again). In a nutshell, I am setting up animations and events to play out in a specific order. Animation events are used to tell the player object to transition between animations and move in a certain way. The issue I am having is that the movement isn't working. My player object plays the appropriate animation, but it isn't moving and so it locks up the rest of the scripts.

    The script I am using for movement is based on locating the nearest drop-off point. Essentially, I have it so the pickup animation is played. Once that is done, it executes the Transporter function. The animation (which loops) for transporting has an event for ToDrop. ToDrop should then transition to Dropper as shown below, which plays an animation and then goes back to idle, giving the player control over the player object.

    Whatever is breaking appears to be in Transporter, as the game object just gets stuck in the animation and never moves. Thus ToDrop and Dropper are never initialized.

    Code (CSharp):
    1.    
    2.     public void Transporter()
    3.     {
    4.         playerHand.SetBool("toPit", true);
    5.         playerHand.SetBool("handCatch", false);
    6. // This is the part that seems to not work.
    7.         var nearestPit = Pit.FindClosestPit(transform.position);
    8.         transform.position = Vector2.Lerp(transform.position, nearestPit.transform.position, moveSpeed);
    9.         Vector2 difference = nearestPit.transform.position - transform.position;
    10.  
    11.         handBody.position = new Vector2(handBody.position.x, handBody.position.y);
    12.     }
    13.  
    14.     public void ToDrop()
    15.     {
    16.         var nearestPit = Pit.FindClosestPit(transform.position);
    17.         if (gameObject.transform.position == nearestPit.transform.position)
    18.         {
    19.             Dropper();
    20.         }
    21.     }
    22.  
    23.     public void Dropper()
    24.     {
    25.         playerHand.SetBool("droppping", true);
    26.         playerHand.SetBool("toPit", false);
    27.         playerHand.SetBool("handCatch", false);
    28.     }
    29.  
    Am I missing a Vector in here somewhere?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    You're probably going to have to play it and bring up the Animator window and see what state you're stuck in, what the properties are at runtime, etc.

    One thing I notice is your "difference" local variable on line 9 above is never used. Did you forget a step?
     
  3. masterpug13

    masterpug13

    Joined:
    Mar 31, 2019
    Posts:
    19
    I am trying to mimic the function I normally use for the player character, which is based off mouse position. I actually normalize it at the end, though I am not sure if that is needed.

    As an update, I had a sort of "EUREKA!" moment when I was picking my kid up from daycare. Instead of having a separate function, add those functions to the Update. That way they are updating every frame, which is sort of required for smooth movement. Turns out it worked like a charm. I had another issue where the anim got stuck, but you were right. Double checking values and events ended up making it all work.

    Huzzah! One of the core mechanics of the game now half-works. XD Thanks for the help!
     
    Kurt-Dekker likes this.