• 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 Ruchaice · Jan 14 at 04:14 AM · 3dscripting beginnershootingshootshooter

How to shoot a bullet to the mouse cursor position?

I'm currently making a 3d top-down shooter and I want the player to shoot bullets to the position of a mouse click, but right now no matter where I click, the bullets always go on top direction. Here is my shooting bullet script so far: public class shooting : MonoBehaviour { public GameObject projectile; public float shootingForce; public AudioClip shootingSound; private AudioSource shootingAudio;

 // Start is called before the first frame update
 void Start()
 {
     shootingAudio = GetComponent<AudioSource>();
 }

 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Mouse0))
     {
         // GameObject shot = GameObject.Instantiate(projectile, transform.position, transform.rotation);
         // shot.GetComponent<Rigidbody>().AddForce(transform.forward * shootingForce);
         
         GameObject shot = GameObject.Instantiate(projectile, transform.position, transform.rotation);
     
         Vector3 screenPoint = Camera.main.ScreenToWorldPoint(transform.position);
         Vector3 direction = (Input.mousePosition - screenPoint);
         direction.Normalize();
        
         shot.GetComponent<Rigidbody>().AddForce(direction*shootingForce, ForceMode.Impulse);

         shootingAudio.PlayOneShot(shootingSound, 1.0f);
     }
 }

} Does anybody knows how to fix this?

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 mf41z · Jan 14 at 09:09 AM

You mean the bullet goes up? 'the bullets always go on top direction' Didn't get this part. What you should be doing is using Raycast if you want the bullet to hit what you're pointing at specifically. First get a ray that goes out from your camera to the mouse position:

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

The end of that ray will be where you're looking at.

         Vector3 dir;    
         if (Physics.Raycast(ray, out RaycastHit hit, 30f))
         {
             dir = hit.point - player.transform.position;
         }

That will get you the point where the player is looking at, rather than the cursor position overall. Use that direction to apply the force at the end. Tell me if that works for you or if you have any questions

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 Ruchaice · Jan 15 at 06:19 AM 0
Share

UPDATE: I tried to fix my own code by following your instructions but I got the error that says "shooting.cs(31,35): error CS0120: An object reference is required for the non-static field, method, or property 'Component.transform'". Here is my code so far.

public class shooting : MonoBehaviour { public GameObject projectile; public float shootingForce; public AudioClip shootingSound; private AudioSource shootingAudio;

 // Start is called before the first frame update
 void Start()
 {
     shootingAudio = GetComponent<AudioSource>();
 }

 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Mouse0))
     {
         GameObject shot = GameObject.Instantiate(projectile, transform.position, transform.rotation);
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         Vector3 dir;

         if (Physics.Raycast(ray, out RaycastHit hit, 30f))
         {
             dir = hit.point - player.transform.position;
             shot.GetComponent<Rigidbody>().AddForce(dir * shootingForce, ForceMode.Impulse);
         }

         shootingAudio.PlayOneShot(shootingSound, 1.0f);
     }
 }

Can you explain to me how to fix that?

avatar image Ruchaice · Jan 20 at 01:15 PM 0
Share

@mf41z UPDATE 2: I fixed my code a little bit and I got two problems. 1) The bullets are moving very slowly no matter how much shooting force I applied to my script. 2) The bullets goes on random directions instead of following the mouse click. Here is my code so far. public class shooting : MonoBehaviour { public GameObject projectile; public float shootingForce; public AudioClip shootingSound; private AudioSource shootingAudio;

 // Start is called before the first frame update
 void Start()
 {
     shootingAudio = GetComponent<AudioSource>();
 }

 // Update is called once per frame
  void Update()
 {
     if (Input.GetKeyDown(KeyCode.Mouse0))
     {

         GameObject shot = GameObject.Instantiate(projectile, transform.position, transform.rotation);
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         shootingAudio.PlayOneShot(shootingSound, 1.0f);
         Vector3 dir;

         if (Physics.Raycast(ray, out RaycastHit hit, 30f))
         {
             dir = hit.point - transform.position;
             shot.GetComponent<Rigidbody>().AddForce(dir * shootingForce, ForceMode.Impulse);
         }

     }
 }

Can you tell me how to fix this?

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

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

FPS rigidbody bullets not moving to center of screen 0 Answers

Shoot Script Help 1 Answer

Problem with firing laser in direction of camera 1 Answer

Shooting and reloading mechanism problem. 1 Answer

How to shoot with swipe? 0 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