Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by luismgf321 · May 10, 2020 at 02:15 AM · c#camerarecttransformworldtoscreenpointindicator

Strange behaviour with WorldToScreenPoint

Hello, I am trying to make an off-screen target indicator (COD revive style) with WorldToScreenPoint, it works as expected on the orthographic camera, but in perspective it behaves strangely when z position is negative. Any suggestion is appreciated. Video of the behaviour.

 using UnityEngine;
 
 public class ReviveIndicator : MonoBehaviour
 {
     public RectTransform indicator;
     public Transform target;
 
     Camera mainCamera;
 
     void Start()
     {
         mainCamera = Camera.main;
     }
 
     void Update()
     {
         Vector3 pos = mainCamera.WorldToScreenPoint(target.position);
  
         if(pos.z < 0)
         {
             pos *= -1;
         }
 
         pos.x = Mathf.Clamp(pos.x, 0, Screen.width);
         pos.y = Mathf.Clamp(pos.y, 0, Screen.height);
 
         indicator.position = pos;
     }
 }
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

1 Reply

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

Answer by luismgf321 · May 10, 2020 at 06:56 AM

I got it.

 using UnityEngine;
 
 public class ReviveIndicator : MonoBehaviour
 {
     [Header("References")]
     public RectTransform indicator;
     public Transform target;
     
     Transform canvas;
 
     Camera mainCamera;
 
     [Header("Variables")]
     public Vector3 offset;
 
     Vector3 screenCentre;
 
     void Start()
     {
         canvas = UIReferences.instance.transform;
         mainCamera = Camera.main;
     }
 
     void Update()
     {
         Vector3 screenPosition = mainCamera.WorldToScreenPoint(target.position);
 
         if (IsTargetVisible(screenPosition))
         {
             Vector3 pos = mainCamera.WorldToScreenPoint(target.position + offset);
 
             indicator.position = Vector3.MoveTowards(indicator.position, pos, 500 * Time.deltaTime);
 
             return;
         }
 
         screenCentre = new Vector3(Screen.width, Screen.height, 0) / 2;
 
         screenPosition -= screenCentre;
 
         if (screenPosition.z < 0)
         {
             screenPosition *= -1;
         }
 
         float angle = Mathf.Atan2(screenPosition.y, screenPosition.x);
         float slope = Mathf.Tan(angle);
 
         if (screenPosition.x > 0)
         {
             screenPosition = new Vector3(screenCentre.x, screenCentre.x * slope, 0);
         }
         else
         {
             screenPosition = new Vector3(-screenCentre.x, -screenCentre.x * slope, 0);
         }
 
         if (screenPosition.y > screenCentre.y)
         {
             screenPosition = new Vector3(screenCentre.y / slope, screenCentre.y, 0);
         }
         else if (screenPosition.y < -screenCentre.y)
         {
             screenPosition = new Vector3(-screenCentre.y / slope, -screenCentre.y, 0);
         }      
 
         screenPosition += screenCentre;
 
         float x = indicator.rect.width * canvas.localScale.x / 2;
         float y = indicator.rect.height * canvas.localScale.y / 2;
 
         screenPosition.x = Mathf.Clamp(screenPosition.x, x, Screen.width - x);
         screenPosition.y = Mathf.Clamp(screenPosition.y, y, Screen.height - y);
 
         indicator.position = Vector3.MoveTowards(indicator.position, screenPosition, 500 * Time.deltaTime);
     }
 
     bool IsTargetVisible(Vector3 screenPosition)
     {
         if(Player.instance != null)
         {
             if (Vector3.Distance(target.position, Player.instance.transform.position) >= 14)
             {
                 return false;
             }
         }
 
         bool isTargetVisible = screenPosition.z > 0 && screenPosition.x > 0 && screenPosition.x < Screen.width && screenPosition.y > 0 && screenPosition.y < Screen.height;
         return isTargetVisible;
     }
 }
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

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

Simple Camera Switch Using C# 3 Answers

How to get photo image and work with it 0 Answers

C# Script for 3rd Person Camera? 1 Answer

Using Camera.main.ViewportToWorldPoint to limit player movement in the Y direction 0 Answers

Received an error: RenderTexture's anti-aliasing changed from 2 to 1 due hardware limitations. 0 Answers


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