Hi all,
I’ve currently got a script that causes a Weapon to Instantiate a Projectile prefab, and then adds force to it, to make it shoot. Currently I’m just adding X force, because I reckoned that it only needs to fly straight. However, I failed to consider the possibility of the weapon being aimed at any angle other than straight ahead, meaning that when you shoot up, the bullet simply exits the barrel and flies…forward, instead of upward. This means that you can’t currently shoot above or beneath your character. I want to remedy this, but I’m not entirely sure how. The code in question is:
void Effect ()
{
Transform musketBall = Instantiate (MusketBall, firePoint.position, firePoint.rotation) as Transform;
musketBall.rigidbody2D.AddForce(new Vector2(projectileForce,0f));
Transform clone = Instantiate (muzzleFlashPrefab, firePoint.position, firePoint.rotation) as Transform; //CASTING the result of the Instantiate method into a Transform. We're doing this so that we can affect individual instances of MuzzleFlash, instead of the class as a whole.
clone.parent = firePoint; //The muzzleflash will be auto-set as the child of a firePOint
float size = Random.Range (0.6f, 0.9f);
clone.localScale = new Vector3 (size, size, size); //sets the local scale, which is the scale of the transform relative to its parent, (X, Y, Z) to each be equal to the randomised size of the explosion
Destroy (clone.gameObject, 0.04f); //deletes the muzzleFlash special effect after 0.02 seconds of existence - we don't want the muzzleflash to last too long, now, do you?
}
I considered adding Y force based on the y position of firePoint, but that didn’t seem to work properly. I also considered checking if the weapon is at a certain position (ie, if its position.y is less than 0 that means it’s beneath the player) and basing the y movement off of that, but that didn’t seem to work well either.
Any ideas?