Instantiating A Bullet Casing

Hey guys glad that you are here and thanks for any help! :slight_smile:

Either way lets get to my question I am working on a fps project and I have come to a point where as the player shoots I want to spawn a bullet casing I did this a while ago on a prototype project where it worked perfectly but since I deleted the project and can’t remember the way I did it I tried remaking it out of nothing and there is issues which is why I am here at first here is the code for the bullet casing and the weapon spawning the bullet casing. Then the question will be below them.

This is the script attached to my bullet casing prefab.

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

[RequireComponent(typeof(BoxCollider))]
[RequireComponent(typeof(Rigidbody))]
public class CasingHandler : MonoBehaviour
{
    [Header("Variables")]
    public float Velocity = 5;
    public float MinumumRotation = 5f;
    public float MaximumRotation = 15f;

    private Rigidbody rb;

    void Start()
    {
        gameObject.transform.parent = null;
        rb = gameObject.GetComponent<Rigidbody>();
    }



    private void FixedUpdate()
    {
        rb.velocity = transform.TransformDirection(Vector3.right * Velocity);
    }


    private void OnCollisionEnter(Collision collision)
    {
        Destroy(gameObject);
    }
}

Here is my script where I instantiate the bullet at a empty gameobject.

    void InstantiateBulletCasing()
    {
        Instantiate(BulletCasing, CasingPoint.position, Quaternion.identity);
    }

So the issue is once the bullet casing is spawned it just starts floating to where it’s right transform point is what I wanted to do is add an impulse at the start to the right of the bullet to eject it but it doesn’t matter where my player looks the casing will float to the original right axis. I will upload a video below to demonstrate the issue and for the life for me I can’t figure it out if any one has a idea please explain it to me I want to learn and make sure I fix this thank you so much for your time I hope you can help!

The video :

It looks to me like you’re not orienting the bullet casing relative to the gun when you instantiate. You probably want to set the bullet casing’s initial rotation to either CasingPoint.rotation or the gun’s rotation. Then it probably just works.

Right now you are setting it to Quaternion.identity, which will orient them all the same relative to worldspace, so they will all fly in the same direction, because Vector3.right is all the same direction.

My guess at least.

Instantiate(BulletCasing, CasingPoint.position, CasingPoint.rotation);

Thank you very much that seems to be the issue I have no idea why I didn’t think of that thanks a lot man!

1 Like