Projectile Breaking After Pickup

Matt-The-Slayer

New member
In my game, every arrow fired can be picked back up and added to your inventory.

To do this I combined the necessary projectile components with the necessary item pickup components. The process of firing and picking up arrows is working exactly as I want it to, however after picking up an arrow off the ground, the projectiles no longer reloading on my bow. This results in not being able to fire the bow after the already loaded arrow is fired.

Any direction I should look in?
 
I would take a look at ShootableWeapon.DetermineVisibleProjectile. This is what determines if the projectile is visible. The actual reloading is done within ShootableWeapon.StartItemReload.
 
Fixed!

I ended up coding my own script as I needed to add some complexity to it, such as certain layers I didn't want the arrow to act as an Item Pickup on (SubCharacter). The script is called by the Projectile - On Impact Event. Something about the way the separate ItemPickup prefab worked fixed the issue with the projectile not properly reloading into the bow.

Code:
using UnityEngine;

public class ProjectilePickup : MonoBehaviour
{
    // Spawns a Pickup object at the location of the projectile.

    [Tooltip("Collider ItemPickup will use as a trigger, can be a non trigger collider by default.")]
    [SerializeField] GameObject c_Pickup;
    [Tooltip("Should the pickup objects rotation be set to the rotation of the projectile object?")]
    [SerializeField] bool b_CopyRotation;
    [Tooltip("Destroy the projectile gameObject after the pickup object is spawned?")]
    [SerializeField] bool b_DestroyOnSpawn;
    [Tooltip("Delay destruction time after pickup is spawned. Only used if Destroy On Spawn is true.")]
    [SerializeField] float f_DestroyDelay = 0;

    public void SpawnPickupDelayed(float delay)
    {
        // Run the SpawnPickup method after a given delay
        // The delay can be helpful in ensuring the projectile gets parented to the gameObject that it stuck to before spawning the pickup object.
        Invoke("SpawnPickup", delay);
    }

    public void SpawnPickup()
    {
        GameObject parent = gameObject.transform.parent.gameObject;
        int parentLayer = parent.layer;
       
        // If parentLayer is of SubCharacter then we assume the projectile is attached to a character and we do not spawn an arrow pickup
        if (parentLayer == 30) { return; }

        // We cannot spawn the pickup object if its null
        if (c_Pickup == null) { return; }

        Quaternion rotation = Quaternion.identity;
        Vector3 position = gameObject.transform.position;

        // If b_CopyRotation, then set the rotation Quaternion to the projectiles current rotation.
        if (b_CopyRotation) { rotation = gameObject.transform.rotation; }

        // Create the ItemPickup
        Instantiate(c_Pickup, position, rotation, parent.transform);

        // If b_DestroyOnSpawn is false then we want to keep the original projectile alive.
        if (!b_DestroyOnSpawn) { return; }

        // Destroy the projectile after a given delay.
        Destroy(gameObject, f_DestroyDelay);
    }
}
 
Last edited:
Top