- Home /
What am I doing wrong with shooting at mose position?
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<Rigidbody2D>() 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.
Answer by Kciwsolb · Nov 12, 2018 at 06:18 PM
It looks like you are trying to add a Rigidbody Component to your bullet prefab by calling AddComponent. It also looks like you are trying to store a reference to that Rigidbody in rb from the return of that call.
Unity will let you add a Component to a prefab from script, but once you do that one time, subsequent AddComponent calls will not return a value since your prefab already has that component.
So if your prefab already has a Rigidbody, this line will return null:
rb = bullet.AddComponent<Rigidbody2D>() as Rigidbody2D;
However, even if you did get a reference there, it wouldn't be what you want. A reference to the Rigidbody from your bullet prefab would not do you any good since it is not the actual bullet you are Instantiating in the scene. The bullet prefab is just a template. You want a reference to the clone of the bullet prefab that you are Instantiating.
Instead of adding a Rigidbody to your prefab, you should make sure your prefab already has a Rigidbody added in the inspector and then call GetComponent after you instantiate an instance. Assign the return value of that call to rb and you will be good to go.
The quickest and dirtiest way to do that is like this:
rb = Instantiate(bullet, Gracz1.transform.position, Quaternion.identity).GetComponent<Rigidbody2D>();
Oh thanks! i get rid of the bug. Also thanks to you i discovered that this line bullet.transform.LookAt(direction);
does not rotate actual in-game object, but bullet prefab. Also the bullet is not rotating via z axis for some reason. Do you know how to fix this?
You need to rotate your new clone ins$$anonymous$$d. Since you already (assu$$anonymous$$g you inserted the line of code at the bottom of my answer) have a reference to the Rigidbody of the new clone, you can just call your rotate on that.
rb.transform.LookAt(direction); //This will fix the first problem but not the second
HOWEVER, your second problem of it not rotating correctly is because LookAt is designed for 3d games and attempts to point the Z axis of the object at your target. In 2d you want to probably want to point the X or Y axis at your target. So to get it working you should check out the accepted answer to this question that explains how to write a 2d equivalent to LookAt.
https://answers.unity.com/questions/654222/make-sprite-look-at-vector2-in-unity-2d-1.html
Your answer
Follow this Question
Related Questions
Getting 2D projectiles that fire in the direction of the mouse position to work in unity 5? 0 Answers
Unity 2D Shoot towards mouse position 1 Answer
Make an object move towards the mouse and then keep going 0 Answers
How to make 2d shooting towards the mouse but have it continue in staright line afterwards? 1 Answer