• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RouniXd · Nov 11, 2018 at 12:58 PM · shootingmousepositionrotating

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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>();
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RouniXd · Nov 15, 2018 at 06:24 PM 0
Share

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?

avatar image Kciwsolb RouniXd · Nov 19, 2018 at 04:06 PM 0
Share

You need to rotate your new clone ins$$anonymous$$d. Since you already (assuming 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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

97 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting 2D projectiles that fire in the direction of the mouse position to work in unity 5? 0 Answers

rotating to mouse location 1 Answer

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges