Guys, I’m getting desperate here. This question was asked several times before by other people, but either my case is set up incorrectly or I don’t understand what I’m actually reading, in any case I do have to resort to wasting your valuable time.
I have a car. This car has a Rigidbody which is set to interpolate and continuous dynamic collision detection. The car has wheelchlildren, which as far as I can see ultimately move the Rigidbody in
public class Wheel : MonoBehaviour
{
[...]
void FixedUpdate ()
{
[...]
body.AddForceAtPosition (suspensionForce + roadForce, pos);
}
}
Additionally, the car has a PlayerObject as child. The player also has a rigidbody in case that matters, set to interpolate and kinematic when in car. Also a camera, which has a script that casts forwards from the camera to the center of the screen on buttonUp:
var vDist : float = 200;
var vHitObj : Transform;
function Update ()
{
if(Input.GetButtonUp("Fire2"))
{
Cast();
}
}
function Cast()
{
yield new WaitForFixedUpdate ();
var hit : RaycastHit;
if (Physics.Raycast (this.transform.position, this.transform.forward, hit, vDist))
{
Debug.DrawLine (this.transform.position, hit.point);
vHitObj = hit.collider.transform;
print(vHitObj);
vHitObj.SendMessage ("OnHit");
}
}
Furthermore, the car has multiple cubes that represent switches and such, to be clicked by the player. They have only a collider set to trigger, and a script that receives messages and triggers an animation:
function OnHit()
{
if(this.transform.parent.GetComponent(DoorCar).vStatus == 1)
{
this.transform.parent.GetComponent(DoorCar).vStatus = 0;
this.transform.parent.GetComponent(Animation).Play("CarDoorLeftOpen");
print("Door Open");
}
else
{
this.transform.parent.GetComponent(DoorCar).vStatus = 1;
this.transform.parent.GetComponent(Animation).Play("CarDoorLeftClose");
print("Door Closed");
}
}
Now, that works just fine when the car stands still or moves slowly. When I get over 50mph my raysasts start missing the cube and return the terrain behind them 9 out of 10 times. Now, as I said a google search returns multiple cases of this happening, I took care to have colliders where I should, no rigidbodies where I shouldn’t, increased and lowered the timestep, as far as I can see I’m doing everything in FixedUpdate, but I have a feeling I missunderstand that somehow, I don’t feel like I have control over when the position updates and when the casts trigger. Using layers is often suggested but it doesn’t sound like my problem, It’s not that I’m hitting colliders in front of what I want to hit, it’s that I’m hitting stuff behind it. There was a nice solution that used a polygon library to raycast entirely without colliders, but alas, that doesn’t work on moving meshes.
So yeah, please give me any insight you have on this, I just wanna click on multiple small-ish objects that move and trigger their scripts. I’d prefer I did something really stupid since that usually means less work, but really any new information is greatly appreciated.
Thanks for reading.