Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by Queee · Feb 18, 2017 at 02:29 AM · scripting problemmousetop down shooter

How do I shoot where my mouse is pointing in a 2d top down game?

Hey im making a 2d top down shooter and i just found out how to make my character shoot but he only shoots in one direction how do i make him shoot where my mouse is at?

Heres my code

Shooting:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerShooting : MonoBehaviour {
 
     public Vector3 bulletOffset = new Vector3(0, 0.5f, 0);
 
     public GameObject bulletPrefab;
     int bulletLayer;
 
     public float fireDelay = 0.25f;
     float cooldownTimer = 0;
 
     void Start() {
         bulletLayer = gameObject.layer;
     }
 
     // Update is called once per frame
     void Update () {
         cooldownTimer -= Time.deltaTime;
 
         if( Input.GetButton("Fire1") && cooldownTimer <= 0 ) {
             // SHOOT!
             cooldownTimer = fireDelay;
 
             Vector3 offset = transform.rotation * bulletOffset;
 
             GameObject bulletGO = (GameObject)Instantiate(bulletPrefab, transform.position + offset, transform.rotation);
             bulletGO.layer = bulletLayer;
         }
     }
 }





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
Best Answer

Answer by oStaiko · Feb 18, 2017 at 04:27 AM

What you'll want here is to calculate the angle between the x axis, and the line made by the two points being your character and the mouse. The vector math behind this looks like this:

 GameObject player;
 Camera camera; //You need to set up your main camera here
 float nextFire = 0;

 void Update ()
 {
     if (Input.GetButtomDown("Fire1") && Time.time > nextFire)
     {
         nextFire = Time.time + fireDelay;

         Vector2 characterPos = camera.WorldToScreenPoint(player.transform.position);
         Vector2 mousePos = Input.mousePosition;
         Vector2 xAxis = new Vector2 (0,1); //Represents direction of X-axis
 
         Vector2 newLine = new Vector2(mousePos.x-characterPos.x, mousePos.y-characterPos.y);
         float angle = Vector2.Angle(xAxis, newLine);
         Quaternion rotation = Quaternion.identity;
         rotation.eulerAngles = new Vector3(0, angle, 0); //Assuming it's top down, with only y rotations

         Gameobject obj = (GameObject) Instantiate(bulletPrefab, transform.position, rotation);
          obj.layer = bulletLayer;
     }
 }
Comment
Add comment · Show 12 · 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 Queee · Feb 18, 2017 at 04:33 AM 0
Share

The 'Camera.WorldToScreenPoint' is red and the ' Vector2 xAxis = (0,1)' is too, and i dont know how to fix it... sorry im new to c# I really dont understand the vector things you dont have to explain it if you want im going to try to look it up

avatar image oStaiko Queee · Feb 18, 2017 at 04:46 AM 0
Share

Sorry! Yes it was a bit quick and those are just compiler errors, I'll update it in a sec

avatar image Queee oStaiko · Feb 18, 2017 at 04:49 AM 0
Share

Alright ill have to test it in the morning. Thanks! (i hope this works)

Show more comments
avatar image oStaiko Queee · Feb 18, 2017 at 05:31 PM 0
Share

I updated it again for you to include exactly what you need in your update function. I also edited your timer, it's faster to do it this way

avatar image Queee oStaiko · Feb 18, 2017 at 11:00 PM 0
Share

Omg i feel like im being to needy but in what you put if (Input.GetButtonDown("Fire1" && Time.time > nextFire)) 'nextFire' is red in Quaternion rotation = Quaternion.Identity; 'Quaternion.Identity' is red and in Instantiate(bulletPrefab, transform.position, rotation).layer = bulletLayer; '.layer' is red. you did have one spelling error but i fixed it c: Again im sorry im bugging you so much about this

Show more comments
Show more comments
avatar image oStaiko Queee · Feb 19, 2017 at 07:37 PM 0
Share

It is because nextFire was never declared, a pretty easy mistake to catch. I fixed it, but you really ought to learn how to discern errors yourself! It's an invaluable coding skill. Pay attention to what happens when you try to load the script in Unity, the error code will tell you how to fix it usually.

avatar image Queee oStaiko · Feb 19, 2017 at 08:46 PM 0
Share

I did try to fix it myself but i did not put a number after the float nextFire

Show more comments
Show more comments

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.

Follow this Question

Answers Answers and Comments

117 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

Related Questions

Unity 3D: Third person movement and mouse camera control 0 Answers

How to make movement speed decrease for 2D Top-down shooting game? 0 Answers

How would i change my mouse cursor in between scenes 0 Answers

Needing help with 3rd person mouse camera 1 Answer

ScreenToWorldPoint not working with cinemachine virtual camera 1 Answer


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