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 getgcs · Jan 16, 2013 at 03:42 PM · mousecrosshair

Clicking in game view with cross-hair doesn't work

Do I need extra code for the clicking to work when I have a cross-hair? I have to constantly build and run to test something because when I click an object (in game view)it doesn't click the object.

When I build and run it and set up my native resolution it does click the object. For testing purposes how can I continue making my game and have a crosshair? The reason why I need a cross-hair is because the mouse is all messed up. I also had to lock the mouse on every frame to make the mouse go away... This is nuts.

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 · Jan 16, 2013 at 03:58 PM 0
Share

Posting some source might help us see what you are trying to do and where you are having the problem. $$anonymous$$y guess is that you are not doing the Raycast correctly.

avatar image getgcs · Jan 16, 2013 at 04:01 PM 0
Share
 var crosshairTexture : Texture2D;
 var position : Rect;
 static var OriginalOn = true;
 
 function Update() // Start will only get the screen size once. it will not refresh it. the turn around is to use function Update() ins$$anonymous$$d.
 {
     position = Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height - 
         crosshairTexture.height) /2, crosshairTexture.width, crosshairTexture.height);
 }
 
 function OnGUI()
 {
     if(OriginalOn == true)
     {
         GUI.DrawTexture(position, crosshairTexture);
     }
 }
avatar image getgcs · Jan 16, 2013 at 04:02 PM 0
Share

Is there a raycast in this code?

1 Reply

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

Answer by robertbu · Jan 16, 2013 at 05:24 PM

You need to Raycast the position into the scene to see what you hit. Your crosshairs are always locked at the center of the screen, so here is a snippet of untested code:

 Vector2 v2ScreenCenter = new Vector2(Screen.width / 2, Screen.width / 2);
 Ray ray = Camera.mainCamera.ScreenPointToRay (v2ScreenCenter);
 RaycastHit hit;
         
 if (Physics.Raycast (ray, out hit))
 {
     Destroy(hit.collider.gameObject);
 }

This code destroys the game object. Replace with whatever you want to do on a hit. v2ScreenCenter only needs to be initialized once.

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 getgcs · Jan 16, 2013 at 05:37 PM 0
Share

Why can't you use the regular mouse functions in unity correctly? Why do they not center nor work correctly? Thanks for the help on ray-casting though I wish it wasn't required.. I hope the unity $$anonymous$$m plans on removing the need for code more.. eventually.

avatar image robertbu · Jan 16, 2013 at 06:10 PM 0
Share

Raycasting is needed because Unity is a 3D platform. You view things in perspective. Imagine you are looking out the window in the real world. You see more of the scene at a distance than near to the window. If I placed my finger on the window and closed one eye, I could create a line from my eye through my finger into the scene. The X,Y coordinates of something that my finger was overlaying would be differnet for objects far away than for near object. (relative to the center of the window) That is what you are doing by raycasting.

Note if you were using a mouse, you could use something like On$$anonymous$$ouseDown() {} and avoid raycasts. Alternatively you can change your camera's projection from "Perspective" to "Orthographic." Then there is only a simple bit of math to translate screen position to world position (no Raycast needed). But by making this change you lose the perspective view of objects in the scene.

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

9 People are following this question.

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

Texture-based mouseclick detection 1 Answer

Crosshair when holding RMB 2 Answers

Gun object following mouse. 2 Answers

Use Gamepad right analog stick instead of mouse to control crosshair movement 3 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