private void CounterSlopes(Vector3 groundNormal) { Vector3 carForward = transform.right; Vector3 gravity = Physics.gravity; Vector3 directionOfFlat = Vector3.Cross(-gravity, groundNormal).normalized; //the direction that if you head in you wouldnt change altitude Vector3 directionOfSlope = Vector3.Cross(directionOfFlat, groundNormal); //the direction down the slope float affectOfGravity = Vector3.Dot(gravity, directionOfSlope); // returns 1 on a cliff face, 0 on a plane float affectOfWheelAlignment = Mathf.Abs(Vector3.Dot(carForward, directionOfSlope)); // returns 1 if facing down or up the slope, 0 if 90 degrees to slope PhysicsSphere.AddForce(-directionOfSlope * affectOfWheelAlignment * affectOfGravity, ForceMode.Acceleration); }
private void FixedUpdate() { RaycastHit hitOn; RaycastHit hitNear; OnGround = Physics.Raycast(transform.position, Vector3.down, out hitOn, rayMaxDistance); NearGround = Physics.Raycast(transform.position, Vector3.down, out hitNear, rayMaxDistance + 0.8f); VehicleModel.up = Vector3.Lerp(VehicleModel.up, hitNear.normal, Time.deltaTime * 8.0f); VehicleModel.Rotate(0, transform.eulerAngles.y, 0); if (NearGround) { PhysicsSphere.AddForce(transform.forward * speedTarget, ForceMode.Acceleration); PhysicsSphere.AddForce(transform.right * strafeTarget, ForceMode.Acceleration); } else { PhysicsSphere.AddForce(transform.forward * (speedTarget / 10), ForceMode.Acceleration); PhysicsSphere.AddForce(Vector3.down * Gravity, ForceMode.Acceleration); } Vector3 localVelocity = transform.InverseTransformVector(physicsSphere.velocity); localVelocity.x *= 0.9f + (Drift / 10); if (NearGround) { PhysicsSphere.velocity = transform.TransformVector(localVelocity); } if (StopSlopeSlide) { CounterSlopes(hitNear.normal); } }