• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by _Newbz · Jun 26, 2015 at 02:12 AM · c#topdown

2d top down shooting towards mouse

I have a 2d top down game , to which i need a script that fires a projectile in the direction om my mouse this is what i got so far

 using UnityEngine;
 using System.Collections;
 
 public class Shoot : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update ()
         if( Input.GetMouseDown( 0 ) )
     {
         Plane zeroPlane = new Plane( Vector3.up, Vector3.zero );
         Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
         float distance;
         
         if( zeroPlane.Raycast( ray, out distance ) )
         {
             Vector3 outputPosition = ray.origin + ray.direction * distance;
             Debug.Log( "Position: " + outputPosition  );
             
             //You can use this position to rotate the bullet like so
             BulletClone.transform.LookAt( outputPosition );
         }
     }

But it doesn't seem to work.

Comment
Add comment · Show 5
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 Cherno · Jun 26, 2015 at 02:14 AM 0
Share

But it doesn't seem to work.

Please be more specific. What happens when you shoot? What does the bullet do? Did you insert Debug.Log lines to check if all lines of your code are reached? Have you used Debug.DrawRay/Debug.DrawLine to see where the raycast is going?

avatar image _Newbz · Jun 26, 2015 at 03:01 AM 0
Share

it gives me lots of errors: Assets/Scripts/Shoot.cs(13,18): error CS1519: Unexpected symbol if' in class, struct, or interface member declaration Assets/Scripts/Shoot.cs(13,39): error CS1519: Unexpected symbol (' in class, struct, or interface member declaration Assets/Scripts/Shoot.cs(19,18): error CS1519: Unexpected symbol if' in class, struct, or interface member declaration Assets/Scripts/Shoot.cs(19,38): error CS1519: Unexpected symbol (' in class, struct, or interface member declaration Assets/Scripts/Shoot.cs(19,43): error CS1519: Unexpected symbol ,' in class, struct, or interface member declaration Assets/Scripts/Shoot.cs(19,58): error CS1519: Unexpected symbol )' in class, struct, or interface member declaration Assets/Scripts/Shoot.cs(22,34): error CS1519: Unexpected symbol (' in class, struct, or interface member declaration Shoot.cs(22,67): error CS1519: Unexpected symbol )' in class, struct, or interface member declaration Assets/Scripts/Shoot.cs(25,53): error CS1519: Unexpected symbol (' in class, struct, or interface member declaration Assets/Scripts/Shoot.cs(25,70): error CS1519: Unexpected symbol )' in class, struct, or interface member declaration Assets/Scripts/Shoot.cs(27,9): error CS8025: Parsing error

avatar image Cherno · Jun 26, 2015 at 03:07 AM 0
Share

You Update function is missing it's {}:

 void Update ()
     {//<- the first missing one
          if( Input.Get$$anonymous$$ouseDown( 0 ) )
          {
               Plane zeroPlane = new Plane( Vector3.up, Vector3.zero );
               Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
               float distance;
          
               if( zeroPlane.Raycast( ray, out distance ) )
               {
                   Vector3 outputPosition = ray.origin + ray.direction * distance;
                   Debug.Log( "Position: " + outputPosition  );
              
                   //You can use this position to rotate the bullet like so
                   BulletClone.transform.LookAt( outputPosition );
               }
           }
      }//<- the second missing one :P

I copy this comment as an answer.

avatar image _Newbz · Jun 26, 2015 at 05:18 PM 0
Share

Thanks , i will try

avatar image GrahamP · Jun 26, 2015 at 11:36 PM 0
Share

Should the command be Input.Get$$anonymous$$ouseButtonDown(0) as apposed to Get$$anonymous$$ouseDown?

1 Reply

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

Answer by Cherno · Jun 26, 2015 at 03:07 AM

You Update function is missing it's {}:

 void Update ()
 {//<- the first missing one
      if( Input.GetMouseDown( 0 ) )
      {
           Plane zeroPlane = new Plane( Vector3.up, Vector3.zero );
           Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
           float distance;
      
           if( zeroPlane.Raycast( ray, out distance ) )
           {
               Vector3 outputPosition = ray.origin + ray.direction * distance;
               Debug.Log( "Position: " + outputPosition  );
          
               //You can use this position to rotate the bullet like so
               BulletClone.transform.LookAt( outputPosition );
           }
       }
  }//<- the second missing one :P
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 _Newbz · Jun 26, 2015 at 05:28 PM 0
Share

Yeah but it give me errors : Assets/Scripts/Shoot.cs(14,27): error CS0117: UnityEngine.Input' does not contain a definition for Get$$anonymous$$ouseDown' Assets/Scripts/Shoot.cs(26,33): error CS0103: The name `BulletClone' does not exist in the current context

avatar image Cherno · Jun 26, 2015 at 06:39 PM 0
Share

Use this ins$$anonymous$$d of Input.Get$$anonymous$$ouseDown:

 if (Input.Get$$anonymous$$ouseButtonDown(0)) {
 
 }

Input.Get$$anonymous$$ouseButtonDown

if BulletClone does not exist, then you need to declare this variable in the script's main body

 public class Shoot : $$anonymous$$onoBehaviour {
  
      public GameObject BulletClone;
 
      // Use this for initialization
      void Start () {
      
      }
 //...

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

2D camera zoom smoothing and limitations? 1 Answer

2d top down shooting system help ? 1 Answer

C# LookAt Mouse 2D 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges