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 /
avatar image
0
Question by husnain_rao · Jan 21, 2018 at 02:21 PM · shootingmouse positionmouse clickrestrictionsregion

Restrict mouse click area

hey Firends! Can i restrict my mouse to click whithin a specific region. Actualy i m working on a shooting game when i click on screen it fires on the direction. it fires when i click any where in the screen. i want it to b fired only when mouse is clicked near my player. Thanx

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
1

Answer by EDevJogos · Jan 21, 2018 at 06:09 PM

You can calculate the distance between the player character and the mouse click, then check if it's less than x range.
.

  1. Get the player character pixels coordinates with Camera.main.WorldToScreenPoint(/*position*/);

  2. Calculate the distance between both, Input.mousePosition - /*character pixel coordinates*/

  3. Just check if the distance is less than x value in pixels, for exemple 150f;

.

Note. make the result value of distance absolute with Mathf.Abs(); or use Vector3.Distance() to calculate it.

.

Also consider storing a reference to the main camera and use it instead of Camera.main.

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
1

Answer by Kabaw · Jan 21, 2018 at 06:54 PM

Hi @husnain_rao,

Search's answer is correct. I've also created a simple Unity project where you can visualize the solution to your problem.

Link: https://drive.google.com/open?id=1nfm8ojic02t_5h5hoDGL0bh2ARzooNKF

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 husnain_rao · Jan 22, 2018 at 10:46 AM 0
Share

Hey @Search and @$$anonymous$$abaw Thanx it helped me a great. i did it and its working perfectly Thanx Again

avatar image
0

Answer by husnain_rao · Jan 22, 2018 at 09:39 AM

Hey @Search I did,nt understand this can u please Integrate in it

 Vector2 myPos;
         Vector2 target = Camera.main.ScreenToWorldPoint( new Vector2(Input.mousePosition.x,  Input.mousePosition.y) );//Get mouse Poz
         myPos = new Vector2(transform.position.x,transform.position.y);//get player pos
         
         Vector2 direction = target - myPos;//assume direction
         direction.Normalize();//make direction normalise
         GameObject ball = (GameObject) Instantiate( prefab, myPos, Quaternion.identity);//instantiate gameobject from prefab vriable, on player position
         ball.GetComponent<Rigidbody2D> ().velocity = direction * FireSpeed; //through object
         yield return new WaitForSeconds (3f);//wait for 3 seconds to destroy newly created object
         Destroy (ball);
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

76 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

Related Questions

How can I shoot a projectile into mouse position? 2 Answers

2D Platformer – changing the cursor position when facing left or right 0 Answers

Unity 2D - Bullets don't follow mouse 1 Answer

Add Effect to a small part on a mouse click 0 Answers

Help rotating gun towards mouse position (with flipping) 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