Quantcast
Channel: Questions in topic: "parenting"
Viewing all articles
Browse latest Browse all 302

Physics.Raycast lagging behind

$
0
0
I am making a game where players can be onboard fast moving ships. These ships have controls in the form of panels and I use raycasting to find out which one, if any, the player is currently aiming at. The ships are moved using forces and physics while the player merely keeps the same relative position through a script. Each panel has a kinematic rigidbody, and is a child of the ship rigidbody. When the ship moves slowly things work perfectly, but when the speed is high raycasts aimed at the leading side of the panels cease to hit. Through debugging I've managed to confirm that the raycast originates at the correct position, and it does cross the panel, but Physics.Raycast won't hit it. Getting the actual collider and using its Raycast method does however hit. Raycasting from where the ship is going to where the ship was before (ie head on) hits perfectly, the hit is where the edge of the collider is, not embedded inside it. I'm guessing Physics.Raycast checks if the ray crosses the bounds of a rigidbody before doing more precise calculations against the actual collider, and that in this case the bounds of my panel isn't correctly placed (the data is one fixedupdate old). What is causing this, is it a bug in unity or intended, and how might I work around it? --------------------------- Edit: I did some more testing and managed to make a minimal example. Using this script: using UnityEngine; using System.Collections; public class RaycastTester : MonoBehaviour { void Update() { RaycastHit hit; if (Physics.Raycast(transform.position, transform.forward, out hit)) { Debug.DrawLine(transform.position, hit.point, Color.red); } else { Debug.DrawRay(transform.position, transform.forward * 10); } } } In the scene I set up a cube with a rigidbody and a constant force, meaning it will continuously accelerate, and then attached a child kinematic rigidbody. In addition I attached two empties to the parent, gave them the script above, and aimed them so that the raycasts would hit the leading edge of each cube. Shortly after hitting play: ![alt text][1] After letting the cube accelerate for a little while: ![alt text][2] The selected cube is the parent and they move towards positive X. Thus it seems to me that movement through parenting happens after all physics is done, presumably in every Update or thereabout. Moving the Raycast to FixedUpdate causes the Raycast to happen before things move giving the appearance of the lines lagging one frame behind in position, but not changing whether the raycasts hit or not. [1]: /storage/temp/28729-screen+shot+2014-07-05+at+23.47.34.png [2]: /storage/temp/28730-screen+shot+2014-07-05+at+23.48.08.png

Viewing all articles
Browse latest Browse all 302

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>