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 Cocotimba · Jan 02, 2012 at 07:20 AM · cameracrosshairmouse look

C# Crosshair

I am trying to add a Crosshair to my mouse look on my FPS using C#. I am totally clueless on how to do this, I been searching and found some java script tutorials, but I don't know java script or have any idea how exactly the crosshair works in a game.

Can someone please help me? I know I didn't put any code, but really I am clueless about 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

4 Replies

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

Answer by syclamoth · Jan 02, 2012 at 08:53 AM

Why don't you just do something really, really simple. There are a couple of things, but they depend on exactly what you want to achieve.

First up, make a crosshair image in your pixel manipulator of choice. Import into your project, preferably with some kind of transparency so that you can see through parts of it.

Then, in your C# file put the line

 public Texture2D crosshairImage;

in the variable declarations section.

This will allow you to drop a texture onto the component, and use it in your script!

Then, you can draw it on the screen with this-

 void OnGUI()
 {
     float xMin = (Screen.width / 2) - (crosshairImage.width / 2);
     float yMin = (Screen.height / 2) - (crosshairImage.height / 2);
     GUI.DrawTexture(new Rect(xMin, yMin, crosshairImage.width, crosshairImage.height), crosshairImage);
 }

This will draw the texture in the middle of the screen!

Of course, this isn't always what you want. Sometimes, you need it to be drawn on the mouse position instead!

 float xMin = (Screen.width - Input.mousePosition.x) - (crosshairImage.width / 2);
 float yMin = (Screen.height - Input.mousePosition.y) - (crosshairImage.height / 2);
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 Tomboy · Feb 23, 2013 at 02:57 PM 1
Share

I had to do the following for the crosshair at mouse position to work: substract (Screen.width - Input.mousePosition.x) from Screen.width. Otherwise the crosshair will mirror your mouse position at the center.

 // draw on current mouse position
 float x$$anonymous$$in = Screen.width - (Screen.width - Input.mousePosition.x) - (crosshairImage.width / 2);
 float y$$anonymous$$in = (Screen.height - Input.mousePosition.y) - (crosshairImage.height / 2);
 GUI.DrawTexture(new Rect(x$$anonymous$$in, y$$anonymous$$in, crosshairImage.width, crosshairImage.height), crosshairImage);

Although I do not know why but you don't have to do it on the y$$anonymous$$in value. Could someone explain this?

avatar image PraetorBlue Tomboy · Aug 20, 2016 at 07:04 PM 0
Share

It's because in screen coordinates, the Y values are "inverted" relative to how the mouse axis works. What I mean by this is that Y values near 0 are near the top of your screen, and Y values that are near Screen.height are near the bottom of your screen.

Compare this to the Y values from your mouse input - where moving your mouse "up" (Away from your body) gives you higher Y values and "down" (towards your body) gives you lower Y values.

The reason screen space coordinates work this way dates back to how CRT screens draw images - they draw "scan lines" across the image from left to right, starting at the top of the screen and going down. Thus the top line of the screen is y = 0.

avatar image
5

Answer by Jam0kid · Jul 30, 2012 at 10:15 AM

Hey bro, why not just try this simple C# code for a GUI Crosshair?

 void OnGUI(){
     GUI.Box(new Rect(Screen.width/2,Screen.height/2, 10, 10), "");
  }
     //this should be an easy fix if you are impatient :D
   
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
0

Answer by Cocotimba · Jan 02, 2012 at 11:10 AM

Thank you so much!

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
0

Answer by SETHI-ONE · Mar 17, 2015 at 04:18 PM

/ When I find myself in trouble, mother Mary comes to me. Speaking word of wisdom. Let it be! Just copy and paste / using UnityEngine; using System.Collections;

public class Crosshair : MonoBehaviour {

 public Texture2D crosshair;
 public Rect position;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     position = new Rect((Screen.width - crosshair.width) / 2, (Screen.height - crosshair.height) /2, crosshair.width, crosshair.height);
 }

 void OnGUI() {
     GUI.DrawTexture (position, crosshair);
 }

}

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 Victor2006 · Mar 18, 2017 at 05:50 AM 0
Share

Hahahahaha

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

11 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

Related Questions

Smoother camera in First Person Controller Prefab? 1 Answer

Spaceship crosshair 1 Answer

How to make camera position relative to a specific target. 1 Answer

My camera flickers and it's driving me crazy 2 Answers

Controls Like the Mouse Look. Just without using Mouse 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