• 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 Micordie · Jul 12, 2020 at 07:53 AM · shooting2d-gameplay

How to make projectiles shoot towards mouse

I started Unity and C# about a week ago and have been building small projects to get started. I want to learn how to make a 2D shooter and have made some progress. I can successfully instantiate the bullet and add some force to it (seemingly) in the direction of my mouse. The problem is that there seems to be a constant force up and right. Depending on where my mouse is, the bullets move slower. The only way I can get the bullets to travel left is if the mouse is in the very bottom left of the screen. Here is the script in question:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BulletTracker : MonoBehaviour
 {
     public float moveSpeed;
     private bool hasShot = false;
     public Rigidbody2D rb;
     public GameObject bulletSpawn;
     
     void Update() {
         if (!hasShot)
             rb.AddForce(Input.mousePosition * moveSpeed);
             hasShot = true;
     }
 }

I know this is probably incredibly inefficient and could be done easier, but for now I am just getting the hang of the language. The variable bulletSpawn is the empty object in front of my character where the bullets spawn. When I spawn the bullets without this script active, the bullets just fall to the ground, so I think the problem must be here somewhere. If anyone could help I would really appreciate it. Thanks!

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
1

Answer by MakerBen · Jul 12, 2020 at 11:51 AM

Ok your issue is with Input.mousePositioin. You need to de

 Vector 2 mousePos;
 mousePos = Camera.screenPointToWorld(mouseposition)

so the completed code is

     using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class BulletTracker : MonoBehaviour
      {
          public float moveSpeed;
          private bool hasShot = false;
          public Rigidbody2D rb;
          public GameObject bulletSpawn;
          
          void Update() {
                if (!hasShot)
 `             `{
                 Vector 2 mousePos;
                 mousePos = Camera.screenPointToWorld(Input.mousePosition);
                 rb.AddForce(mousePos* moveSpeed);
                 hasShot = true;
              }
          }
      }

@microdie

Comment
Add comment · Show 1 · 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 Micordie · Jul 12, 2020 at 07:11 PM 0
Share

So I tried implementing the code you supplied and ran into 1 error:

Assets/BulletTracker.cs(15,31): error CS0117: 'Camera' does not contain a definition for .screenPointToWorld'

I looked it up and thought maybe you meant to write 'ScreenToWorldPoint', but that did not work either.

Thank you for the help!

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

134 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 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

Problem with 2D shooter Game? 1 Answer

Shooting/Firing in 2D Top Down Shooter 0 Answers

how to shoot bullets from circumference of a circle ? 1 Answer

Component organization for shooting 2 Answers

fps shooting enemy 1 Answer

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