void LateUpdate() { var h = rb.position.magnitude; var positionDelta = rb.position - previousPosition; previousPosition = rb.position; var positionNormalized = rb.position.normalized; var t = positionDelta - (positionNormalized * Vector2.Dot(positionDelta, positionNormalized)); rb.rotation -= Mathf.Atan(t.magnitude / h) * Mathf.Sign(PSEdge.Cross(t, positionNormalized)) * Mathf.Rad2Deg; }
void FixedUpdate() { float turn = Input.GetAxis(turnAxis); var h = rb.position.magnitude; var positionNormalized = rb.position.normalized; float thursterForceMagnitude = 0f; var afterBurnerOn = afterBurner.isEmitting; var forwardSpeed = Vector2.Dot(rb.velocity, transform.up); if (forwardSpeed < (afterBurnerOn ? afterBurnerMaxSpeed : maxSpeed)) { float thrust = Mathf.Max(Input.GetAxis(thrustAxis), 0f); float athmosphereCoefficient = Mathf.Clamp((120f - h) / 20f, 0f, 1f); thursterForceMagnitude = athmosphereCoefficient * (afterBurnerOn ? afterBurnerThrustPower : thrust * maxThrustPower); } Vector2 gravity = positionNormalized * gravityForceMagnitude; Vector2 thrusters = transform.up * thursterForceMagnitude; var forwardVelocity = forwardSpeed * (Vector2)transform.up; var orthogonalVelocity = rb.velocity - forwardVelocity; rb.AddForce(-orthogonalVelocity * orthogonalVelocity.magnitude * (airDrag + orthogonalDrag)); rb.AddForce(-forwardVelocity * forwardVelocity.magnitude * airDrag); rb.AddForce(thrusters + gravity); rb.angularVelocity = -turn * 300f; var positionDelta = rb.position - previousPosition; previousPosition = rb.position; var t = positionDelta - (positionNormalized * Vector2.Dot(positionDelta, positionNormalized)); rb.rotation -= Mathf.Atan(t.magnitude / h) * Mathf.Sign(PSEdge.Cross(t, positionNormalized)) * Mathf.Rad2Deg; }