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 RainnFai · Mar 07, 2015 at 04:19 AM · unity5canvasupdate

New Canvas slow update

Hello there,

I'm using the new canvas system to display enemies health bars. I update the position of the canvas-image using WorldToScreenPoint and all is fine BUT the update rate of the canvas seems to be slower compared to the game update. If I move the camera quickly, the health bars move much slower, they seems to lag (my frame-rate is good so I don't think it's related to frame-rate).

Any suggestion? Does the canvas actually update slower? Am I missing something?

Thank you!

Comment
Add comment · Show 2
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 RainnFai · Mar 08, 2015 at 05:06 AM 0
Share

LateUpdate didn't make much difference. I did try a scene with just one 3D object and one UI image following the object (WorldToScreenPoint). The UI image follows the 3D object slowly, about one frame behind. Any advice? Anybody ever had a similar issue?

avatar image RainnFai · Mar 08, 2015 at 05:22 AM 0
Share

I did manage to solve the problem. I had to manually change the execution order of my health bar script and put it after everything (just late update was not enough). Thank you!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by OndrejP · May 06, 2018 at 11:11 AM

Camera matrix update is delayed until very late in frame (probably updates just before rendering). WorldToScreenPoint relies on matrix to calculate screen position.


To make it work correctly you have to do two things:

  1. Ensure HealthBar update is called AFTER input handling (which moves camera)

  2. Force Camera matrix to be updated


The code:

 using UnityEngine;
 public class HealthBarPositionUpdate : MonoBehaviour
 {
     public GameObject Enemy;
     public RectTransform HealthBarElement;

     // LateUpdate ensures that camera position was already updated
     private void LateUpdate()
     {
         Camera.main.ResetWorldToCameraMatrix(); // Force camera matrix to be updated
         var targetPos = Camera.main.WorldToScreenPoint(Enemy.transform.position);
         if (targetPos.z > 0)
         {
             var pos = targetPos.Round(); // Pixel snapping
             pos.z = 0;
 
             HealthBarElement.SetPositionAndRotation(pos, Quaternion.identity);
         }
     }
 }



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

22 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

Related Questions

Continue work on project from 4.6 to 5.0 1 Answer

troubles with canvas and ?culling mask? 0 Answers

Scrollview Without Canvas 1 Answer

how can i use spot light in canvas 2d 0 Answers

Enable/Disable Blocks Raycasts within a Canvas Group using code [Help Appreciated] 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