Search Unity

Projectile following where controller is pointing.

Discussion in 'Daydream' started by pfreema1, Apr 13, 2017.

  1. pfreema1

    pfreema1

    Joined:
    Apr 5, 2017
    Posts:
    21
    Hello!

    Inside Update() on the player I have this:

    Code (CSharp):
    1.         if (GvrController.ClickButton && Time.time > nextFire) {
    2.  
    3.             nextFire = Time.time + fireRate;
    4.  
    5.             GameObject bullet = Instantiate (Resources.Load ("Bullet", typeof(GameObject)),gun.transform.position, gun.transform.rotation, gun.transform) as GameObject;
    6.             //set bullets parent **maybe this is costly in terms of performance?**
    7.             //bullet.transform.SetParent(gun.transform);
    8.             Rigidbody rb = bullet.GetComponent<Rigidbody> ();
    9.  
    10.  
    11.  
    12.             Vector3 controllerDirection = GvrController.Orientation * Vector3.forward;
    13.  
    14.             //rb.AddExplosionForce (controllerDirection, gun.transform.position);
    15.  
    16.             //only add force ONCE!  not every Update!
    17.  
    18.             //original
    19.             rb.AddForce (controllerDirection * laserSpeed);
    20.  
    21.             //play sound and animation
    22.  
    23.             //destroy the bullet after 1 second
    24.             Destroy (bullet, 2);
    25.         }
    It shoots the object from the controller, but the object continues to follow where the controller is pointing. Any idea what could be wrong? I thought it would have something to do with AddForce adding force over and over in the direction the controller is pointing, but I'm not sure.
     
  2. thep3000

    thep3000

    Unity Technologies

    Joined:
    Aug 9, 2013
    Posts:
    400
    Your bullet is a child of the gun's transform.

    Try:

    Code (CSharp):
    1. bullet.GetComponent<Transform>().parent = null;
    after adding the force.
     
  3. pfreema1

    pfreema1

    Joined:
    Apr 5, 2017
    Posts:
    21
    That was it! Thank you very much!

    So this essentially makes it where the bullet is instantiated in the root right?

    edit: spelling