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 Fierce Waffle · Jan 06, 2011 at 12:09 AM · fpscolorchange

Making FPS Reticule Change

I am trying to make i so when My reticule(characters Z) goes over an object tagged enemy it changes the reticule to red.

Heres my code so far

var target:Transform; var range = 10.0; var reticule:GUITexture; var colorOn:Color; var colorOff:Color;

function Awake() { if(!target) { target = GameObject.FindWithTag("Enemy").transform; } }

function Update() { if(target && CanAttackTarget()) { reticule.color = colorOn; }

 else// if(target && !CanAttackTarget())
 {
     reticule.color = colorOff;
     //var defaultLookRotation = Quaternion.LookRotation(defaultLook.position - transform.position, Vector3.up);
     //transform.rotation = Quaternion.Slerp(transform.rotation, defaultLookRotation, Time.deltaTime * damp);
     print("NO");
 }

}

function CanAttackTarget() { //Check if target is in range if(Vector3.Distance(transform.position, target.position) > range) { print("Not in Range"); return false; }

 var hit : RaycastHit;
 //Check if theres interferance between the two
 if(Physics.Linecast(transform.position, target.position, hit))
 {
     if(hit.collider.gameObject.tag != "Enemy")
     {
         print("Interferance from : " + hit.collider.gameObject.name);
         return false;
     }

     else
     {
         print("enemy detected");
         return true;
     }
 }

 return true;

}

the only problem is when even when Im not looking at it its red, but when Im not in the specified range it turns white. I wan it to turn white when Im not looking at it and red when I am (from the distance specified as range)

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 Justin Warner · Jan 06, 2011 at 12:30 AM 0
Share

Okay, sorry. Lol, hope someone can help you then.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by The_r0nin · Jan 06, 2011 at 02:00 AM

First of all, a raycast is from position to position... it has nothing to do with "looking at" something. It is simply a line drawn from one point to another. So when you raycast in your code, you are simply asking if a line can be drawn from you to the target, not if your camera can "see" it. What you want is Camera.main.ScreenPointToRay.

If you check the documentation, you'll find that this will allow you to project your ray through the camera's viewpoint towards a target.

var ray : Ray = camera.ScreenPointToRay (Vector3(mousePosition.x,mousePosition.y,0));
var hit : RaycastHit;
if(Physics.Raycast(ray, hit)){
    // do your tag check here
}

That should determine if there is interference between your mouse position and the target. You may have to invert the mousePosition.y (with something like [Screen.height-mousePosition.y] to get the correct mouse position for the ScreenPointToRay... I forget).

Another thing you might want to look at is renderer.isVisible. That might be useful as well...

P.S.: If you put your distance in the Raycast, you no longer need your first "if" statement. Granted, you'll be Raycasting every frame, so you might want to keep it for optimization, but it isn't necessary:

if (Physics.Raycast (ray, hit, range)){
   // do your tag check here
}
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 Fierce Waffle · Jan 06, 2011 at 02:18 AM 0
Share

i get an error saying Assets/Halo/Scripts/reticuleControl.js(41,20): BCE0023: No appropriate version of 'UnityEngine.Physics.Linecast' for the argument list '(UnityEngine.Ray, UnityEngine.RaycastHit)' was found.

avatar image The_r0nin · Jan 06, 2011 at 12:36 PM 0
Share

Ahhh! When I cut and pasted from your code, I missed that you had used Linecast. Use RayCast ins$$anonymous$$d... I've edited the code above.

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

No one has followed this question yet.

Related Questions

gui.label color change 1 Answer

changing the color of particles using the legacy particle animator? 2 Answers

Invert The Color Of A GUIText Crosshair? 1 Answer

How would I change the colour of a prefab relative to the size of the character? 1 Answer

Blend into next color instead of jumping to next color 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