0
\$\begingroup\$

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):

enter image description here

Here's what it looks like on the East (not working):

enter image description here

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.

\$\endgroup\$
1

1 Answer 1

0
\$\begingroup\$

When you move your projectiles like you did in this code:

transform.Translate (transform.forward * Time.deltaTime * speed);

it defaults into using Space.Self which takes into account all three position, rotation and scale of the object. this is why your code works on the west side but when rotating to the east side it no longer works.

so all you need to do is use Space.World in your transform.Translate method:

transform.Translate (transform.forward * Time.deltaTime * speed, Space.World);

this will move the object relative to unity's (0,0,0). this way no matter which direction your projectiles are facing they will always move forward.

\$\endgroup\$
1
  • \$\begingroup\$ Alternatively, you can keep using Space.Self, just specify the offset in local coordinates instead of world coordinates: transform.Translate(0, 0, Time.deltaTime * speed) \$\endgroup\$
    – DMGregory
    Commented Dec 30, 2020 at 21:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .