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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trying to get a prefab to disappear when firing a gun - C#

Discussion in 'Scripting' started by AlyxSokarad, Oct 2, 2016.

  1. AlyxSokarad

    AlyxSokarad

    Joined:
    Sep 25, 2016
    Posts:
    2
    Hello all! This is what I am trying to achieve:

    The player is holding a pistol. When the player fires the gun, I want the bullet to disappear for 2 seconds and then reappear, ready for the next shot. Like an Russian RPG in certain games.

    On the gun gameobject, there is a separate bullet prefab that is a child of the gun gameobject. I thought about using blender to make an animation that would hide the bullet but I went with scripting instead because I thought it would be easier.

    Here is the script that is attached to Controller (right): Pastebin.

    I don't have a strong background in C# so any help and critiques are welcome. Thanks!
     

    Attached Files:

  2. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    You can use coroutine (a lot more easyer for timer things)

    (not sure about the syntax you can have some syntax errors :) )
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ControllerManager : MonoBehaviour {
    6.  
    7.     public Rigidbody BulletPrefab;
    8.     public Transform BarrelEnd;
    9.     public float MyForce;
    10.     public float timer;
    11.     public GameObject PlayerBullet;
    12.     Animation otherAnimator;
    13.  
    14.     private Valve.VR.EVRButtonId triggerButton = Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger;
    15.     private SteamVR_TrackedObject trackedObject;
    16.     private SteamVR_Controller.Device device;
    17.  
    18.     void Start () {
    19.         trackedObject = GetComponent<SteamVR_TrackedObject> ();
    20.         timer = 0;
    21.     }
    22.  
    23.     void Update () {
    24.    
    25.  
    26.    
    27.         if(device.GetPressDown(triggerButton))
    28.         {
    29.            StartCoroutine(TimerShowBullet());
    30.         }
    31.     }
    32.  
    33.     IEnumerator TimerShowBullet()
    34.     {
    35.      device = SteamVR_Controller.Input ((int)trackedObject.index);
    36.        yield return new WaitForSeconds(2);
    37.         device.TriggerHapticPulse (700);
    38.         otherAnimator = PlayerBullet.GetComponent<Animation> ();
    39.         Rigidbody bulletInstance;
    40.         bulletInstance = Instantiate (BulletPrefab, BarrelEnd.position, BarrelEnd.rotation) as Rigidbody;
    41.         bulletInstance.AddForce (BarrelEnd.forward * MyForce);
    42.     }
    43. }
    44.  
    45.  
     
  3. yepfuk

    yepfuk

    Joined:
    Sep 23, 2015
    Posts:
    67
    What do you mean by "dissapear and reappear" ?
     
  4. AlyxSokarad

    AlyxSokarad

    Joined:
    Sep 25, 2016
    Posts:
    2
    Sorry, I should have elaborated. I would like to turn the mesh renderer off for two seconds and turn it back on again.
     
  5. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Why don't you create a simple behaviour script that does this specific task and attach it to your bullet prefab?

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class BulletHideBehaviour : MonoBehaviour
    4. {
    5.    [SerializeField]
    6.    private float disappearTime;//the amount of time the renderer is disabled
    7.    private float appearTime;//the time when the bullet should re-appear
    8.    private MeshRenderer meshRenderer;
    9.  
    10.    void Start()
    11.    {
    12.      this.meshRenderer = this.GetComponent<MeshRenderer>();
    13.      if (this.meshRenderer == null)
    14.      {
    15.        throw new UnityException("Missing MeshRenderer! BulletHideBehaviour requires a MeshRenderer component to function correctly.");
    16.      }
    17.      this.meshRenderer.enabled = false;
    18.      this.appearTime = Time.time + this.disappearTime;
    19.    }
    20.  
    21.    void Update()
    22.    {
    23.      if (Time.time >= this.appearTime && this.meshRenderer != null)
    24.      {
    25.        this.meshRenderer.enabled = true;
    26.        this.enabled = false;
    27.      }
    28.    }
    29. }
     
    Last edited: Oct 4, 2016