I’m new to Unity and am making a 2D TopDownShooter and i want my player to shoot in the direction of the mouse but instead he just shoots in the top right corner of the screen.
Anyone knows how to fix it?
Heres the bullet code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBullet : MonoBehaviour
{
public float speed = 15f, minDamage = 20f, maxDamage = 40f, destroyDelay = 0.02f;
public Vector3 mousePos;
public Vector2 direction;
public Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
mousePos = Input.mousePosition;
mousePos.z = 0;
direction = (mousePos - transform.position).normalized * speed;
rb.velocity = new Vector2 (direction.x, direction.y);
Destroy(gameObject, 20f);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Enemy")
{
other.GetComponentInParent<Health>().health -= Random.Range(minDamage, maxDamage);
Destroy(gameObject, destroyDelay);
}
if (other.tag == "Border")
{
Destroy(gameObject, destroyDelay);
}
}
}