I cannot for the life of me get this to work. Whenever I press play and left click, a projectile just gets Instantiated right in the middle of my player with no movement… can anyone help?
public class PlayerShooting : MonoBehaviour
{
public GameObject projectilePrefab;
Rigidbody2D projectileRB;
Vector2 playerLook;
Vector2 direction;
Vector2 myPosition;
public float speed = 5f;
private void Start()
{
projectileRB = projectilePrefab.GetComponent<Rigidbody2D>();
}
private void Update()
{
playerLook = Camera.main.ScreenToWorldPoint(Input.mousePosition);
myPosition = new Vector2(transform.position.x, transform.position.y);
direction = playerLook - myPosition;
direction.Normalize();
if(Input.GetMouseButtonDown(0))
{
GameObject newProjectile = (GameObject) Instantiate(projectilePrefab, myPosition, Quaternion.identity);
projectileRB.velocity = direction * speed;
}
}
}