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 dfghdh · Mar 21, 2013 at 02:44 PM · raycastfpsperspective

Question About Ray casting in an Area and Perspective

Hi,

I am making the code for tracking on my missile launcher (not the missile but the target). The target is not acquired instantly instead the player must hover the scope over the target for a couple of seconds until it is acquired then fire. However if the target moves out of the area of the cross hair then the timer is reset.

I haven't started coding yet but realise the two biggest problems are working out when the target leaves the area of the scope (considering that the area occupied by the scope is much greater at the targets location due to perspective) using raycasting in an area which I have not done before.

Second problem that I reckon I can solve is making a 2D texture appear over the target which will use Camera.WorldToScreenPoint() to convert the location of the target into a screen location.

I need help with the first problem. How can I ray cast in an area and work out the size of that area at the target?

Thanks

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

2 Replies

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

Answer by robertbu · Mar 21, 2013 at 03:22 PM

How difficult the problem is depends on how precise you want to be and how you define 'within the cross hair'. For something simple like making sure the center position of the target stays within a circular area, Simply use WorldToScreenPoint() and compare the distance of the screen center position of cross hair with position of the target. If it is below some radius, then it is within the crosshair. By converting to screen coordinates, you've already projected the 3D world position onto a 2D plane. If you want to make sure that the entire target stays within the cross hair, then you will have to compare the cross hair screen position with the eight bounding box positions of Renderer.bounds. Since bounds will likely include a bit more than the actual target, this is not perfect.

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 dfghdh · Mar 21, 2013 at 03:28 PM 0
Share

Thanks, the simple approach is what I needed.

avatar image
0

Answer by mxmcharbonneau · Mar 21, 2013 at 03:28 PM

You should probably find the angle between the line from the launcher to the target and the direction forward of your launcher. Something like

 Vector3 direction=target.position-launcher.position; 
 float angle=Vector3.Angle(launcher.forward,direction);

then you check if the angle is in the range you want. Far easier than raycasting to a plane with a variable size.

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 dfghdh · Mar 21, 2013 at 10:07 PM 0
Share

This was my finished code, thanks for everyones help:

 //RocketLauncer.js
     function Track()
     {
         if (CheckTracking())
         {        
             var trackTexture : TrackingIcon = GameObject.Find("TrackingIcon").GetComponent(TrackingIcon);
             
             trackTexture.Track(target);
             if (trackTime < 6)
             {
                 trackTime += Time.deltaTime;
                 trackTexture.blink = true;
                 if (trackTime < 2.0)
                 {
                     trackTexture.blinkSpeed = 1.0;
                 }
                 else if (trackTime < 4.0 && trackTime > 2.0)
                 {
                     trackTexture.blinkSpeed = 0.7;
                 }
                 else if (trackTime < 6.0 && trackTime > 4.0)
                 {
                     trackTexture.blinkSpeed = 0.3;
                 }
             }
             else
             {
                 trackTexture.blink = false;
                 canFire = true;
             }
         }
         else
         {
             ResetTracking();
         }
     }
     
     function AquireTarget()
     {
         var ray = Camera.main.ScreenPointToRay(Vector3(Camera.main.pixelWidth/2, Camera.main.pixelHeight/2, 0));
         var rayHit : RaycastHit;
         if (Physics.Raycast(ray, rayHit))
         {
             if (rayHit.collider.gameObject.tag == "Target")
             {
                 target = rayHit.collider.gameObject.transform;
                 tracking = true;
                 GameObject.Find("TrackingIcon").GetComponent(TrackingIcon).Target = target;
                 GameObject.Find("TrackingIcon").GetComponent(TrackingIcon).Enable();
             }
         }
     }
     function CheckTracking() : boolean
     {
         var targetPos = Camera.main.WorldToScreenPoint(target.position);
         var targetPos2D : Vector2;
         targetPos2D = new Vector2(targetPos.x, targetPos.y);
         var screenCentre : Vector2;
         screenCentre = new Vector2(Camera.main.pixelWidth/2, Camera.main.pixelHeight/2);
         
         var distance : double;
         distance = Vector2.Distance(screenCentre, targetPos2D);
         
         if (distance > 140.0)
         {
             return false;
         }
         else
             return true;
     }
     function ResetTracking()
     {
         tracking = false;
         canFire = false;
         GameObject.Find("TrackingIcon").GetComponent(TrackingIcon).Disable();
         trackTime = 0;
     }
 //TrackingTexture.js
 var Target : Transform;
 var enabledTracking : boolean = false;
 var blink: boolean = false;
 var blinkTimer : double = 0;
 var blinkSpeed : double = 0.7;
 function Start () {
 
 }
 
 function Update () 
 {
     if (enabledTracking)
     {
         if (blink)
         {
             blinkTimer += Time.deltaTime;
             if (blinkTimer >= blinkSpeed)
             {
                 blinkTimer = 0;
                 if (guiTexture.enabled == true)
                     guiTexture.enabled = false;
                 else
                     guiTexture.enabled = true;
             }
         }
         else
             guiTexture.enabled = true;
     }
 }
 function Track(target: Transform)
 {
     var screenPos : Vector3;
     screenPos = Camera.main.WorldToViewportPoint(target.position);
     transform.position = screenPos;
 }
 function Enable()
 {
     guiTexture.enabled = true;
     enabledTracking = true;
 }
 function Disable()
 {
     guiTexture.enabled = false;
     enabledTracking = false;
 }

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

Raycast Decals,Sound and MuzzleFlash not working too well 0 Answers

bullet hole problem/raycast problem || help me please 1 Answer

How to use raycast when standing still. 1 Answer

Raycast Not Rotating 1 Answer

Raycasr in my fps? 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