Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by xawaxus · Dec 27, 2013 at 10:53 PM · 2dmousebulletcursorshooter

Shooting in direction of mouse cursor 2d

Hello guys, I'm making 2d shooter and I want the player's gun shoot in the direction of mouse cursor. So I want bullet don't changing its direction when i move mouse. Just get a position of mouse when I click button and shoot there. But the mouse cursor can't be a camera because the game is "one area" (don't know what it's called, lol, McPixel or Super Crate Box for example :D) I've really read many tutorials but I still don't get it, please help :/ Thanks guys

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

5 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by Michelcyc · Dec 10, 2014 at 03:17 PM

I just solve the problem. Here is my code:

 //...setting shoot direction
 Vector3 shootDirection;
 shootDirection = Input.mousePosition;
 shootDirection.z = 0.0f;
 shootDirection = Camera.main.ScreenToWorldPoint(shootDirection);
 shootDirection = shootDirection-transform.position;
 //...instantiating the rocket
 Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
 bulletInstance.velocity = new Vector2(shootDirection.x * speed, shootDirection.y * speed);

Basically what I'm doing is mapping the mouse click from screenposition(2D) to worldposition(3D) based on the main camera. After that I calculate the position of the mouse minus the position of the player so we can create a vector of direction. Then I instantiate the rocket using only the X and Y from this directional vector to set the velocity.

I based my solution in this answer, you can use it for further explanations.

Comment
Add comment · 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
0

Answer by sparkzbarca · Dec 28, 2013 at 12:55 AM

to aim the gun at the mouse.

 gun.transform.rotation = quanternion.lookat(mouseposition in world space)

add a rigidbody to the bullet prefab

on mouse button press instantiate a bullet

 bullet = instantiate(bulletprefab,gun.position + .1 * gun.forward,gun.rotation);
 
 bullet.rigidbody.addforce(gun.forward * bulletspeed);


now you've made the gun look at the mouse and when a button is pressed you spawn a bullet traveling out from the gun.

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 xawaxus · Dec 28, 2013 at 06:29 PM 0
Share

okey I was working with that and I wrote

         Vector2 gunLook = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
         Camera.main.ScreenToWorldPoint (gunLook);
         transform.LookAt (gunLook);

but it works strangely. I add a photo! click

thanks

ps. but when mouse is on right side, in some deegres it works very good. it has to work in 360 degrees in every distance of mouse

avatar image
0

Answer by Unicorn-slayer · Jun 26, 2016 at 11:22 AM

This worked for me:

         Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         Vector2 direction = (Vector2)((worldMousePos - transform.position));
         direction.Normalize ();
 
         // Creates the bullet locally
         GameObject bullet = (GameObject)Instantiate (
                                 currentBulletPrefab,
                                 transform.position + (Vector3)( direction * 0.5f),
                                 Quaternion.identity);
 
         // Adds velocity to the bullet
         bullet.GetComponent<Rigidbody2D> ().velocity = direction * bulletVelocity;
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 mdjl4 · Jul 23, 2017 at 05:22 AM 0
Share

TYS$$anonymous$$.............. BUT i cant get this code to shoot at timed intervals :( by thanks alot anyway.

avatar image mdjl4 · Jul 23, 2017 at 05:50 AM 0
Share

oh and i edited the script to this:
void Start () {

 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetButtonDown("Fire1"))
     {
         Vector3 world$$anonymous$$ousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

         Vector2 direction = (Vector2)((world$$anonymous$$ousePos - transform.position));
         direction.Normalize();

         // Creates the bullet locally
         GameObject bullet = (GameObject)Instantiate(
                                 bullet1,
                                 transform.position + (Vector3)(direction * 0.5f),
                                 Quaternion.identity);

         // Adds velocity to the bullet
         bullet.GetComponent<Rigidbody2D>().velocity = direction * bulletVelocity;
     }

 }

} with 1 public gameobject called bullet so that i could shoot when clicked but still no solution to timed intervals.

avatar image
0

Answer by mdjl4 · Jul 23, 2017 at 05:52 AM

ok so i used the above persons script (Unicorn-slayer ) and edited it to use clicking:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class shooting : MonoBehaviour {

 public float bulletVelocity = 5f;
 public GameObject bullet;
 public GameObject bullet1;

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetButtonDown("Fire1"))
     {
         Vector3 worldMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

         Vector2 direction = (Vector2)((worldMousePos - transform.position));
         direction.Normalize();

         // Creates the bullet locally
         GameObject bullet = (GameObject)Instantiate(
                                 bullet1,
                                 transform.position + (Vector3)(direction * 0.5f),
                                 Quaternion.identity);

         // Adds velocity to the bullet
         bullet.GetComponent<Rigidbody2D>().velocity = direction * bulletVelocity;
     }

 }

}

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 DiangoGav · Sep 01, 2018 at 05:36 PM 0
Share

Works for my, Thanks!

avatar image
0

Answer by RealGamesss · Jul 10, 2020 at 03:31 PM

This maybe help https://youtu.be/Xq-rypmsTaA

Comment
Add comment · 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

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

26 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

Related Questions

2D platformer cursor 1 Answer

Cannonball not shooting according to the rotation of the cannon. 1 Answer

How to make a 2D character points his gun to the mouse position? 2 Answers

SetCursor is not a member of Cursor 1 Answer

How to center custom cursor 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