How could I add "gravity" to a top-down shooter's bullet, such as in The Binding of Isaac?
I am currently working on a 2D top down twin-stick shooter with similar mechanics to The Binding of Isaac. One thing I've noticed in Isaac is that, although you can only shoot in 4 directions, the bullets' direction is influenced by the movement of the player, which allows for a little more strategic shooting. I uploaded a video to exemplify that, notice how I can reach areas that are beyond where my player can walk. I want to do that in my game as well, but I don't know how. Currently my shooting script looks like this:
void Shot()
{
GameObject bullet = Instantiate(bulletPrefab, tipOfGun.position, tipOfGun.rotation);
Rigidbody2D bulletRb = bullet.GetComponent<Rigidbody2D>();
bulletRb.AddForce(tipOfGun.up * bulletSpeed, ForceMode2D.Impulse);
}
The shot comes out of an object called "tipOfGun" that is a child of the gun object, which is, in turn, a child of the player object. The player movement is equally simple:
private Vector3 GetMovementFromInput()
{
var posX = horizontal * playerSpeed * Time.deltaTime;
var posY = vertical * playerSpeed * Time.deltaTime;
var posZ = 0;
var vMovement = new Vector3(posX, posY, posZ);
return vMovement;
}
void FixedUpdate()
{
if (horizontal != 0 && vertical != 0) // Checks if moving diagonally
{
// limits diagonal movement speed to 70%
horizontal *= velLimit;
vertical *= velLimit;
}
rb.AddForce(vMovement.normalized * playerSpeed, ForceMode2D.Impulse);
}
The only way I can think of doing this is taking account of the player's speed and direction and adding an additional force to the bullet according to that, but I wonder if there's a more straightforward way, using physics.
Your answer
Follow this Question
Related Questions
Bubble Bubble with unity 0 Answers
Sensing an objects collisions by another object 0 Answers
Shooting mechanic - can't shoot left and right,2D shooting mechanic - can't shoot lift and right 0 Answers
Reload only when not firing 1 Answer
How can I make my Raycast ignore my player, but have other player's Raycasts be able to hit it? 1 Answer