Flying bug movement mechanics

Hi to all,

I’m working on a game where there will be some flying insects (bees, mainly) and I’m wondering if there is a kind o math formula to move these objects correctly in a 2D space.

I’m thinking about some randomness in up & down plus some dynamic in horizontal like following a path and flip back at a random point.

Any hint?

Thanks!

Hi,

Welcome to our community! :slight_smile:

What do you mean by “correctly”? If you want to move the insects via the physics simulation, you could use the AddRelativeForce method of the Rigidbody2D component. In the real world, insects drop because of the gravity, so the force they create with their wings (or whatever) has to work against the gravity to keep them in the air.

If you want to achieve some “randomness” in the movement, you could, for example, occasionally call AddRelativeForce instead of calling it all the time. Random.Range could be interesting.

Start simple: Make the bees move with the Rigidbody2D. Then refine their movement until you are happy with the result.

Please feel free to ask our helpful community of students for further advice over on our Discord chat server.

Good luck! :slight_smile:


See also:

Hi Nina, thanks for your reply,

After some attempts, here we have the result:

public class BeeFlight : MonoBehaviour
{
    [SerializeField] float speed = 1.0f;

    Vector3 newPosition;

    void Start()
    {
        PositionChange();
    }

    void PositionChange() {
        newPosition = new Vector2(Random.Range(-5.0f, 5.0f), Random.Range(-5.0f, 5.0f));

        if (newPosition.x > transform.position.x) {
            transform.localScale = new Vector3(-1, 1, 1);
		} else {
            transform.localScale = Vector3.one;
		}
	}


    void Update()
    {
        if (Vector2.Distance(transform.position, newPosition) < 1) {
            PositionChange();
		}

        transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * speed);
    }
}

The sprite now fly randomly from one point to another, facing the right direction, but it moves in line, without some “up and down” that insects usually do while flying

I have to try to add the Rigidbody2D component adding continuously some force from down to up, I think, like you suggested in your reply… uhm… :thinking:

Here we have the last version

It works well but could be better… Maybe I have to addForce to the RigidBody even for the horizontal movement…

public class BeeFlight : MonoBehaviour
{
    [SerializeField] float speed = 1.0f;
    [SerializeField] float flapThrust = 0.3f;

    Rigidbody2D rb;

    Vector3 newPosition;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        PositionChange();
    }

    void PositionChange()
    {
        newPosition = new Vector2(Random.Range(-5.0f, 5.0f), Random.Range(-5.0f, 5.0f));

        if (newPosition.x > transform.position.x)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }
        else
        {
            transform.localScale = Vector3.one;
        }
    }

    IEnumerator Flap()
    {

        rb.AddForce(transform.up * Random.Range(0, flapThrust), ForceMode2D.Impulse);

        yield return new WaitForSeconds(Random.Range(0.2f, 0.5f));

    }

    void Update()
    {
        if (Vector2.Distance(transform.position, newPosition) < 1)
        {
            PositionChange();
        }

        transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * speed);

        StartCoroutine(Flap());

    }
}

Good job so far! :slight_smile:

Does your code really work as expected/intended? Update gets called multiple times per second. Unless I’m missing something, the StartCoroutine method call is not wrapped in an if-statement. This means that the code starts a new coroutine 60 times per second if you have a framerate of 60.

Ops, my bad!

That’s the right script and it works well

Thanks again :hugs:

public class BeeFlight : MonoBehaviour
{
    [SerializeField] float speed = 1.0f;
    [SerializeField] float flapThrust = 0.3f;

    Rigidbody2D rb;

    Vector3 newPosition;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        PositionChange();
        StartCoroutine(Flap());
    }

    void PositionChange()
    {
        newPosition = new Vector2(Random.Range(-5.0f, 5.0f), transform.position.y);

        if (newPosition.x > transform.position.x)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }
        else
        {
            transform.localScale = Vector3.one;
        }
    }


    IEnumerator Flap()  {
        while(true) {
            rb.AddForce(transform.up * Random.Range(0, flapThrust), ForceMode2D.Impulse);
            Debug.Log("Flapping...");
            yield return new WaitForSeconds(Random.Range(0.2f, 0.5f));
        }
        

    }

    void Update()     {
        if (Vector2.Distance(transform.position, newPosition) < 1)    {
            PositionChange();
        }

        transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * speed);

    }
}

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms