• 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
1
Question by allashremoter · Aug 09, 2012 at 11:04 AM · crosshairworldtoscreen

convert world to screen (crosshair follow)

hi! help with converting in screen coords. I made crosshair following where is muzzle of tank aims. But i can't convert world coord in screen rightly.

 if(Physics.Raycast(firePoint.position, firePoint.TransformDirection(Vector3.left), out hit))
         {
                 Debug.DrawLine(firePoint.position,hit.point,Color.red);
                 
 // Vector 3 of hit.gameobject
                 pointCursor = Camera.main.WorldToScreenPoint(hit.transform.position);
         
        }
 
         void OnGUI()
         {
                 Vector2 vector2 = GUIUtility.ScreenToGUIPoint(new Vector2(pointCursor.x, pointCursor.y));
                 Rect labelRect = new Rect();
                 labelRect.x = vector2.x;                
                 labelRect.y = vector2.y;
                 labelRect.width = cursorTexture.width;
                 labelRect.height = cursorTexture.height;
 
                 GUI.DrawTexture(labelRect,cursorTexture);
         }
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
0
Best Answer

Answer by aldonaletto · Aug 09, 2012 at 11:16 AM

You should use hit.point: this returns the 3D point where your raycast hit something. If you use hit.transform.position, the crosshair will be located at the hit object's pivot: looks weird for small objects, and completely wrong for the bigger ones.

...
if (Physics.Raycast(firePoint.position, firePoint.TransformDirection(Vector3.left), out hit)){
  // Use the hit point instead:
  pointCursor = Camera.main.WorldToScreenPoint(hit.point);
}
...
NOTE: The crosshair will be rendered even if the raycast doesn't hit anything, what may seem weird. You can avoid this using a boolean variable like this:

private var hitSomething: boolean; // <- declare this variable

... // assign the raycast result to it: hitSomething = Physics.Raycast(firePoint.position, firePoint.TransformDirection(Vector3.left), out hit); if (hitSomething){ // Use the hit point: pointCursor = Camera.main.WorldToScreenPoint(hit.point); } ...

function OnGUI(){ if (hitSomething){ // only draw the crosshair if something hit: Vector2 vector2 = GUIUtility.ScreenToGUIPoint(new Vector2(pointCursor.x, pointCursor.y)); ... GUI.DrawTexture(labelRect,cursorTexture); } }

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 allashremoter · Aug 09, 2012 at 12:16 PM 0
Share

Thank you very much for this answer!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

crosshair help needed 2 Answers

Crosshair help 1 Answer

Question about a space shooter crosshair targeting script 1 Answer

Shooting with a locked camera position 1 Answer

Aiming Down Sights Problem 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges