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
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);
}
}
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
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.
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 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?