Hello. For a few deys straight i cant find a solution for this problem. I want to rotate and then shoot a bullet into mouse position. I tried using planes, camera.mian.screentoworldpoint but that dont work. I dont know what i am doing wrong. This is my code so far:
void Awake()
{
rb = bullet.AddComponent() as Rigidbody2D;
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Input.mousePosition;
mousePos = cam.ScreenToWorldPoint(mousePos);
Vector2 direction = new Vector2(
mousePos.x - Gracz1.transform.position.x,
mousePos.y - Gracz1.transform.position.y
);
Instantiate(bullet, Gracz1.transform.position, Quaternion.identity);
bullet.transform.LookAt(direction);
rb.velocity = new Vector2(direction.x, direction.y);
}
}
From this I get "object reference not set to an instance of object' error and i dont know what to do with it. Bullet spawns but dont rotate and dont 'shoot' at mouse position. My camera is set to ortoghraphic.
↧