private void GrabbedUpdate() { if (input.gripAutoHolds) { if (input.GetReleaseGripButtonPressed(input.activeController)) { this.ClearActiveController(); return; } } else if (!input.GetGripButtonDown(input.activeController)) { this.ClearActiveController(); return; } float percComplete = (Time.time - this.grabStartTime) / this.grabFlyTime; if (percComplete < 1 && this.shouldFly) { this.inHand = false; transform.position = Vector3.Lerp(this.grabStartPosition, this.input.PositionForController(this.input.activeController), percComplete); transform.rotation = Quaternion.Lerp(this.grabStartRotation, this.input.RotationForController(this.input.activeController), percComplete); } else if (isKicking) { this.kickOffset = Quaternion.Lerp(this.kickOffset, Quaternion.identity, 0.05f); this.transform.SetPositionAndRotation(this.input.PositionForController(this.input.activeController), this.input.RotationForController(this.input.activeController) * this.kickOffset); float curAngle = Quaternion.Angle(this.kickOffset, Quaternion.identity); if (curAngle < minKickAngle || Time.time - this.kickStartTime > maxKickDuration) { this.isKicking = false; } } else { this.inHand = true; this.transform.SetPositionAndRotation(this.input.PositionForController(this.input.activeController), this.input.RotationForController(this.input.activeController)); } }
private void GrabbedUpdate() { if (input.gripAutoHolds) { if (input.GetReleaseGripButtonPressed(input.activeController)) { this.ClearActiveController(); return; } } else if (!input.GetGripButtonDown(input.activeController)) { this.ClearActiveController(); return; } // Get target Rotation Quaternion targetRotation; Quaternion controllerRotation = this.input.RotationForController(this.input.activeController); if (this.forceRotationInHand) { targetRotation = controllerRotation * Quaternion.Euler(this.rotationInHand); } else { targetRotation = controllerRotation * this.grabData.grabStartLocalRotation; } // Make sure position offset respects rotation Vector3 targetOffset; if (this.input.activeController == SVControllerType.SVController_Left && mirrorXOffsetInLeftHand) { Matrix4x4 mirrorMatrix = Matrix4x4.Scale(new Vector3(-1, 1, 1)); Matrix4x4 offsetAndRotation = Matrix4x4.TRS(-positionOffsetInHand, Quaternion.Euler(this.rotationInHand), Vector3.one); Matrix4x4 finalOffsetAndRotation = mirrorMatrix * offsetAndRotation; targetRotation = controllerRotation * Quaternion.LookRotation(finalOffsetAndRotation.GetColumn(2), finalOffsetAndRotation.GetColumn(1)); targetOffset = targetRotation * finalOffsetAndRotation.GetColumn(3); targetRotation *= Quaternion.AngleAxis(180, Vector3.up); } else { targetOffset = targetRotation * -positionOffsetInHand; } float percComplete = (Time.time - this.grabData.grabStartTime) / this.grabFlyTime; if (percComplete < 1 && this.shouldFly && !this.grabData.hasJoint) { this.inHand = false; transform.position = Vector3.Lerp(this.grabData.grabStartPosition, this.input.PositionForController(this.input.activeController) + targetOffset, percComplete); transform.rotation = Quaternion.Lerp(this.grabData.grabStartWorldRotation, targetRotation, percComplete); } else { this.inHand = true; Vector3 targetPosition = this.input.PositionForController(this.input.activeController); // If we're moving too quickly and allow physics, drop the object. This also gives us the ability to drop it if you are trying to move it through // a solid object. if (!this.ignorePhysicsInHand && (transform.position - targetPosition).magnitude >= objectDropDistance) { grabData.recentlyDropped = true; this.ClearActiveController(); return; } // If we've got a joint let's forget about setting the rotation and just focus on the position. // This keeps us from losing our minds! if (this.grabData.hasJoint) { transform.position = targetPosition + targetOffset; } else // otherwise just lock to the hand position so there is no delay { if (this.ignorePhysicsInHand) { this.transform.SetPositionAndRotation(targetPosition + targetOffset, targetRotation); } else { transform.position = Vector3.Lerp(this.transform.position, targetPosition + targetOffset, inHandLerpSpeed); transform.rotation = Quaternion.Lerp(this.transform.rotation, targetRotation, inHandLerpSpeed); rb.velocity = this.input.ActiveControllerVelocity(); rb.angularVelocity = this.input.ActiveControllerAngularVelocity(); } } } }