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
1
Question by dylan56477 · Nov 21, 2011 at 10:51 PM · guitexture2draycastingcrosshair

Make a crosshair turn red when over an enemy

Well I know what I need to do, I'm just having trouble writing the code the way unity likes it if u kno what i mean...I want to have a crosshair that turns red when over an enemy by raycasting. But I haven't set up the Red crosshair texture yet. Now I just don't know how to write the script.

This is the normal crosshair script that works fine which is attached to my camera on the first person controller:

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);

 }

}

Then this is my raycasting somewhat...

if(Physics.Raycast(transform.position, transform.forward, hit, 100))

but as you can see it's not complete. Someone told me I needed to use the update function for the raycasting, but as you can see the crosshair is set up with the OnGuI and the Function Start. So that's rly got me confused. Can someone give me a hand here? Thank you so much!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Nov 21, 2011 at 11:50 PM

It's better to use Raycast in Update - OnGUI is called multiple times in each Update cycle and will waste time doing unnecessary Raycasts. You should do the Raycast in Update and compare the hit.transform.tag to "Enemy" (or whatever you tag your enemies), setting a boolean flag when the result is true. In OnGUI, check this variable and change the crosshair texture to the red version when it is true - something like this:

var crosshair: Texture2D; // drag the normal crosshair here var redCrosshair: Texture2D; // drag the red crosshair here

private var isEnemy: boolean = false;

function Update(){ if (Physics.Raycast(transform.position, transform.forward, hit, 100)){ isEnemy = (hit.transform.tag == "Enemy"); // assign the comparison result to is enemy ... } }

function OnGUI(){ var cross = crosshair; // assume normal crosshair if (isEnemy) cross = redCrosshair; // change to red if isEnemy is true GUI.DrawTexture(position, cross); }

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 dylan56477 · Nov 22, 2011 at 09:53 PM 0
Share

Ok looks great but I get a couple of errors. 1 the private isEnemy did u mean that to be a private var? then I get these 2 errors which seems really contradicting look:

Assets/Scripts/New crosshair .js(10,49): UCE0001: ';' expected. Insert a semicolon at the end.

then: Assets/Scripts/New crosshair .js(10,49): BCE0043: Unexpected token: ;.

weird huh??

avatar image aldonaletto · Nov 23, 2011 at 04:01 AM 0
Share

$$anonymous$$y fault: I forgot the var keyword before the variable name. I forgot also to initialize it to false. The whole instruction should be:

 private var isEnemy: boolean = false;

I fixed my answer already.

avatar image
1

Answer by brumdogpro · Nov 23, 2011 at 09:48 AM

I did the same thing but I created it so it would turn green over allies and red over enemies. All I did what set a raycast forward from the camera and added tags to allies and enemies that were something along the lines of "Enemy" or "Ally". Then something like this I guess:

     if(hit.point.gameObject.tag == "Enemy")
 {
 
 currentReticle = redReticle;
 
 }
 else 
 {
 
 currentReticle = regularReticle;
 
 }



Assuming that you defined variables for the regular, red, and green textures.

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 lprussel · Nov 22, 2011 at 01:14 AM

You can also do something along the lines of this:

var Crosshair : GUITexture;

function Update () {

var hit: RaycastHit;

if (Physics.Raycast(transform.position, transform.forward, hit)){

 var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
 if (hit.transform.tag == "Enemy"){
     Crosshair.color = Color.red;
     }
 
 else {
     Crosshair.color = Color.white;
     }
 }

}

Assign this to the camera so that the raycast is sent from the camera, assuming that your crosshair is camera based. This will change the color of the crosshair GUI texture when the raycast collides with an object with the tag "Enemy".

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

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

7 People are following this question.

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

Related Questions

Crosshairs change when sprinting? 3 Answers

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

Normal Crosshair (crosshair IS the mouse) 1 Answer

how do i have a crosshair turn red when over an enemy? 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