# Orbiting Bullets

**URL:** https://discussions.unity.com/t/orbiting-bullets/63413
**Category:** Questions & Answers
**Created:** [February 16, 2013, 3:29pm UTC](https://discussions.unity.com/t/orbiting-bullets/63413 "2013-02-16T15:29:34Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![DiscGolfer17](https://avatars.discourse-cdn.com/v4/letter/d/9f8e36/32.png) [@DiscGolfer17](https://discussions.unity.com/u/DiscGolfer17)
#### Post date: [February 16, 2013, 3:29pm UTC](https://discussions.unity.com/t/orbiting-bullets/63413/1 "2013-02-16T15:29:34Z")

</div>

I have a ship that is orbiting an object, and I need to now make it shoot bullets. I am having trouble getting this to work. I currently have my ship a child of a pivot point (0,0,0) and I have a gun a child of the ship. I also have two on-screen virtual joysticks (one controls the ship movement and the other controls the direction of the gun)…

I want to make it so that when the gun joystick is touched, that it starts shooting bullets in that direction and the bullets would orbit until they hit something, or their lifetime has elapsed.

If you would like a little more information, feel free to ask! Thanks!

**Edit:**

Here is what I have so far in my hierarchy:

- Pivot
  - Camera
  - Ship
    - Gun
      - BulletSpawnPoint

The pivot point is the center of my planet (0,0,0). The Camera and ship orbit around the planet as one joystick is moved. The gun is controlled by a different joystick. Bullets are spawned at the bullet spawn point when fired.

Here is my code attached to the gun (The gun turns fine until the ship is moved to the other side of the planet and then it seems some signs get swapped):

```
        // If the gun joystick is touched and the bullet interval has elapsed
		if (GunJoystick.IsFingerDown() && ((Time.time*1000) - lastBulletTime) > BULLET_INTERVAL)
		{
			// Rotate the gun according to the joystick angle
			currentAngle = (Mathf.Atan2(-GunJoystick.position.y, -GunJoystick.position.x) * (180/Mathf.PI)) + 90;
			thisTransform.RotateAroundLocal(Vector3.forward, (Mathf.DeltaAngle(lastAngle, currentAngle)*Mathf.Deg2Rad));
			lastAngle = currentAngle;
			
			// Get the next bullet
			currentBullet = bullets[nextBullet++];
			
			// Check if we have used the last bullet and wrap around
			if (nextBullet >= bullets.Length)
			{
				nextBullet = 0;
			}
			
			// Set the bullet values
			currentBullet.bullet.SetActiveRecursively(true);
			currentBullet.bullet.transform.position = thisTransform.position;
			currentBullet.bullet.transform.rotation = thisTransform.localRotation;
			currentBullet.startTime = Time.time * 1000;
			currentBullet.bullet.GetComponent<BulletController>().direction.Set(GunJoystick.position.y, -GunJoystick.position.x, 0.0f);
			
			// Set the last time a bullet was shot from the gun
			lastBulletTime = currentBullet.startTime;
		}

```

Here is the script attached to the bullet prefab, and it appears that something is wrong with this because the bullets don’t orbit correctly:

```
public class BulletController : MonoBehaviour 
{
	public Vector3 direction;
	private Transform thisTransform;
	private const float BULLET_SPEED = 200;
	
	public void Start()
	{
		thisTransform = transform;
		direction = new Vector3();
	}
	
	public void Update()
	{
		if (gameObject.active)
		{
			//transform.Rotate(Direction.x + 100 * Time.deltaTime, Direction.y + 100 * Time.deltaTime, 0.0f);
			thisTransform.RotateAround(Vector3.zero, direction, BULLET_SPEED * Time.deltaTime);
			//thisTransform.Rotate(direction.y * BULLET_SPEED * Time.deltaTime, -direction.x * BULLET_SPEED * Time.deltaTime, 0.0f);
		}	
	}
}

```

---

<div class="post-metadata">

### Author: ![robertbu](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@robertbu](https://discussions.unity.com/u/robertbu)
#### Post date: [February 16, 2013, 5:02pm UTC](https://discussions.unity.com/t/orbiting-bullets/63413/2 "2013-02-16T17:02:35Z")

</div>

I would make an empty game object a child of the gun and place it just past the end of the gun (with the same rotation). This will be your spawn point. You can use [Rigidbody.AddRelativeForce()](http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddRelativeForce.html) using Vector3.forward to shoot the bullet.

I’m not sure how you want to handle this. If you want any sort of realism, note that not all trajectories will result in a orbit. For example, imagine the gun point directly toward the planet. What I would start with is a script on the bullet that add a small force every frame towards the planet. You’ll have to play to get this force just right (and it will depend on the force you use to shoot your bullets). You have the bullet position and the planet center position, so figuring the direction of that force would be easy. This way bullets pointed down or out into space will behave as expected, and other trajectories will have a chance at an orbit.

In the few cases I tried, I often had trouble using balanced forces to achieve something stable (in my case a hovercraft), so you may have trouble getting a stable orbit. If the lifespan is short for the bullet, then this may not matter, but if it does matter, you can adjust the position of the bullet to keep a stable altitude at each frame. I believe you want to use Rigidbody.MovePosition() to reset the position rather than directly modifying the transform.
