2D weapon pickup bob

Hey everyone, its been a while since I’ve posted here.

I have a 2d top down shooter and i want the weapon and pickup sprites to repeatedly bob up and down in place where they spawn to highlight them as collectable objects. Must they have a rigidbody to move like that or can you just Translate the objects in game space? Perhaps Lerp? i don’t really know how to use Lerp yet.

No, you won’t need a Rigidbody2D.

For something like this I would use an animation instead, much simpler. Either animate its position or size to get it to be noticeable.

1 Like

And make it rotate, bounce, sparkle and emit some small quality looking particles — you know, to make sure it is highlighted and the player sees it. :wink:

You should be able to manipulate the translate component. An animation is propably what you are searching for, but you could apply this code to your weapon to make it scale up and down if that is what you are looking for :slight_smile:

using UnityEngine;

public class EffectScript : MonoBehaviour {

    float scaleSpeed = 3f;
    float maxScale = 2f;
    float minScale = 1f;
    float currentScale = 1f;
    float scaleDirection = 1f;
   
    void Update () {

        //Check if the scale limits are reached, if so change direction
        if (currentScale <= minScale) {
            scaleDirection = 1;
        }
        else if (currentScale >= maxScale) {
            scaleDirection = -1;
        }

        //Calculate the new scale
        currentScale += scaleSpeed * scaleDirection * Time.deltaTime;
  
        //Apply scale to X and Y
        this.transform.localScale = new Vector3(currentScale,currentScale, 0);

    }
}

Or not in the update but with a coroutine:

2902270--213691--bop.gif

public class Bop : MonoBehaviour {

    public float timeToUp;
    public Vector3 startPos;
    public Vector3 endPos;
    private Transform myTransform;
   
    // Use this for initialization
    void Start () 
    {
        myTransform = GetComponent<Transform>();
        StartCoroutine("BopUpDown");
    }
   
    IEnumerator BopUpDown()
    {
        float timer = 0;
        bool flip = false;
        while(true)
        {
            myTransform.position = Vector3.Lerp(startPos,endPos,timer/timeToUp);
           

            if(timer < timeToUp && !flip)
            {
                timer += Time.deltaTime;
            }
            else if(timer > 0 && flip)
            {
                timer -= Time.deltaTime;
            }
            else
            {
                flip = !flip;
            }
            yield return null;
        }
    }
}