So, I’m working on a 2d game with a top view, where the player character is a balloon that keeps falling, and you have to shoot little gusts of air to keep itself afloat while avoiding obstacles and enemies. The issue I’m having right now is that I want to make the force part of the gust of air to work, but have no idea of how to code it since I’m not that good at coding. The shooting part works fine, I can instantiate projectiles towards where I click and everything, but have no idea of how the addForce part would work.
Here’s the script for the player character
{
public float moveSpeed = 0f;
public Rigidbody2D rb;
public Weapon weapon;
public bool canMove;
Vector2 moveDirection;
Vector2 mousePosition;
// Update is called once per frame
void Update()
{
float moveX = Input.GetAxisRaw("Horizontal");
float moveY = Input.GetAxisRaw("Vertical");
if(Input.GetMouseButtonDown(0)){
weapon.Fire();
}
moveDirection = new Vector2(moveX, moveY).normalized;
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
private void FixedUpdate() {
rb.velocity = new Vector2(moveDirection.x * moveSpeed, moveDirection.y * moveSpeed);
Vector2 aimDirection = mousePosition - rb.position;
float aimAngle = Mathf.Atan2(aimDirection.y, aimDirection.x) * Mathf.Rad2Deg - 90f;
rb.rotation = aimAngle;
}
}
and here’s the fire() method:
public void Fire(){
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
bullet.GetComponent<Rigidbody2D>().AddForce(firePoint.up* fireForce, ForceMode2D.Impulse);
}
any help would be greatly appreciated.