public override void UpdateVelocity(ref Vector3 currentVelocity, float deltaTime) { if (KinematicCharacterMotor.IsStableOnGround) { if (deltaTime > 0) { // The final velocity is the velocity from root motion reoriented on the ground plane currentVelocity = AssignedCharacterController.RootMotionPositionDelta / deltaTime; currentVelocity = KinematicCharacterMotor.GetDirectionTangentToSurface(currentVelocity, KinematicCharacterMotor.GroundNormal) * currentVelocity.magnitude; } else { // Prevent division by zero currentVelocity = Vector3.zero; } } else { if (AssignedCharacterController.ForwardAxis > 0f) { // If we want to move, add an acceleration to the velocity Vector3 targetMovementVelocity = KinematicCharacterMotor.CharacterForward * AssignedCharacterController.ForwardAxis * MaxAirMoveSpeed; Vector3 velocityDiff = Vector3.ProjectOnPlane(targetMovementVelocity - currentVelocity, Gravity); currentVelocity += velocityDiff * AirAccelerationSpeed * deltaTime; } // Gravity currentVelocity += Gravity * deltaTime; // Drag currentVelocity *= (1f / (1f + (Drag * deltaTime))); } }
public static void MySimulate(float deltaTime, KinematicCharacterMotor motor) { // Make sure all actors have reached their interpolation destination if (InterpolationMethod == CharacterSystemInterpolationMethod.Custom) { motor.Transform.SetPositionAndRotation(motor.TransientPosition, motor.TransientRotation); // motor.Rigidbody.position = motor.TransientPosition; // motor.Rigidbody.rotation = motor.TransientRotation; } // Save pre-simulation poses for (int i = 0; i < CharacterMotors.Count; i++) { motor.InitialTickPosition = motor.Transform.position; motor.InitialTickRotation = motor.Transform.rotation; } Simulate(deltaTime, motor); // post // Return characters to their initial poses and move to target // motor.Rigidbody.position = motor.InitialTickPosition; // motor.Rigidbody.rotation = motor.InitialTickRotation; // motor.Rigidbody.position = motor.TransientPosition; // motor.Rigidbody.rotation = motor.TransientRotation; // motor.Rigidbody.MovePosition(motor.TransientPosition); // motor.Rigidbody.MoveRotation(motor.TransientRotation); }
// Token: 0x060026F9 RID: 9977 RVA: 0x000B4708 File Offset: 0x000B2908 public static void RegisterCharacterMotor(KinematicCharacterMotor motor) { KinematicCharacterSystem.CharacterMotors.Add(motor); RigidbodyInterpolation interpolation = (KinematicCharacterSystem._internalInterpolationMethod == CharacterSystemInterpolationMethod.Unity) ? RigidbodyInterpolation.Interpolate : RigidbodyInterpolation.None; motor.Rigidbody.interpolation = interpolation; }
public void OnValidate() { if (Motor == null) { Motor = GetComponent <KinematicCharacterMotor>(); } }
/// <summary> /// (Called by KinematicCharacterMotor during its update cycle) /// This is called before the character begins its movement update /// </summary> public override void BeforeCharacterUpdate(float deltaTime) { // Handle detecting water surfaces { // Do a character overlap test to detect water surfaces if (KinematicCharacterMotor.CharacterOverlap(KinematicCharacterMotor.TransientPosition, KinematicCharacterMotor.TransientRotation, _waterOverlap, WaterLayer, QueryTriggerInteraction.Collide) > 0) { // If a water surface was detected if (_waterOverlap[0] != null) { // If the swimming reference point is inside the box, make sure we are in swimming state if (Physics.ClosestPoint(SwimmingReferencePoint.position, _waterOverlap[0], _waterOverlap[0].transform.position, _waterOverlap[0].transform.rotation) == SwimmingReferencePoint.position) { if (CurrentMovementState == DefaultMovementState) { TransitionToState(SwimmingState); SwimmingState.WaterZone = _waterOverlap[0]; } } // otherwise; default state else { if (CurrentMovementState == SwimmingState) { TransitionToState(DefaultMovementState); } } } } } CurrentMovementState.BeforeCharacterUpdate(deltaTime); }
/// <summary> /// Remembers the point to interpolate from for KinematicCharacterMotors and PhysicsMovers /// </summary> public void PreSimulationInterpolationUpdate(float deltaTime) { // Save pre-simulation poses and place transform at transient pose for (int i = 0; i < CharacterMotors.Count; i++) { KinematicCharacterMotor motor = CharacterMotors[i]; motor.InitialTickPosition = motor.TransientPosition; motor.InitialTickRotation = motor.TransientRotation; motor.Transform.SetPositionAndRotation(motor.TransientPosition, motor.TransientRotation); } // for (int i = 0; i < PhysicsMovers.Count; i++) // { // PhysicsMover mover = PhysicsMovers[i]; // // mover.InitialTickPosition = mover.TransientPosition; // mover.InitialTickRotation = mover.TransientRotation; // // mover.Transform.SetPositionAndRotation(mover.TransientPosition, mover.TransientRotation); // mover.Rigidbody.position = mover.TransientPosition; // mover.Rigidbody.rotation = mover.TransientRotation; // } }
// This is called by MyPlayer upon interaction input public void Interact() { // Overlap test if (KinematicCharacterMotor.CharacterOverlap(KinematicCharacterMotor.TransientPosition, KinematicCharacterMotor.TransientRotation, _interactionOverlap, InteractionLayer, QueryTriggerInteraction.Collide) > 0) { if (_interactionOverlap[0] != null) { // Handle ladders MyLadder ladder = _interactionOverlap[0].gameObject.GetComponent <MyLadder>(); if (ladder) { // Transition to ladder climbing state if (CurrentMovementState == DefaultMovementState) { LadderClimbingState.ActiveLadder = ladder; TransitionToState(LadderClimbingState); } // Transition back to default movement state else if (CurrentMovementState == LadderClimbingState) { TransitionToState(DefaultMovementState); } } } } }
/// <summary> /// Initiates the interpolation for KinematicCharacterMotors and PhysicsMovers /// </summary> public void PostSimulationInterpolationUpdate(float deltaTime) { _lastCustomInterpolationStartTime = Time.time; _lastCustomInterpolationDeltaTime = deltaTime; // Return interpolated roots to their initial poses for (int i = 0; i < CharacterMotors.Count; i++) { KinematicCharacterMotor motor = CharacterMotors[i]; motor.Transform.SetPositionAndRotation(motor.InitialTickPosition, motor.InitialTickRotation); } // for (int i = 0; i < PhysicsMovers.Count; i++) // { // PhysicsMover mover = PhysicsMovers[i]; // // if (mover.MoveWithPhysics) // { // mover.Rigidbody.position = mover.InitialTickPosition; // mover.Rigidbody.rotation = mover.InitialTickRotation; // // mover.Rigidbody.MovePosition(mover.TransientPosition); // mover.Rigidbody.MoveRotation(mover.TransientRotation); // } // else // { // mover.Rigidbody.position = (mover.TransientPosition); // mover.Rigidbody.rotation = (mover.TransientRotation); // } //} }
/// <summary> /// Handles per-frame interpolation /// </summary> private void CustomInterpolationUpdate() { float interpolationFactor = Mathf.Clamp01((Time.time - _lastCustomInterpolationStartTime) / _lastCustomInterpolationDeltaTime); // Handle characters interpolation for (int i = 0; i < CharacterMotors.Count; i++) { KinematicCharacterMotor motor = CharacterMotors[i]; motor.Transform.SetPositionAndRotation( Vector3.Lerp(motor.InitialTickPosition, motor.TransientPosition, interpolationFactor), Quaternion.Slerp(motor.InitialTickRotation, motor.TransientRotation, interpolationFactor)); } // Handle PhysicsMovers interpolation // for (int i = 0; i < PhysicsMovers.Count; i++) // { // PhysicsMover mover = PhysicsMovers[i]; // // mover.Transform.SetPositionAndRotation( // Vector3.Lerp(mover.InitialTickPosition, mover.TransientPosition, interpolationFactor), // Quaternion.Slerp(mover.InitialTickRotation, mover.TransientRotation, interpolationFactor)); // // Vector3 newPos = mover.Transform.position; // Quaternion newRot = mover.Transform.rotation; // mover.PositionDeltaFromInterpolation = newPos - mover.LatestInterpolationPosition; // mover.RotationDeltaFromInterpolation = Quaternion.Inverse(mover.LatestInterpolationRotation) * newRot; // mover.LatestInterpolationPosition = newPos; // mover.LatestInterpolationRotation = newRot; // } }
public KinematicCharacterMotor SpawnMotor(KinematicCharacterMotor motorPrefab, Vector3 position, Quaternion rotation, Transform parent = null) { var motor = Instantiate(motorPrefab, position, rotation, parent); CharacterMotors.Add(motor); return(motor); }
public override void AfterCharacterUpdate(float deltaTime) { // Handle jump-related values { // Handle jumping pre-ground grace period if (_jumpRequested && _timeSinceJumpRequested > JumpPreGroundingGraceTime) { _jumpRequested = false; } if (AllowJumpingWhenSliding ? KinematicCharacterMotor.FoundAnyGround : KinematicCharacterMotor.IsStableOnGround) { // If we're on a ground surface, reset jumping values if (!_jumpedThisFrame) { _doubleJumpConsumed = false; _jumpConsumed = false; } _timeSinceLastAbleToJump = 0f; } else { // Keep track of time since we were last able to jump (for grace period) _timeSinceLastAbleToJump += deltaTime; } } // Handle landing and leaving ground if (KinematicCharacterMotor.IsStableOnGround && !KinematicCharacterMotor.WasStableOnGround) { OnLanded(); } else if (!KinematicCharacterMotor.IsStableOnGround && KinematicCharacterMotor.WasStableOnGround) { OnLeaveStableGround(); } // Handle uncrouching if (IsCrouching && !_shouldBeCrouching) { // Do an overlap test with the character's standing height to see if there are any obstructions KinematicCharacterMotor.SetCapsuleDimensionsAuto(0.5f, 2f); if (KinematicCharacterMotor.CharacterOverlap( KinematicCharacterMotor.TransientPosition, KinematicCharacterMotor.TransientRotation, _probedColliders, KinematicCharacterMotor.CollidableLayers, QueryTriggerInteraction.Ignore) > 0) { // If obstructions, just stick to crouching dimensions KinematicCharacterMotor.SetCapsuleDimensionsAuto(0.5f, 1f); } else { // If no obstructions, uncrouch MeshRoot.localScale = new Vector3(1f, 1f, 1f); IsCrouching = false; } } }
public void OnStart(KinematicCharacterMotor motor) { _motor = motor; GameSettings.AvatarSettings.Initialize(); _gravity = GameSettings.AvatarSettings.Gravity; _gravityFalling = GameSettings.AvatarSettings.GravityFalling; }
public override void UpdateVelocity(ref Vector3 currentVelocity, float deltaTime) { Vector3 targetMovementVelocity = Vector3.zero; if (KinematicCharacterMotor.IsStableOnGround) { // Reorient velocity on slope currentVelocity = KinematicCharacterMotor.GetDirectionTangentToSurface(currentVelocity, KinematicCharacterMotor.GroundNormal) * currentVelocity.magnitude; // Calculate target velocity Vector3 inputRight = Vector3.Cross(_moveInputVector, KinematicCharacterMotor.CharacterUp); Vector3 reorientedInput = Vector3.Cross(KinematicCharacterMotor.GroundNormal, inputRight).normalized *_moveInputVector.magnitude; targetMovementVelocity = reorientedInput * MaxStableMoveSpeed; // Independant movement Velocity currentVelocity = Vector3.Lerp(currentVelocity, targetMovementVelocity, 1 - Mathf.Exp(-StableMovementSharpness * deltaTime)); } else { // Add move input if (_moveInputVector.sqrMagnitude > 0f) { targetMovementVelocity = _moveInputVector * MaxAirMoveSpeed; Vector3 velocityDiff = Vector3.ProjectOnPlane(targetMovementVelocity - currentVelocity, Gravity); currentVelocity += velocityDiff * AirAccelerationSpeed * deltaTime; } // Gravity currentVelocity += Gravity * deltaTime; // Drag currentVelocity *= (1f / (1f + (Drag * deltaTime))); } // Jumping _timeSinceJumpRequested += deltaTime; if (_jumpRequested) { if (!_jumpConsumed && ((AllowJumpingWhenSliding ? KinematicCharacterMotor.FoundAnyGround : KinematicCharacterMotor.IsStableOnGround) || _timeSinceLastAbleToJump <= JumpPostGroundingGraceTime)) { currentVelocity += (_jumpDirection * JumpSpeed) - Vector3.Project(currentVelocity, KinematicCharacterMotor.CharacterUp); _jumpRequested = false; _jumpConsumed = true; } // Handle jumping grace period if (_timeSinceJumpRequested > JumpPreGroundingGraceTime) { _jumpRequested = false; } } // Take into account additive velocity if (_internalVelocityAdd.sqrMagnitude > 0f) { currentVelocity += _internalVelocityAdd; _internalVelocityAdd = Vector3.zero; } }
public void OnStart(KinematicCharacterMotor motor) { _motor = motor; _state = States.Grounded; _maxJumpVelocity = GameSettings.AvatarSettings.MaxJumpVelocity; _minJumpVelocity = GameSettings.AvatarSettings.MinJumpVelocity; airJumpLimit.Reset(); }
public static KinematicCharacterMotor InitKinematicCharacterMotor(GameObject go) { KinematicCharacterMotor kcc = go.GetComponent <KinematicCharacterMotor>(); Object.DestroyImmediate(kcc); kcc = go.AddComponent <KinematicCharacterMotor>(); return(kcc); }
protected void HandleLoadedModel(PlayerEntity player, GameObject obj) { obj.layer = UnityLayerManager.GetLayerIndex(EUnityLayerName.Player); PlayerEntityUtility.DisableCollider(obj.transform); if (!player.hasCharacterContoller) { var character = DefaultGo.CreateGameObject(player.entityKey.ToString()); character.layer = UnityLayerManager.GetLayerIndex(EUnityLayerName.Player); CharacterController cc = PlayerEntityUtility.InitCharacterController(character); KinematicCharacterMotor kcc = PlayerEntityUtility.InitKinematicCharacterMotor(character); CharacterControllerContext characterControllerContext = new CharacterControllerContext( new UnityCharacterController(cc, !player.isFlagSelf), new Core.CharacterController.ConcreteController.ProneCharacterController(kcc, new ProneController()), new Core.CharacterController.ConcreteController.DiveCharacterController(kcc, new DiveController()), new Core.CharacterController.ConcreteController.SwimCharacterController(kcc, new SwimController()) ); var curver = character.AddComponent <AirMoveCurve>(); curver.AireMoveCurve = SingletonManager.Get <CharacterStateConfigManager>().AirMoveCurve; curver.MovementCurve = SingletonManager.Get <CharacterStateConfigManager>().MovementCurve; curver.PostureCurve = SingletonManager.Get <CharacterStateConfigManager>().PostureCurve; if (character.GetComponent <EntityReference>() == null) { character.AddComponentUncheckRequireAndDisallowMulti <EntityReference>(); } character.GetComponent <EntityReference>().Init(player.entityAdapter); var comp = character.AddComponent <PlayerVehicleCollision>(); comp.AllContext = _contexts; var appearanceManager = new AppearanceManager(); var characterControllerManager = new CharacterControllerManager(); characterControllerManager.SetCharacterController(characterControllerContext); var characterBone = new CharacterBoneManager(); characterBone.SetWardrobeController(appearanceManager.GetWardrobeController()); characterBone.SetWeaponController(appearanceManager.GetController <WeaponController>()); var weaponController = appearanceManager.GetController <WeaponController>() as WeaponController; if (null != weaponController) { weaponController.SetWeaponChangedCallBack(characterBone.CurrentWeaponChanged); weaponController.SetCacheChangeAction(characterBone.CacheChangeCacheAction); } player.AddCharacterControllerInterface(characterControllerManager); player.AddAppearanceInterface(appearanceManager); player.AddCharacterContoller(characterControllerContext); player.AddCharacterBoneInterface(characterBone); player.AddRecycleableAsset(character); player.AddPlayerGameState(PlayerLifeStateEnum.NullState); } }
public static void Simulate(float deltaTime, KinematicCharacterMotor motor) { motor.UpdatePhase1(deltaTime); motor.UpdatePhase2(deltaTime); motor.Transform.SetPositionAndRotation(motor.TransientPosition, motor.TransientRotation); motor.Rigidbody.position = motor.TransientPosition; motor.Rigidbody.rotation = motor.TransientRotation; }
public void UnRegisterMotor(KinematicCharacterMotor motor) { if (!CharacterMotors.Contains(motor)) { return; } CharacterMotors.Remove(motor); OnMotorUnRegistered?.Invoke(motor); }
public void RegisterMotor(KinematicCharacterMotor motor) { if (CharacterMotors.Contains(motor)) { return; } CharacterMotors.Add(motor); OnMotorRegistered?.Invoke(motor); }
//============================================================================================ /** * @brief Sets up the wrapper, getting a reference to an attached character controller * *********************************************************************************************/ private void Awake() { _kinematicCharacterMotor = GetComponent <KinematicCharacterMotor>(); Assert.IsNotNull(_kinematicCharacterMotor, "Error (UnityControllerWrapper): Could not find" + "CharacterController component"); //m_characterController.enableOverlapRecovery = true; }
public override void Enter() { base.Enter(); playerController.onStartUpdateVelocity += UpdateVelocity; playerController.onStartUpdateRotation += UpdateRotation; characterMotor = playerController.CharacterMotor; gravity = -(2 * MaxJumpHeight.Value) / Mathf.Pow(TimeToJumpApex.Value, 2); SetMoveDirection(); //Call to force update moveDir in case updateRot called b4 updateVel }
public static KeyValuePair <float, float> MyCalcRotateBound(KinematicCharacterMotor motor, int frameInterval) { motor.Transform.SetPositionAndRotation(motor.TransientPosition, motor.TransientRotation); // motor.Rigidbody.position = motor.TransientPosition; // motor.Rigidbody.rotation = motor.TransientRotation; motor.UpdatePhase1(frameInterval * 0.001f); return(motor.CalcRotateBound(frameInterval * 0.001f)); }
public override void OnStateEnter(MyMovementState previousState) { KinematicCharacterMotor.HandlePhysics(false, true); _climbingState = ClimbingState.Anchoring; // Store the target position and rotation to snap to _targetPosition = ActiveLadder.ClosestPointOnLadderSegment(KinematicCharacterMotor.TransientPosition, out _onLadderSegmentState); _targetRotation = ActiveLadder.transform.rotation; }
private void Awake() { // Handle initial state TransitionToState(CharacterState.Default); motor = motor ?? GetComponent <KinematicCharacterMotor>() ?? GetComponentInChildren <KinematicCharacterMotor>(); // Assign the characterController to the motor motor.CharacterController = this; }
private void Awake() { //Initial state. TransitionToState(CharacterState.Default); motor = motor ? motor : GetComponentInChildren <KinematicCharacterMotor>(); motor.CharacterController = this; _gravity = configs.gravity; }
public static void Simulate(float deltaTime, KinematicCharacterMotor motor) { // var before = motor.Transform.position; motor.UpdatePhase1(deltaTime); motor.UpdatePhase2(deltaTime); motor.Transform.SetPositionAndRotation(motor.TransientPosition, motor.TransientRotation); // motor.Rigidbody.position = motor.TransientPosition; // motor.Rigidbody.rotation = motor.TransientRotation; //Logger.InfoFormat("Simulate before:{0}, after:{1}, delta:{2}\n---------------------------------------------", before.ToStringExt(), motor.Transform.position.ToStringExt(),(motor.Transform.position - before).ToStringExt()); }
private static void DrawBoundingBox(KinematicCharacterMotor motor) { var characterTransformToCapsuleBottom = motor.CharacterTransformToCapsuleBottom; var characterTransformToCapsuleTop = motor.CharacterTransformToCapsuleTop; // DebugDraw.EditorDrawCapsule(motor.TransientPosition + motor.TransientRotation * characterTransformToCapsuleBottom, // motor.TransientPosition + motor.TransientRotation * characterTransformToCapsuleTop, motor.CapsuleRadius, // Color.magenta); DebugDraw.DebugCapsule(motor.TransientPosition + motor.TransientRotation * characterTransformToCapsuleBottom, motor.TransientPosition + motor.TransientRotation * characterTransformToCapsuleTop, Color.magenta, motor.CapsuleRadius); }
void Start() { if (!TryGetComponent(out Motor)) { Motor = gameObject.AddComponent <KinematicCharacterMotor>(); } Motor.CharacterController = this; CurrentState = states["Idle"]; CurrentState.OnStateEnter(); }
/// <summary> /// This is called by MyPlayer upon crouch input press /// </summary> public void OnCrouch(bool crouch) { _shouldBeCrouching = crouch; if (crouch) { IsCrouching = true; KinematicCharacterMotor.SetCapsuleDimensionsAuto(0.5f, 1f); MeshRoot.localScale = new Vector3(1f, 0.5f, 1f); } }
public void Crouch(bool crouch) { if (crouch) { KinematicCharacterMotor.SetCapsuleDimensionsAuto(0.5f, 1f); MeshRoot.localScale = new Vector3(1f, 0.5f, 1f); } else { _isTryingToUncrouch = true; } }