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 dylan56477 · Nov 21, 2011 at 01:48 AM · guicrosshairred

how do i have a crosshair turn red when over an enemy?

Hi! I was wondering how to make a crosshair turn red when it's over an enemy? What I was thinking was, raycasting perhaps? So it draws an imaginary line then when it's in line with a gameobject tagged enemy it will switch the crosshair 2d Texture with a red one that I'll make. (no need to change the color within unity)

This is my crosshair script:

 var crosshairTexture : Texture2D;
 var position : Rect;
 static var OriginalOn = true;
 
 function Start()
 {
     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);
     }
 }

So how? Thanks a bunch

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 Adam-Buckner ♦♦ · Apr 23, 2013 at 09:59 AM 0
Share

Added code tags to the sample code - [LA]

1 Reply

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

Answer by Kacer · Nov 21, 2011 at 09:51 AM

Heh, you answered your own question in your post.

use a simple raycast to check if you've got your crosshair hovering over an enemy, if that is the case, switch the textures for the crosshair.

or was the question about how you made a raycast and used it to switch the textures?, also, please reread your posts after you've posted them to make sure your code was formatted correctly

edit: Here's some stuff about how to do it.

First of all, a raycast is a ray you can cast in any given direction in the game, here's a link to the documentation: http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

If you cast that in update you get a response for every frame, the return value of the raycasthit is the gameobject itself, with all attached scripts, rigidbodies, values etc etc. SO, what you do is that you write something along the lines of

 if(hit.gameobject.tag == "enemy"){
     guicrosshairtex = redcrosshairtex;
 }else{
     guicrosshairtex = greencrosshairtex;
 }

simple simple :P

Comment
Add comment · Show 3 · 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 dylan56477 · Nov 21, 2011 at 04:33 PM 0
Share

yes! I was asking how to write that into the code, like you writing it in and then reposting the whole code. Please. lol

avatar image dylan56477 · Nov 21, 2011 at 04:34 PM 0
Share

I know how code works, but when it comes to writing it I get stuck.

avatar image Kacer · Nov 21, 2011 at 04:44 PM 0
Share

Well, i am not going to write the entire code for you, but i will help you along then :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Make a crosshair turn red when over an enemy 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Crosshairs change when sprinting? 3 Answers

Normal Crosshair (crosshair IS the mouse) 1 Answer

Setting Scroll View Width GUILayout 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