• 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
Question by Wekthor · Sep 13, 2013 at 12:26 PM · physicsraycastlagrayframe

Raycast Lag - DebugDrawRay always one frame behind

Hi, i noticed some issue with my wheel collider setup, physics for my wheel are based on raycast from wheel collider center, but i guess its messed up because the center of wheel is registered behind actual center of the collider. Seems like it is previous frame position.

I am doing raycast in fixed update. I lowered the timestep significantly. Its raycasting down in local direction. I.e. inversed transformed direction, so is always aiming down properly, but this is probably quite issue if the position is registered behing the actual center of the collider.

Edit: When i put Debug Draw ray in Update() it is not lagging. Thats quite weird, i would expect opossite result, since as far as i know all physics calculations/raycasts should be done in FixedUpdate().

Anyway a bit of the requested code:

This is the stuff that basicly concernes rigidoby and raycast, there is much more code, but everything else doesnt relate directly to moving rigidbody.

 void FixedUpdate()
 {
    rigidbody.AddRelativeForce(Vector3.forward * speed * Time.deltaTime);
 
    if(Physics.Raycast(frontWheelCenter, Vector3.down, out hit)
    groundPoint = hit.point;
 
    Debug.DrawRay(frontWheelCenter, Vector3.down, Color.red);
 
 }


I tried creating simple cube and adding only this code and it was lagging(1 frame offseted ). But its not happening when its in Update().

 void FixedUpdate()
 {
    rigidbody.AddRelativeForce(Vector3.forward * speed * Time.deltaTime);         
    Debug.DrawRay(this.transform.position, Vector3.down, Color.red);
      
  }


Any help greatly appreciated.

alt text

screen shot 2013-09-13 at 12.59.54 pm.png (96.1 kB)
Comment

People who like this

0 Show 9
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 Hoeloe · Sep 16, 2013 at 12:16 PM 0
Share

We need a lot more of your code (actually, any at all would be helpful) to help here. The problem may occur due to when you're doing the raycast, and when you're applying the movement. Also, Unity's physics does have a "margin of error" for colliders, to allow for smoother collisions, so it may be that you're seeing if, for example, you're drawing a ray and then moving the object.

avatar image Fattie · Sep 16, 2013 at 12:19 PM 0
Share

it seems completely strange, and as Hoe explains it is meaningless without the code! Well done on posting an image.

avatar image Wekthor · Sep 16, 2013 at 02:35 PM 0
Share

sorry, can post code, i got 1500 lines of the code, everything works pretty well, but i dont understand why the rays are registered one frame behind and if thats normal. Seems as it aint :) Well, i got rigidbody, i move it basicly with AddRelativeForce. I draw the rays after this, but i tried to swap the functions and the result is still same, still one frame behind. Everything is in fixed update. In the code there is nothing realy non standard concerning moving the rigidbody. Also i am quite positive on the one frame lag, since with slow speed the ray position is just little bit behind wheel while at high speeds the position difference is quite significant.

avatar image Fattie · Sep 16, 2013 at 02:37 PM 0
Share

just add Debug.Log lines at the relevant points and you'll soon figure out what is going wrong. Recall you can pause in the editor, and actually step through one frame at a time. Hope it helps

avatar image Wekthor · Sep 16, 2013 at 02:43 PM 0
Share

thanks, i did all those things tho, i got also three different debug rays, because i am rotating wheel collider to always face to ground with its ray, getting inverse normals to rotate wheel shadows etc. But although i went over each line roughly million times i never noticed any issue with the debug ray ( although its one frame behind:)). I am also quite positive that the bebug ray is called at right spot, since thats where the physics happen and it works well. I just wanted to know if this is something that happens with higher fixed time step or if this issue occurs at some cases, but it does seem like its some bug in my code then :/

Show more comments

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Trouble with Ray & Raycasting and AddForce() 1 Answer

Upper limits of Raycasting; Physics engine "gives up" 0 Answers

Physics.Raycast intermittently stops working 1 Answer

HOW DO I USE RAYCASTHIT 2D???? 1 Answer

Don't allow raycasts to go through colliders? 3 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