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 h00man · Sep 16, 2018 at 07:49 PM · crosshair

how to make a cross hair that follows the exact position of the mouse

hey guys, i"m trying to make a crosshair that is following the position of the mouse cursor on the screen because i'm making a 2.5D shooting game ,i use a script to get only X and Y position of the mouse .my character's gun is looking at this crosshair so when i shoot ,the bullet hits the point where the crosshair is. my problem is that the cross hair is not 100% accurate in following the exact position of the mouse. how do i attach my cross hair to exact position of mouse on screen? i made a GIF file to show the issue : http://www.mediafire.com/view/5qd1pp6qmaq35vg/GIF_20180917_000015.gif

here is my script to look at mouse position only on x and y :

     private Vector3 targetPos;
     public float speed = 2.0f;
 
     void Start() {
         targetPos = transform.position;
        
     }
 
     void Update () {
 
         float distance = transform.position.z + Camera.main.transform.position.z;
         targetPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
         targetPos = Camera.main.ScreenToWorldPoint(targetPos);
 
         Vector3 followXonly = new Vector3(targetPos.x, targetPos.y, transform.position.z);
         transform.position = Vector3.Lerp (transform.position, followXonly, speed * Time.deltaTime);
     }
 }
Comment
Add comment · Show 1
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 h00man · Sep 17, 2018 at 05:50 PM 0
Share

have you guys heard about a game "my friend pedro"? i want a crosshair exactly like that. like this photo :

https://i.ytimg.com/vi/s2$$anonymous$$pyXb9XRk/hqdefault.jpg

where ever the cross hair looks his hand aim ik looks too.by the way i used to use an UI image for the cross hair on te screen and attached a script to it and told it to be where ever the mouse cursor is.the problem was that an image does not have a transform component so i couldn't use an image as a target for my ai$$anonymous$$g hand ik

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by hexagonius · Sep 16, 2018 at 08:44 PM

You're lerping the position. Each frame the position the crosshair is set to is somewhere between it's current position and followXonly. Just set transform.position to followXonly and you're good to go.

Comment
Add comment · Show 6 · 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 h00man · Sep 17, 2018 at 08:11 AM 0
Share

thanks for answer, you mean something like this? transform.position = followXonly;

avatar image Eno-Khaon h00man · Sep 17, 2018 at 09:46 AM 1
Share

To clarify a bit, let's say you're moving from 0 to 120 (on the X-axis), and moving 50% of the way from your current position per frame.

 Frame # | New X Position
 0       | 0
 1       | 60
 2       | 90
 3       | 105
 4       | 112.5



No matter how long you move, you'll never completely reach the destination. With that in $$anonymous$$d, putting the cursor directly at the mouse position (followXonly) is a perfectly reasonable approach.

If you still want to have the crosshair ease towards the cursor position a bit if the mouse moves especially quickly, you might want to look into Vector3.$$anonymous$$oveTowards() to define a strict maximum rate of movement.

avatar image h00man Eno-Khaon · Sep 17, 2018 at 05:40 PM 0
Share

so i changed the script to something like this and still the cross hair doesn't exactly match with mouse position :( idont know whats wrong.

 transform.position = Vector3.$$anonymous$$oveTowards(transform.position, followXonly, speed * Time.deltaTime); 
Show more comments
avatar image hexagonius h00man · Sep 17, 2018 at 06:41 PM 0
Share

yes, that is what I meant

avatar image h00man hexagonius · Sep 18, 2018 at 06:57 AM 0
Share

thanks guys for your advice . so i changed the script and used only Vector2.$$anonymous$$oveTowards and it works fine .but just there is a tiny little issue with the way the cross hair moves on the screen. see here :

http://www.mediafire.com/view/nwhsy3dw9ajggin/GIF_20180918_111600.gif

it's movement is a little bit robotic and not as smooth as before but after all i got want i needed 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

90 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 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

Third Person Cross Hair Question 1 Answer

C# Crosshair 4 Answers

Use Gamepad right analog stick instead of mouse to control crosshair movement 3 Answers

C# OR Java- Multiplayer Shoot Sound 0 Answers

2D Aiming with a mouse angle problem 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