Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Instantiating A Bullet Casing

Discussion in 'Scripting' started by Tengoz, Feb 26, 2021.

  1. Tengoz

    Tengoz

    Joined:
    Dec 14, 2020
    Posts:
    24
    Hey guys glad that you are here and thanks for any help! :)

    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.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(BoxCollider))]
    6. [RequireComponent(typeof(Rigidbody))]
    7. public class CasingHandler : MonoBehaviour
    8. {
    9.     [Header("Variables")]
    10.     public float Velocity = 5;
    11.     public float MinumumRotation = 5f;
    12.     public float MaximumRotation = 15f;
    13.  
    14.     private Rigidbody rb;
    15.  
    16.     void Start()
    17.     {
    18.         gameObject.transform.parent = null;
    19.         rb = gameObject.GetComponent<Rigidbody>();
    20.     }
    21.  
    22.  
    23.  
    24.     private void FixedUpdate()
    25.     {
    26.         rb.velocity = transform.TransformDirection(Vector3.right * Velocity);
    27.     }
    28.  
    29.  
    30.     private void OnCollisionEnter(Collision collision)
    31.     {
    32.         Destroy(gameObject);
    33.     }
    34. }
    Here is my script where I instantiate the bullet at a empty gameobject.
    Code (CSharp):
    1.     void InstantiateBulletCasing()
    2.     {
    3.         Instantiate(BulletCasing, CasingPoint.position, Quaternion.identity);
    4.     }
    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 :
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.

    Code (csharp):
    1. Instantiate(BulletCasing, CasingPoint.position, CasingPoint.rotation);
     
  3. Tengoz

    Tengoz

    Joined:
    Dec 14, 2020
    Posts:
    24
    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!
     
    Joe-Censored likes this.