Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Issue with LeanTween movement script

Discussion in 'Scripting' started by mariogutierrez, Sep 14, 2020.

  1. mariogutierrez

    mariogutierrez

    Joined:
    Feb 11, 2019
    Posts:
    3
    Hello everyone, thanks in advance for your help.

    The script below uses leantween to smoothly make an object go from one side of the screen to the other on the x axis. When it reaches the max position on either side it makes the object go down on the z axis by 0.75f and then go back to the other side. It works fine. However when I introduce a leantween "wobble" up and down while its moving from side to side, once it gets to the max positions it wont go down on the Z axis like instructed. Any ideas or suggestions?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Lv1aEnemyMove : MonoBehaviour
    6. {
    7.     private Transform enemyHolder;
    8.     public float speed;
    9.  
    10.     void Start()
    11.     {
    12.    
    13.         enemyHolder = GetComponent<Transform>();
    14.         LeanTween.moveX(gameObject, 8, speed).setLoopPingPong();
    15.  
    16. // this is the line that's giving me issues. If I comment it, the movement works like its supposed
    17. // if I leave it in, my object will go from side to side but wont do the down move at the edges.
    18.         LeanTween.moveZ(gameObject, (enemyHolder.position.z - 0.5f), 0.5f).setLoopPingPong();
    19.  
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.                 if (enemyHolder.position.x >= 8 || enemyHolder.position.x <= -7.8)
    25.             { enemyHolder.position += Vector3.back * 0.75f;  }
    26.     }
    27.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    8,973
    You need to somehow blend the value for the up-down wobble with the other go-down-the-side logic.

    You can do this mathematically in code, or you can make a sub-GameObject so you have two transforms to modify.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,508
    There are a few different options. I don't know the exact effect you're going for when you say "wobble", but Leantween has .SetOnComplete and a .SetOnUpdate that might help you. This way you can have it tween to a spot and when the tween is done, it triggers some other code. Or, you can have it trigger code during Updates as it's tweening.

    Another option is instead of using moveX or moveZ you can use .value, which lets you tween between two values and as it tweens you can do something with the value (like applying it to movement, or whatever you want really)

    Setting up the tweens differently may help you get a result closer you to what you want.
     
  4. mariogutierrez

    mariogutierrez

    Joined:
    Feb 11, 2019
    Posts:
    3
    I really appreciate your input guys. Will test tonight and update y'all. :)
     
  5. mariogutierrez

    mariogutierrez

    Joined:
    Feb 11, 2019
    Posts:
    3
    I managed to make it work. Basically I did use two separate transforms (parent and child). The parent performs the up'n'down wiggle as an empty game object and the child contains the prefab and does the side to side of the screen motion. Tried the .SetOnComplete and a .SetOnUpdate but it seems that the Vector3.back I was using overrode it or something. But thanks again for your advice.
     
    Kurt-Dekker likes this.
unityunity