Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Yakus-Actual · Dec 27, 2017 at 10:43 PM · uicanvasviewport

Camera Viewport detecting objects behind

Hello Everybody,

I am trying to mark on my Canvas usable points for the player when they click and hold the right mouse button. I calculated the position of the UI elements using @Sylos solution from this thread, but the system is also rendering the points that are behind the player. (Purple circles show correct, and red show the "phantom" ones) Screenshot01 Higher resolution

The code I'm using looks exactly like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GraplingHookPoints : MonoBehaviour {
 
     public GameObject m_Prefab;
     public Camera m_Cam;
     public GameObject m_CanvasRef;
     public RectTransform m_CanvasRect;
 
     public List<Transform> m_HookPoints;
     public List<GameObject> m_HookPointIcons;
     private bool isInitialized = false;
     bool isShowing = false;
 
     Transform m_T;
     Vector2 m_ViewportPosition;
     Vector2 m_WorldObjScreenPos;
 
     // Use this for initialization
     public void Initialize () {
         foreach(Transform t in m_HookPoints)
         {
             m_HookPointIcons.Add(Instantiate(m_Prefab, m_CanvasRef.transform.position, m_CanvasRef.transform.rotation, m_CanvasRef.transform));
         }
         foreach (GameObject go in m_HookPointIcons)
         {
             go.SetActive(false);
         }
 
         m_CanvasRect = m_CanvasRef.GetComponent<RectTransform>();
         isInitialized = true;
     }
     
     // Update is called once per frame
     void Update () {
 
         if (!isInitialized)
             return;
 
         if (Input.GetMouseButtonDown(1))
         {
             ShowHookPoints(true);
         }
 
         if (isShowing)
             UpdateIconPosition();
 
 
         if (Input.GetMouseButtonUp(1))
         {
             ShowHookPoints(false);
         }
 
     }
 
     private void ShowHookPoints(bool isShowingIcons)
     {
         foreach (GameObject go in m_HookPointIcons)
         {
             go.SetActive(isShowingIcons);
         }
         isShowing = isShowingIcons;
     }
 
     private void ShowHookPoint(GameObject hp, bool show)
     {
         hp.SetActive(show);
     }
 
     private void UpdateIconPosition()
     {
         for (int i = 0; i < m_HookPoints.Count; i++) 
         {
             m_T = m_HookPoints[i];
             m_ViewportPosition = m_Cam.WorldToViewportPoint(m_T.position);
             m_WorldObjScreenPos = new Vector2(((m_ViewportPosition.x * m_CanvasRect.sizeDelta.x) - (m_CanvasRect.sizeDelta.x *0.5f)), ((m_ViewportPosition.y * m_CanvasRect.sizeDelta.y) - (m_CanvasRect.sizeDelta.y * 0.5f)));
             m_HookPointIcons[i].GetComponent<RectTransform>().anchoredPosition = m_WorldObjScreenPos;
         }
     }
 }
 

Can anybody advise me on how to make sure that it displays only the icons for the objects on screen, please? N.B. the objects being "traced" have no mesh on them, just a sphere collider

Thank you in advance for all the help!

zrzut-ekranu-2017-12-27-222644.png (420.8 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

126 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

Related Questions

How to fit a camera Viewport Rect to a UI Panel 1 Answer

Scroll view/rect broken on Android only 0 Answers

The most generic way to change ViewPort 0 Answers

Force UIImage to stay on viewport? 0 Answers

Can I combine 2 or more UI masks? 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