- Home /
How do I fire a 2D projectile based on it's current angle/location.
I have a project for class in which I have to make a turret constantly rotate and occaisonally shoot a bullet from the barrel of the turret.
each rotation does the following below.
         turretObject.transform.eulerAngles = new Vector3(turretObject.transform.eulerAngles.x,turretObject.transform.eulerAngles.y,newZ);
         GameObject tempBullet = Instantiate (projectile, projectileLoc, Quaternion.identity) as GameObject;
         tempBullet.transform.eulerAngles = projectileAngle;
in my projectile script(the script attatched to my bullet) every 50Ms I have a corrutine doing this.
     IEnumerator projectileTick(float ms)
     {
         yield return new WaitForSeconds (ms / 1000);
         lifeTime -= 50f;
         if (lifeTime == 0f && currentProjectile != null) 
             Destroy (currentProjectile);
 
 
         currentProjectile.rigidbody2D.velocity = -transform.right * 5;
         print ("here");
         if (currentProjectile != null) 
             StartCoroutine (projectileTick (50));
         
     }
the result is this

I want the bullet to fire based on the angle its facing however I cannot seem to get that to work properly any help would be appreciated.
I've recently done something similar, I think. $$anonymous$$aybe you can try this: Give the end of the barrel an empty child that is used solely for the purpose of it's transform. Whenever you instantiate a bullet, you would make it spawn at that child's position with that child's rotation. Your bullet's instruction could be literally as simple as transform.up * whatever (assu$$anonymous$$g you place the rotation to point "up"). If this is along the lines of what you're looking for and you would like more clarification or a code example, I'll be more than glad to help.
Answer by KingFriggy · Jun 14, 2015 at 01:59 PM
 Public GameObject Cannon;
 
 Private Vector3 Vel;
 Private float speed = 5f;
 
 Void Start()
 {
     Vel = Cannon.transform.eulerAngles.normalize() * speed;
 }
Pass the Cannon object to the projectile. Then use the Vel variable above as the projectile velocity.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                