void OnTriggerExit(Collider col) { if (ignoreFields) { return; } GravityFieldBase gravField = col.GetComponent <GravityFieldBase>(); if (gravField) { int ind = System.Array.IndexOf(mGravityFields, gravField, 0, mGravityFieldCurCount); if (ind != -1) { gravField.ItemRemoved(this); if (mGravityFieldCurCount > 1) { mGravityFields[ind] = mGravityFields[mGravityFieldCurCount - 1]; } else //restore { up = startUp; gravity = mStartGravity; } mGravityFieldCurCount--; } } }
protected virtual void OnEnable() { if (isGlobal) { mGlobal = this; } }
protected virtual void OnDisable() { if (mGlobal == this) { mGlobal = null; } }
void OnTriggerEnter(Collider col) { if (ignoreFields) { return; } if (mGravityFieldCurCount < mMaxGravityFields) { GravityFieldBase gravField = col.GetComponent <GravityFieldBase>(); if (gravField) { int ind = System.Array.IndexOf(mGravityFields, gravField, 0, mGravityFieldCurCount); if (ind == -1) { mGravityFields[mGravityFieldCurCount] = gravField; mGravityFieldCurCount++; } } } }
// Update is called once per frame protected virtual void FixedUpdate() { if (mGravityFieldCurCount > 0) { bool fallLimit = false; float fallSpeedLimit = Mathf.Infinity; Vector3 newUp = Vector3.zero; float newGravity = 0.0f; int numGravityOverride = 0; for (int i = 0; i < mGravityFieldCurCount; i++) { GravityFieldBase gf = mGravityFields[i]; if (gf && gf.gameObject.activeSelf && gf.enabled) { newUp += gf.GetUpVector(this); if (gf.gravityOverride) { newGravity += gf.gravity; numGravityOverride++; } if (gf.fallLimit) { fallLimit = true; if (gf.fallSpeedLimit < fallSpeedLimit) { fallSpeedLimit = gf.fallSpeedLimit; } } } else //not active, remove it { if (mGravityFieldCurCount > 1) { mGravityFields[i] = mGravityFields[mGravityFieldCurCount - 1]; } mGravityFieldCurCount--; i--; } } if (mGravityFieldCurCount > 0) //in case all were inactive { newUp /= ((float)mGravityFieldCurCount); newUp.Normalize(); up = newUp; gravity = numGravityOverride > 0 ? newGravity / (float)numGravityOverride : mStartGravity; if (fallLimit) { //assume y-axis, positive up if (mBody && !mBody.isKinematic) { Vector3 localVel = transform.worldToLocalMatrix.MultiplyVector(mBody.velocity); if (localVel.y < -fallSpeedLimit) { localVel.y = -fallSpeedLimit; mBody.velocity = transform.localToWorldMatrix.MultiplyVector(localVel); } } } } else //restore { up = startUp; gravity = mStartGravity; } } if (!mGravityLocked) { mBody.AddForce(mUp * gravity * mBody.mass * mMoveScale, ForceMode.Force); } }