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 yohanrw · Oct 20, 2013 at 01:42 PM · rigidbodygamemousefire

How to shoot to where "Mouse" is pointing?

Hello,

This is my third day in Unity. I successlly created a small game, as described in my book. The game is "shooting at bricks". Following is the code.

 using UnityEngine;
 using System.Collections;
 
 public class Shooter : MonoBehaviour {
     
     public Rigidbody bullet;
     public float power = 1500f;
     public float moveSpeed = 25f;
 
     
     // Update is called once per frame
     void Update () 
     {
         float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
         float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
         
         transform.Translate(h,v,0);
         
         if(Input.GetButtonUp("Fire1"))
         {
             Rigidbody instance = Instantiate(bullet,transform.position,transform.rotation) as Rigidbody;
             
             Vector3 fwd = transform.TransformDirection(Vector3.forward);
             
             instance.AddForce(fwd*power);
         }
     
     }
 }

Now, the problem is, this is not shooting to where the "Mouse Pointer" is pointing. Instead, it simply start shooting from the center of the screen.

The "Shooter" is assigned to the "Main Camera" object. So, how can I shoot to the place where the mouse pointer is pointing?

Please help

Comment
Add comment · Show 3
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 · Oct 20, 2013 at 04:23 PM 0
Share

How you code this will depend on whether you want to shoot from the mouse, or whether you want to use the mouse to target something but shoot from some other point.

avatar image yohanrw · Oct 20, 2013 at 05:18 PM 0
Share

@robertbu: yes, I need the mouse to target and shoot

avatar image bubzy · Oct 22, 2014 at 07:02 AM 0
Share

i know this isn#t the issue you are having, but why are you using GetButtonUp for shooting?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MarkD · Oct 20, 2013 at 04:01 PM

This is fairly easy. change Vector3 fwd = transform.TransformDirection(Vector3.forward); to Vector3 fwd = transform.TransformDirection(Vector3.forward) to Vector3 fwd = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset - new Vector3(0,0, - Distance);

Distance is a seperate var as we can only retrive x an y coordinates from the mouse, so the z axis has to be put in manualy.

so the code would looke something like this

 using UnityEngine;
 using System.Collections;
  
 public class Shooter : MonoBehaviour {
  
     public Rigidbody bullet;
     public float power = 1500f;
     public float moveSpeed = 25f;
    public float Distance=2;
  
     // Update is called once per frame
     void Update () 
     {
        float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
        float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
  
        transform.Translate(h,v,0);
  
        if(Input.GetButtonUp("Fire1"))
        {
          Rigidbody instance = Instantiate(bullet,transform.position,transform.rotation) as Rigidbody;
  
        Vector3 fwd = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset - new Vector3(0,0, - Distance);
 
  
          instance.AddForce(fwd*power);
        }
  
     }
 }


I hope this helps you get along.

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 JasonC001 · Oct 22, 2014 at 06:24 AM 0
Share

What are curScreenPoint and offset, as they're not declared variables in this script, and without them, this code only seems to shoot in one direction, not even taking into account the camera's rotation. (If I look the other way, it's shooting out behind me, ins$$anonymous$$d of out in front of me.) thanks

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

17 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

Related Questions

My bullet wont move forward 1 Answer

How to properly convert mouse pos to world pos 1 Answer

newbie: drag a rigidbody with mouse 2 Answers

Listening to the "Mouse Scroll Wheel" 1 Answer

Array Problem - Error Code BCE0022 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