2D make an arrow fall on an arc

Hello,

I am making a 2D game and have an enemy that is firing arrows every few seconds. I have this working perfectly, but the arrows do not fall correctly. They fall still in the same horizontal position they left the bow, rather than let the tip of the arrow drag them down to the ground.

I tried a few things like splitting the arrow into the 3 pieces and using different rigidbodys and fixed joints, implementing some code I saw in other threads, but couldn’t get anything to work.

If someone could help me out with this, I would really appreciate it. I have been stuck on it for hours :frowning:

I have reverted back to my original code before I tried all the different approaches:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fire_Arrow : MonoBehaviour
{
    [SerializeField]
    private float _arrowForce; // speed of arrow
    [SerializeField]
    private Transform _arrow;

    private void OnTriggerEnter2D(Collider2D target)
    {
        if (target.gameObject.tag == "FirePoint") // FirePoint = tag applied to Enemy -> SpawnPoint in Inspector
        {
            GetComponent<Rigidbody2D>().AddForce(transform.right * _arrowForce);
        }
    }

Many Thanks,

John

If you wat to try a “true” approach with two objects you need two, pne for the body and head, one for the tail, just having higher drag on the tail should make it behave like you want.

What i do with stuff like this is just snap/lerp transform.forward to rb.velocity.normalized

1 Like

If you’re looking for something more accurate as well for aiming etc. there is also the parabolic arc which there are quite a few videos and tutorials as well.

Hello,

Thank you for your response. This is what I tried. I created an empty gameobject and linked the script above to it. I then added my 3 images as children, with spriterenderer and rigidbody2d components on all 3. I also added fixedjoint2d components to the arrow head and body to link all 3 parts together.

I made this a prefab and tried to assign it to the script linked to my spawnpoint but I received an error “MissingComponentException: There is no ‘Renderer’ attached to the “Arrow(Clone)” game object…” so I added an empty SpriteRenderer to the gameobject. This allows the game to start, but no arrows are being fired.

I am not sure what I am doing wrong?

Many Thanks,

John

Hello,

I have tried a few different ones and even though the arrow moves on an arc, the arrow doesn’t rotate. If you know a good C# one for 2D, I would love to give it a go?

Many Thanks,

John

Hello,

I finally got it working. I will post the code below in case it helps someone else in future:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Arrow : MonoBehaviour
{
    [SerializeField]
    private float _arrowForce; // speed of arrow
    [SerializeField]
    private Transform _arrow;
    private Rigidbody2D _rb;
    private bool _followArc;
    private bool _firstFrame;

    private void Start()
    {
        _rb = GetComponent<Rigidbody2D>();
        _firstFrame = true;
        _followArc = true;
        Destroy(this.gameObject, 4.0f);
    }

    private void OnTriggerEnter2D(Collider2D target)
    {
        if (target.gameObject.tag == "FirePoint") // FirePoint = tag applied to Enemy -> SpawnPoint in Inspector
        {
            _rb.AddForce(transform.right * _arrowForce, ForceMode2D.Impulse);
        }
    }

    private void FixedUpdate()
    {
        if (_followArc && !_firstFrame)
        {
            transform.right = _rb.velocity; // this line makes the arrow follow the parabolic arc
        }
        _firstFrame = false;

        if (transform.position.y < -5.25f)
        {
            Destroy(gameObject);
        }

        if (!GetComponent<Renderer>().isVisible)
        {
            Destroy(gameObject);
        }
    }

    public void OnCollisionEnter2D(Collision2D collision)
    {
        _followArc = false;
    }
}