Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Moving projectiles in VrChat

Discussion in 'Physics' started by TrueMB, Dec 31, 2018.

  1. TrueMB

    TrueMB

    Joined:
    Sep 24, 2017
    Posts:
    3
    Hello together!

    I tried a Long Time now to shoot a projectile.... But I got no Idea how to make the model, so that it does not move with the View of the Player.

    I know it should not be in the Parent Folder, but I got No Idea how to get the Position of the hand, and and and....

    I appreciate any sort of help :)

    Happy new year!
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You mean a graphical model to display? not quite clear on what you mean.
    (you can just use a sphere/cylinder for now)

    you mean the bullet shouldn't be a child object of the gun? if so, yes.

    I'll assume you're wanting to shoot an actual projectile rather than fire a weapon with raycasting (us being in the physics section and all)

    What you need to do is break the problem into smaller problems
    1) we need to create the projectile where the end of the gun barrel is, preferably with it "looking" straight out.
    2) we need to make it go. (Let's go alreadyyy!)


    I also assume you know the basics of references and instantiation else you're diving too deep too soon.
    for the first problem you need a script(doesn't matter where it is, could be on the gun, could be on the player, some manager on an empty GO, your pick) that have a reference to the game object that will act as the bullet, have a reference to a transform at the gun's muzzle(end of the barrel, what I tend to do is just use the muzzle flash particle system's transform for this, already in the right spot, saves us a GO)

    Now what we can do is something like this
    Code (CSharp):
    1. GameObject projectilePrefab;
    2. Transform muzzle;
    3.  
    4. Instatiate(projectilePrefab, muzzle.position, muzzle.rotation);
    Now you have the projectile where you need it, now it's time to move it, you'll need to have some force factor, that is up to you what you wanna use, i'll use muzzle velocity in my example.(note that the ForceMode matters alot, https://docs.unity3d.com/ScriptReference/ForceMode.html)

    All you need to do is go like this:
    Code (CSharp):
    1. GameObject projectilePrefab;
    2. Transform muzzle;
    3. float muzzleVel;
    4.  
    5. ((GameObject)Instatiate(projectilePrefab, muzzle.position, muzzle.rotation)).GetComponent<RigidBody>().AddRelativeForce(0.0f,0.0f,muzzleVel, ForceMode.VelocityChange);
    I hope this isn't a head F*** for you, but i'm a one-liner, you can break this up to:
    Code (CSharp):
    1. GameObject go = Instantiate(projectilePrefab, muzzle.position, muzzle.rotation);//spawn the projectile
    2. Rigidbody rb = go.GetComponent<RigidBody>();//get it's rigidbody
    3. rb.AddRelativeForce(0.0f,0.0f,muzzleVel,ForceMode.VelocityChange);//apply force
    also, happy new year ;)
     
  3. TrueMB

    TrueMB

    Joined:
    Sep 24, 2017
    Posts:
    3
    I really appreciate your help so far. I think I will do this after some sleep, because i got no idea who I am.

    Little explanation: I want to shoot a Rocket with my VR (Oculus Rift) in VrChat.
    While shooting the Rocket there will be a Sound. Also after 1 Second there will be another Sound and Particle Effects.

    The hardest Part is most likely how to shoot the rocket with vr....


    Also the Rocket is a FBX.


    EDIT: Is it possible to start the skript in an Animation?
     
    Last edited: Jan 1, 2019
  4. TrueMB

    TrueMB

    Joined:
    Sep 24, 2017
    Posts:
    3
    UPDATE: I found a little script and edit it. So it fits for me.
    But I got no idea how to change the Input, so that i can use it with my VR (Oculus Rift)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class launchProjectiles : MonoBehaviour
    5. {
    6.     //Drag in the Bullet Emitter from the Component Inspector.
    7.     public GameObject Rocket_Emitter;
    8.  
    9.     //Drag in the Bullet Prefab from the Component Inspector.
    10.     public GameObject Rocket;
    11.  
    12.     //Enter the Speed of the Bullet from the Component Inspector.
    13.     public float Rocket_Forward_Force;
    14.  
    15.     //Enter the Time of Life of the Bullet from the Component Inspector.
    16.     public float Rocket_Destroy_Time;
    17.  
    18.     //Delay of the start of the End.
    19.     public float Delay_Time_End;
    20.  
    21.     //ParticleSystems
    22.     public ParticleSystem ParticleSystem1;
    23.     public ParticleSystem ParticleSystem2;
    24.     public ParticleSystem ParticleSystem3;
    25.     public ParticleSystem ParticleSystem4;
    26.  
    27.  
    28.     //Sounds
    29.     public AudioSource AudioExplosion;
    30.     public AudioSource AudioStart;
    31.  
    32.     // Use this for initialization
    33.     void Start ()
    34.     {
    35.     }
    36.    
    37.     // Update is called once per frame
    38.     void Update ()
    39.     {
    40.         if (Input.GetKeyDown("space"))
    41.         {
    42.            
    43.             //The Bullet instantiation happens here.
    44.             GameObject Temporary_Rocket_Handler;
    45.             Temporary_Rocket_Handler = Instantiate(Rocket, Rocket_Emitter.transform.position, Rocket_Emitter.transform.rotation);
    46.  
    47.             //Sometimes bullets may appear rotated incorrectly due to the way its pivot was set from the original modeling package.
    48.             //This is EASILY corrected here, you might have to rotate it from a different axis and or angle based on your particular mesh.
    49.             Temporary_Rocket_Handler.transform.Rotate(Vector3.left * 90);
    50.  
    51.             //Retrieve the Rigidbody component from the instantiated Bullet and control it.
    52.             Rigidbody Temporary_RigidBody;
    53.             Temporary_RigidBody = Temporary_Rocket_Handler.GetComponent<Rigidbody>();
    54.  
    55.             //Tell the Rocket to be "pushed" forward by an amount set by Rocket_Forward_Force.
    56.             Temporary_RigidBody.AddForce(transform.forward * Rocket_Forward_Force);
    57.  
    58.             //Basic Clean Up, set the Bullets to self destruct after 10 Seconds, I am being VERY generous here, normally 3 seconds is plenty.
    59.  
    60.             // Destroy(Temporary_Rocket_Handler, Rocket_Destroy_Time);
    61.  
    62.  
    63.  
    64.             //Start Sound
    65.             ((GameObject)AudioStart.gameObject).SetActive(true);
    66.             AudioStart.Play();
    67.  
    68.             //Invoke("Explosion", Rocket_Destroy_Time);
    69.             StartCoroutine(Explosion(Temporary_Rocket_Handler, Rocket_Destroy_Time));
    70.        }
    71.     }
    72.  
    73.     public IEnumerator Explosion(GameObject go, float delay)
    74.     {
    75.  
    76.         yield return new WaitForSeconds(delay);
    77.  
    78.         //Position Set and play Particle
    79.         ((GameObject)ParticleSystem1.gameObject).SetActive(true);
    80.         ParticleSystem1.transform.position = go.transform.position;
    81.         ParticleSystem1.transform.rotation = go.transform.rotation;
    82.         ParticleSystem1.Play();
    83.  
    84.         ((GameObject)ParticleSystem2.gameObject).SetActive(true);
    85.         ParticleSystem2.transform.position = go.transform.position;
    86.         ParticleSystem2.transform.rotation = go.transform.rotation;
    87.         ParticleSystem2.Play();
    88.  
    89.         ((GameObject)ParticleSystem3.gameObject).SetActive(true);
    90.         ParticleSystem3.transform.position = go.transform.position;
    91.         ParticleSystem3.transform.rotation = go.transform.rotation;
    92.         ParticleSystem3.Play();
    93.  
    94.  
    95.         ((GameObject)ParticleSystem4.gameObject).SetActive(true);
    96.         ParticleSystem4.transform.position = go.transform.position;
    97.         ParticleSystem4.transform.rotation = go.transform.rotation;
    98.         ParticleSystem4.Play();
    99.  
    100.         //Destroy GameObject
    101.         Destroy(go);
    102.  
    103.         ((GameObject)AudioExplosion.gameObject).SetActive(true);
    104.         AudioExplosion.Play();
    105.  
    106.         //Start End
    107.         //Invoke("End", Delay_Time_End);
    108.     }
    109.  
    110.     /*
    111.     public IEnumerator End(float delay)
    112.     {
    113.  
    114.         yield return new WaitForSeconds(delay);
    115.  
    116.         ParticleSystem1.Stop();
    117.         ParticleSystem2.Stop();
    118.         ParticleSystem3.Stop();
    119.         ParticleSystem4.Stop();
    120.     }
    121.     */
    122. }