Hey guys,
I’m working on a 2D top down space sim, and I’m implementing the firing of projectiles. For my projectile, I have the following code in my script:
public float speed;
void Start()
{
GetComponent<Rigidbody2D>().velocity = transform.up * (speed);
}
The only problem, is that if the ship is moving faster than the specified projectile speed, the projectile just falls back and hits the ship. So my initial solution was to add the ship’s velocity to that of the projectile, like this:
GetComponent<Rigidbody2D>().velocity = transform.up * (speed + shipScript.currentVelocity);
This works alright, but only if the ship is moving forward when firing. If it is firing off to the side, the speed of the projectile will not look right, as it will seem to fire much faster from the ship.
What I’m trying to figure out is how to add the ship’s vector to the projectile’s vector, so that not only the velocity is added, but also the direction. Can anyone show me the correct way to do that?