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
0
Question by RamujTo · May 15, 2014 at 08:10 PM · mouseshootpositioning

How to make a 2Dgun shoot at the position of mouse?

I've alredy made a gun that rotate's in one place and it's facing mouse and it's eaven shooting but it spawns bullets sideway to camera and it's invisible but colider and rigidbody works. But i want my bullets to be visible please HELP.

This is code:

 using UnityEngine;
 using System.Collections;
 
 public class RuchyGracza : MonoBehaviour {
     
     private Vector3 mouse_pos;
     private Vector3 object_pos;
     private float angle;
     private float bulletSpeed = 500f;
     public GameObject[] ammo;        
 
     void Start () {
     }
     
     void Update(){ 
     }
     
     void FixedUpdate () {    
         Vector3 mouse_pos = Input.mousePosition;
         Vector3 player_pos = Camera.main.WorldToScreenPoint(this.transform.position);
         
         mouse_pos.x = mouse_pos.x - player_pos.x;
         mouse_pos.y = mouse_pos.y - player_pos.y;
         
         float angle = Mathf.Atan2 (mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
         this.transform.rotation = Quaternion.Euler (new Vector3(0, 0, angle));
 if(Input.GetButtonDown("Fire1")){
             if(Input.GetMouseButtonDown(0)){
                 int ammoIndex = 0; 
                 GameObject bullet = (GameObject)Instantiate(ammo[ammoIndex], transform.position, transform.rotation);
                 bullet.transform.LookAt(mouse_pos);
                 bullet.rigidbody2D.AddForce(bullet.transform.forward * bulletSpeed);
             }
         }
     }
 }

 
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

3 Replies

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

Answer by robertbu · May 16, 2014 at 01:03 AM

Assuming your bullets are Sprites or Quads, your problems is in the use of LookAt(). Instead you can do:

 if(Input.GetButtonDown("Fire1")){
      if(Input.GetMouseButtonDown(0)){
          int ammoIndex = 0; 
          GameObject bullet = (GameObject)Instantiate(ammo[ammoIndex], transform.position, transform.rotation);
          bullet.rigidbody2D.AddForce(bullet.transform.right * bulletSpeed);
      }
  }

This assumes your bullet and your shooter is facing right when they have a rotation of (0,0,0).

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 Cherno · May 16, 2014 at 12:55 AM

This line:

 Vector3 mouse_pos = Input.mousePosition;

is wrong. Check the Unity Script Reference; Input.mousePosition is in pixel coordinated and has nothing to do with the world coordinate system.

To shoot at the point that you aim at with the mouse cursor, you can use a raycast:

 Ray mouseRay = Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit hit;
 
 void Update() {
 
      if(Input.GetMouseButtonDown(0)) {
           if(Physics.Raycast(mouseRay, out hit, Mathf.Infinity) == true) {
                ShootBullet(hit.point);
                return;
           }
      }
 }
 
 void ShootBullet(Vector3 hitPoint) {
      GameObject bullet = Instantiate(ammo[ammoIndex], transform.position, transform.rotation) as GameObject;
      bullet.transform.LookAt(hitPoint);
      bullet.rigidbody2D.AddForce(bullet.transform.forward * bulletSpeed);
 }


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 robertbu · May 16, 2014 at 01:00 AM 0
Share

@Cherno - he does a WorldToScreenPoint and calculates the angle based on the screen coordinates, so his rotation code looks fine.

avatar image Cherno · May 16, 2014 at 01:11 AM 0
Share

I stand corrected, thank you. It's an interesting way to do things :)

avatar image
0

Answer by RamujTo · May 16, 2014 at 01:36 PM

Thank you very much for all responds now it's working perfect now!

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 Cherno · May 16, 2014 at 05:49 PM 0
Share

Please post comments as comments, not answers.

Also, please mark the right answer as accepted.

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

21 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

Related Questions

Can't shoot towards mouse click point 1 Answer

Need Javascript to propel an object toward mouse click 1 Answer

2D Sprite rotate relative to mouse snapping incorrectly. 2 Answers

The code is giving me errors 1 Answer

How can I rotate/orbit the camera around an object? 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