I’m just trying to get the darn thing to work, I’ll worry about performance later. I do have my own thread but here is some code I have been toying with:
public class Antigrav : MonoBehaviour {
public float agField, floatHeight;
public Vector3 buoyancyCentreOffset1, buoyancyCentreOffset2, buoyancyCentreOffset3, buoyancyCentreOffset4;
public float bounceDamp;
public float AGfactor = 2;
public RaycastHit hit;
void Update(){
if (Physics.Raycast(transform.position, -Vector3.up, out hit, /*floatHeight*/Mathf.Infinity)) {
Debug.Log(hit.collider.tag + " within " + hit.distance + " meters.");
if (hit.collider.tag == "Track"){
agField = (float)hit.distance + (float)hit.transform.localPosition.y;
Vector3 actionPoint1 = transform.position + transform.TransformDirection(buoyancyCentreOffset1);
Vector3 actionPoint2 = transform.position + transform.TransformDirection(buoyancyCentreOffset2);
Vector3 actionPoint3 = transform.position + transform.TransformDirection(buoyancyCentreOffset3);
Vector3 actionPoint4 = transform.position + transform.TransformDirection(buoyancyCentreOffset4);
float forceFactor1 = AGfactor - ((actionPoint1.y - agField) / floatHeight) / (float)hit.distance;
float forceFactor2 = AGfactor - ((actionPoint2.y - agField) / floatHeight) / (float)hit.distance;
float forceFactor3 = AGfactor - ((actionPoint3.y - agField) / floatHeight) / (float)hit.distance;
float forceFactor4 = AGfactor - ((actionPoint4.y - agField) / floatHeight) / (float)hit.distance;
if (forceFactor1 > 0f) {
Vector3 uplift1 = -Physics.gravity * (forceFactor1 - rigidbody./*angularV*/velocity.x * bounceDamp);
rigidbody.AddForceAtPosition(uplift1, actionPoint1);
}
if (forceFactor2 > 0f){
Vector3 uplift2 = -Physics.gravity * (forceFactor2 - rigidbody.velocity.x * bounceDamp);
rigidbody.AddForceAtPosition(uplift2, actionPoint2);
}
if (forceFactor3 > 0f){
Vector3 uplift3 = -Physics.gravity * (forceFactor3 - rigidbody./*angularV*/velocity.x * bounceDamp);
rigidbody.AddForceAtPosition(uplift3, actionPoint3);
}
if (forceFactor4 > 0f){
Vector3 uplift4 = -Physics.gravity * (forceFactor4 - rigidbody./*angularV*/velocity.x * bounceDamp);
rigidbody.AddForceAtPosition(uplift4, actionPoint4);
}
}
}
}
}
I still think this, although sloppy, is the better way to go. I get a consistent hovering effect but it seems to occur at one point - WipEout 1 - 3 physics. I am aiming for WipEout PurE or HD -like floaty physics. I had a revelation in the shower (where all great ideas come from :P) yesterday after reading this thread and XYZ’s - I need to place multiple raycasts on my craft. My code supports this so I decided to incorporate that idea as well as dramatically clean up my code - this is what I’ve got:
public class Antigrav : MonoBehaviour {
// Publics
public float agField, floatHeight;
public Vector3 buoyancyCentreOffset1, buoyancyCentreOffset2, buoyancyCentreOffset3, buoyancyCentreOffset4;
public float bounceDamp; // should be between 0 and 1
public float AGfactor = 1;
public RaycastHit hit1;
public RaycastHit hit2;
public RaycastHit hit3;
public RaycastHit hit4;
// Privates
private bool ray1;
private bool ray2;
private bool ray3;
private bool ray4;
private float forceFactor1;
private float forceFactor2;
private float forceFactor3;
private float forceFactor4; //= AGfactor - ((actionPoint4.y - agField) / floatHeight) / (float)hit.distance;
//others
Vector3 actionPoint1;
Vector3 actionPoint2;
Vector3 actionPoint3;
Vector3 actionPoint4;
void Update(){
actionPoint1 = transform.position + transform.TransformDirection(buoyancyCentreOffset1);
actionPoint2 = transform.position + transform.TransformDirection(buoyancyCentreOffset1);
actionPoint3 = transform.position + transform.TransformDirection(buoyancyCentreOffset1);
actionPoint4 = transform.position + transform.TransformDirection(buoyancyCentreOffset1);
ray1 = Physics.Raycast(actionPoint1, -Vector3.up, out hit1, Mathf.Infinity);
ray2 = Physics.Raycast(actionPoint2, -Vector3.up, out hit2, Mathf.Infinity);
ray3 = Physics.Raycast(actionPoint3, -Vector3.up, out hit3, Mathf.Infinity);
ray4 = Physics.Raycast(actionPoint4, -Vector3.up, out hit4, Mathf.Infinity);
// Determine object below and distance from said object relative to each ray source/action point.
// Set agField and calculate forceFactor.
if (ray1) {
Debug.Log(hit1.collider.tag + " within " + hit1.distance + " meters of actionPoint1.");
if (hit1.collider.tag == "Track"){
agField = (float)hit1.distance * floatHeight;
forceFactor1 = AGfactor - ((actionPoint1.y - agField) / floatHeight);
}
}
if (ray2) {
Debug.Log(hit2.collider.tag + " within " + hit2.distance + " meters of actionPoint2.");
if (hit2.collider.tag == "Track"){
agField = (float)hit2.distance;
forceFactor2 = AGfactor - ((actionPoint2.y - agField) / floatHeight);
}
}
if (ray3) {
Debug.Log(hit3.collider.tag + " within " + hit3.distance + " meters of actionPoint3.");
if (hit3.collider.tag == "Track"){
agField = (float)hit3.distance;
forceFactor3 = AGfactor - ((actionPoint3.y - agField) / floatHeight);
}
}
if (ray4) {
Debug.Log(hit4.collider.tag + " within " + hit4.distance + " meters of actionPoint4.");
if (hit4.collider.tag == "Track"){
agField = (float)hit4.distance;
forceFactor4 = AGfactor - ((actionPoint4.y - agField) / floatHeight) / (float)hit4.distance;
}
}
// Apply force
if (forceFactor1 > 0f) {
Vector3 uplift1 = -Physics.gravity * (forceFactor1 - rigidbody./*angularV*/velocity.y * bounceDamp);
rigidbody.AddForceAtPosition(uplift1, actionPoint1);
}
if (forceFactor2 > 0f){
Vector3 uplift2 = -Physics.gravity * (forceFactor2 - rigidbody.velocity.y * bounceDamp);
rigidbody.AddForceAtPosition(uplift2, actionPoint2);
}
if (forceFactor3 > 0f){
Vector3 uplift3 = -Physics.gravity * (forceFactor3 - rigidbody./*angularV*/velocity.y * bounceDamp);
rigidbody.AddForceAtPosition(uplift3, actionPoint3);
}
if (forceFactor4 > 0f){
Vector3 uplift4 = -Physics.gravity * (forceFactor4 - rigidbody./*angularV*/velocity.y * bounceDamp);
rigidbody.AddForceAtPosition(uplift4, actionPoint4);
}
}
}
This code, surprisingly, doesn’t work the way I had hoped. It never applies enough uplift and when I manually drag it off the track, it dips it’s nose down and does a face/nose plant into the track - I am confused with this one… :?