Quantcast
Channel: Questions in topic: "rotating"
Viewing all articles
Browse latest Browse all 173

What am I doing wrong with shooting at mose position?

$
0
0
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.

Viewing all articles
Browse latest Browse all 173

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>