I am dropping bombs from a spaceship orbiting a planet. My camera is affixed to the ship.
I fire bombs by instantiating a bomb at the position of the camera, which is at all times facing toward the planet, however, the bomb is not intended to move directly toward the planet center. Rather, there is a movable crosshair between the camera and the planet and I want the bomb to move from the camera in the direction of the crosshair. My bomb script looks like this:
public class bomb : MonoBehaviour {
public float speed;
GameObject crosshair;
// Use this for initialization
void Start () {
crosshair = GameObject.Find ("Crosshair");
transform.SetParent (null);
transform.LookAt (crosshair.transform);
}
// Update is called once per frame
void Update () {
transform.Translate (transform.forward * Time.deltaTime * speed);
}
}
My expectation is that the bomb instantiates at the camera, disassociates from its parent (the camera), turns to face the crosshair and then moves in that direction until it hits the planet.
But that's not what happens.
For reasons unknown, the projectile travels due left of the camera. So I adjust my projectile to move transform.right instead of transform.forward. And this works - half of the time.
Here's what it looks like on the West (working):
Here's what it looks like on the East (not working):
The projectiles are kind of small, but trust me, they're there.
As mentioned, my ship is orbiting the planet. If it is orbiting on one hemisphere (0:180), it shoots toward the planet, but if it is on the other hemisphere(0:-180), it shoots directly away from the planet. I can't explain this, because the camera continuously looks toward the planet, and the crosshair is always between the two, and the bomb disassociates from them anyway.
Transform.Translate
— in particular, theSpace
parameterrelativeTo
and what happens when it's omitted as you've done here. \$\endgroup\$