- Home /
Help In Making a SphereCast for 3D Tire! Working RayCast Script included!
Hello,
I am wondering if using a Sphere Raycast is a good idea for a vehicle tire? I want to develop a rock Crawler game :D iOS.
DOCS: http://docs.unity3d.com/Documentation/ScriptReference/Physics.SphereCast.html
Is high speeds a problem? What are the concerns and is it hard to do?
HERE is my FULLY working Raycast Tire system, I would LOVE it if someone could help me transpose it to using SphereCast :D.
CODE:
var wheels:iWheels[];
private var skid:Skidmarks;
private var slip:float=1.0;
private class iWheels {//Wheel set
var collider : WheelCollider;
var wheelPref : Transform;
var driveWheel : boolean = false;
var steerWheel : boolean = false;
var rearWheel : boolean = false;
var lastSkidmark : int = -1.0;
var rotationValue : float = 0.0;
}
function Update () {
for(var i:int=0; i<wheels.length; i++){//for each wheel
var hit : RaycastHit;//We will define WHAT the raycast hit here.
var whPref = wheels[i].wheelPref; //For each of the wheels, we will create a prefference based on if this is a (Steerable wheel, drive wheel, front wheel)
var ColliderCenterPoint : Vector3 = wheels[i].collider.transform.TransformPoint(wheels[i].collider.center ); // Lets find the wheel colliders CENTER point in a VECTOR 3, and Give that the name "ColliderCenterPoint".
if (Physics.Raycast( ColliderCenterPoint, -wheels[i].collider.transform.up, hit, wheels[i].collider.suspensionDistance + wheels[i].collider.radius ) ) {// Then lets Cast a ray DOWN to the ground. Raycast( Center of wheel collider, Direction in which we cast the ray! Notice (-) symbol! That means casting DOWN, what ever it hits becomes the "hit" variable, the raycast Distance is ONLY as long as the suspension distance of the wheel collider PLUS the wheels Radius!) Thats it :D!
whPref.position = hit.point + (wheels[i].collider.transform.up * wheels[i].collider.radius); // NOW lets Make the wheel transform (the graphic wheels) rest at the hit point, BUT we **dont** half the tire in the ground, so we mulitply the radius of the tire, to get it to perfectly (touch) the ground.
}else{
whPref.position = ColliderCenterPoint - (wheels[i].collider.transform.up * wheels[i].collider.suspensionDistance);//If the Raycast does NOT hit the ground put it at the FURTHEST point on the suspension distance (the HANGING tire effect).
}
whPref.rotation = wheels[i].collider.transform.rotation * Quaternion.Euler( wheels[i].rotationValue, wheels[i].collider.steerAngle, 0 );//Lets make that (graphic Wheel) the same rotation value and speed as that of the wheel collider!! (NEED to see the tire spinning!!).
wheels[i].rotationValue += wheels[i].collider.rpm * ( 360/60 ) * Time.deltaTime;// More calculation to get the spin just right on the (Graphic Tire).
var CorrespondingGroundHit : WheelHit; //IF the wheel is HITTING THE GROUND,
wheels[i].collider.GetGroundHit(CorrespondingGroundHit); // Oh forgot, this makes sure we are looking at the wheel collider on the WHEEL collider, NOT the graphic tire.
if (Mathf.Abs(CorrespondingGroundHit.sidewaysSlip)>8.0*Time.timeScale&&skid&&wheels[i].collider.isGrounded||Mathf.Abs(CorrespondingGroundHit.forwardSlip)>0.3*Time.timeScale&&skid&&wheels[i].collider.isGrounded) { //Basically lets see if the tire slipped :P.
wheels[i].lastSkidmark=skid.AddSkidMark (hit.point,hit.normal,1.0,wheels[i].lastSkidmark);// If soo! then lets make that SKIDMARKS prefab do its thing! (seperate script)...
slip=Mathf.Abs(CorrespondingGroundHit.sidewaysSlip)/70; //Lets determine HOW much the tire slipped!
}else{
wheels[i].lastSkidmark=-1; //Else if it didnt slip then just make the,
slip=0.0; // Slip equal 0 :D
}
}
}
Thanks
Daniel
It's quite easy if you have any knowledge of physics. If done correctly, you should have accuracy at any speed. The only problem I see, is that it uses a sphere cast, meaning your tires will be spheres and not cilinders, possibly causing other problems :)
Yes, This is true, However i saw that P2 is the variable for the capsules length maybe you could make this halved so the sphere is now squished?
Btw looks like its the same as Ray cast BUT it makes a 3D casting :P so this should be easy!
@$$anonymous$$ G By P2 do you mean the second parameter? It's name is radius and all it does it "make" the sphere smaller, not squashed :)
And yes for all intents and purposes it can be considered a "thick" raycast, however I think the backend is more of a sweeptest :)
I'm having a hard time reading the code, would you $$anonymous$$d improving the format and adding some comments ;)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Only activate chat if T is pressed 0 Answers
How can i make it so animation would play only when button is being pressed? 0 Answers
Help with car 2-player Input 0 Answers