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 WintryBlaze · Nov 28, 2018 at 06:10 PM · animationpositionbones

Why is transform.position giving a different result when in Update and a coroutine?

Basically, I have dragged a bone into the inspector and I'm using Debug.DrawRay to visualize the bone's position in the scene view. But for some reason, when using Debug.DrawRay in a coroutine, it doesn't follow the bone properly. The ray drawn from Update() follows the bone perfectly throughout the animation, but the ray drawn from the coroutine doesn't. What am I missing?

The thing I don't understand is that they are both starting at visionTransform.position so I expect to see them originate from the same place, but they don't.

 void Update() {
     Debug.DrawRay(visionTransform.position, Vector3.up, Color.blue);
 }

 IEnumerator Test() {
     while (true) {
         Debug.DrawRay(visionTransform.position, Vector3.up, Color.red);
         yield return 0;
     }
 }

The weird thing is though, the ray drawn in the coroutine sometimes snaps to the correct position.

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
0
Best Answer

Answer by WintryBlaze · Nov 29, 2018 at 10:52 AM

So the issue seemed to be with the asset FinalIK, specifically AimIK, which is a component that manipulates the spine bones to point a weapon precisely at a target. Turns out I needed to uncheck the "Fix Transforms" option. This fixes bones to their initial state each Update() which I assume is why the coroutine was showing the bone at its initial position, since it runs after Update(). Thanks to everyone who commented, it made me think about what the issue could be.

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
avatar image
-3

Answer by mayur7garg · Nov 28, 2018 at 06:24 PM

It might be because the Update and the Coroutine are being called at different frequencies. Update is being called every frame whereas coroutine much more quickly. Try using yield return new WaitForSeconds(Time.deltaTime); and see if that improves your case.

Comment
Add comment · Show 8 · 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 WintryBlaze · Nov 28, 2018 at 06:30 PM 0
Share

Thanks, I tried it but still got the same problem. I believe coroutines which yield return 0 or null always run after update anyway.

avatar image ecv80 · Nov 28, 2018 at 06:37 PM 0
Share

How about yield return new WaitForEndOfFrame(); ? Wouldn't that be more accurate/reliable?

avatar image Bonfire-Boy · Nov 28, 2018 at 06:46 PM 2
Share

This answer is based on a misconception - Coroutines get an update once per frame, just the same as the Update function.

And yes, using Time.deltaTime there also makes little sense (wait for as long as the previous frame took... why?!). yield return null (or WaitForEndOfFrame) should work fine.

avatar image ecv80 Bonfire-Boy · Nov 28, 2018 at 08:46 PM 0
Share

Thank you for clearing this up. I thought they ran as fast as possible and users were responsible of the ti$$anonymous$$g through the WaitFor... calls. So this means WaitForSeconds only gets as accurate as the nearest frame to the specified time?

avatar image Bonfire-Boy ecv80 · Nov 28, 2018 at 09:00 PM 2
Share

Yes WaitForSeconds just skips as many frames as it takes to get to the total duration you specify. It will almost always take a fraction longer, of course. You can't predict how long each frame is going to take, but you do know how long the last one took (Time.deltaTime) so if you need to know how long was actually waited for you can work it out retrospectively (i.e. after the waitforseconds)

The Time class keeps track of the time between the start of one frame and the next- that's what deltaTime is.

avatar image PizzaPie · Nov 28, 2018 at 06:52 PM 0
Share

That s wrong Update is called once per frame no more no less, exactly once per frame. Now coroutines can be "paused" based the yield statment provided, ex.

 IEnumarator UpdateRoutine(){
       for(;;){
          //code
          yield return null;
        }
 }

is equivalent to Update() with main difference it is called afterwards (but in the same frame)

Anyway, for the question try to use exact same statements (either Vector3.up, or Vector3.forward, not both)

As for WaitForEndOfFrame won't be wrong as AnimationInternalUpdate(if root motion used) occure before it and Update before that thus it is closer to LateUpdate? execution order

avatar image GamitusLabs · Nov 28, 2018 at 06:59 PM 1
Share

You are correct about the co-routines always being after update as seen in the Unity docs: https://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html

I'm afraid you've given very little to go off in the way of being able to help debug the issue. Can you provide some screen shots of the render/values? Why are you drawing the ray from separate functions?

Is it necessary to render the forward vector after the update due to some other calculations not seen here? Perhaps the LateUpdate function would be a better option?

avatar image Bonfire-Boy GamitusLabs · Nov 28, 2018 at 07:11 PM 0
Share

I was wondering about LateUpdate, but that runs after the animation update, whereas both Update and Coroutine run before it. So it's hard to see how they could have different values (unless something else is changing the position). I would definitely Debug.log the 2 positions just to see whether it's the position that's different or something in the rendering of the line.

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

267 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Animation switching but rotation isn't proper! 0 Answers

Animation not at correct position W/Video 0 Answers

Make two game objects interact via animation JS 1 Answer

Positioned Blender models with animation snap back to 0,0,0 1 Answer

Animation problem 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