- Home /
OnCollisionStay & FixedUpdate
I think i have a sync problem with OnCollisionStay & FixedUpdate.
I need local contact points. I am using transform.InverseTransformPoint
When i use this script on 4 moving rigid, 3 of them works successfully. But one of them calculating old (one frame ago) contact points with new position (center of transform changed so locals are wrong).
How can i solve this problem?
problem not about Update or FixedUpdate. OnCollisionStay generates this problem. transform.InverseTransformPoint in OnCollisionStay function.
Answer by SuperProgrammer · Feb 08, 2014 at 09:26 AM
Problem solved. Store last position: Add Below:
private Vector3 lastrigidposition;
void Start() {
lastrigidposition = transform.position;
}
void FixedUpdate() {
lastrigidposition = transform.position;
}
Now you only need to check is contact possible on its bounds.
For example: For Sphere Collider: if local positon magnitude greater then sphere Radius we need to use lastrigidposition because a sphere can only collide around Radius.
transform.InverseTransformPoint(position)
equal position - transform.point
So oldlocal = position - lastrigidposition;
Dont forget there can be multiple contacting rigidbody. Maybe you need to change other rigidbody calculations too. So maybe you need a few more variables.
If you drawrays you will see two different positions:
void OnCollisionStay(Collision collisionInfo) {
Debug.DrawRay(transform.position, transform.up * 4, Color.green);
Debug.DrawRay(lastrigidposition, transform.up * 4, Color.white);
}