Question by
rubenwinant · Mar 11, 2020 at 11:54 PM ·
rotation2d gameaiming2d rotationtop down shooter
2D top down shooter can only aim to the top right (mouse aim)
Hi, i'm very new to unity and i was trying to make a top-down shooter where you aim with the cursor. But wherever on the screen i aim, it only fires between the red lines.
The code i used:
public Transform firepoint;
public Camera cam;
public GameObject bulletPrefab;
public float bulletForce = 3.5f;
float nextShot = 0f;
float shotDelay = 1;
Vector2 mousePos;
private void Update()
{
mousePos = cam.WorldToScreenPoint(Input.mousePosition);
nextShot = Time.time;
if (Input.GetButtonDown("Fire1"))
{
Shoot();
nextShot = Time.time + nextShot;
}
}
private void FixedUpdate()
{
Vector2 lookDir = mousePos - new Vector2(firepoint.position.x, firepoint.position.y);
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
firepoint.rotation = Quaternion.Euler(0f, 0f, angle);
}
void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab, firepoint.position, firepoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firepoint.up * bulletForce, ForceMode2D.Impulse);
}
any help is very welcome
naamloos.png
(5.2 kB)
Comment
Your answer
Follow this Question
Related Questions
How do I give my top down game bullet spread? 1 Answer
How can I make my player's gun point at the cursor, not the player itself? 1 Answer
Aim at my cursor 1 Answer
2D fixed cam aim problem 0 Answers