void IConvertGameObjectToEntity.Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { if (enabled) { var componentData = new CharacterControllerComponentData { Gravity = Gravity, MovementSpeed = MovementSpeed, MaxMovementSpeed = MaxMovementSpeed, RotationSpeed = RotationSpeed, JumpUpwardsSpeed = JumpUpwardsSpeed, MaxSlope = math.radians(MaxSlope), MaxIterations = MaxIterations, CharacterMass = CharacterMass, SkinWidth = SkinWidth, ContactTolerance = ContactTolerance, AffectsPhysicsBodies = AffectsPhysicsBodies, }; var internalData = new CharacterControllerInternalData { Entity = entity, Input = new CharacterControllerInput(), }; dstManager.AddComponentData(entity, componentData); dstManager.AddComponentData(entity, internalData); } }
void IConvertGameObjectToEntity.Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { if (enabled) { ref PhysicsWorld world = ref World.Active.GetExistingSystem <BuildPhysicsWorld> ().PhysicsWorld; var componentData = new CharacterControllerComponentData { Gravity = Gravity, MovementSpeed = MovementSpeed, RotationSpeed = RotationSpeed, JumpUpwardsSpeed = JumpUpwardsSpeed, MaxSlope = math.radians(MaxSlope), MaxIterations = MaxIterations, CharacterMass = CharacterMass, SkinWidth = SkinWidth, ContactTolerance = ContactTolerance, AffectsPhysicsBodies = AffectsPhysicsBodies, }; var internalData = new CharacterControllerInternalData { Entity = entity, Input = new CharacterControllerInput(), }; dstManager.AddComponentData(entity, componentData); dstManager.AddComponentData(entity, internalData); dstManager.AddComponentData(entity, new PlayerTag()); dstManager.AddComponentData(entity, new PlayerNotInGround()); }
void PlayerOnGround(CharacterControllerInternalData data) { if (!animator.GetBool("InGround")) { return; } animator.SetBool("IsLand", true); animator.SetBool("IsJump", false); var sqrLength = math.lengthsq(data.LinearVelocity); // Debug.Log(sqrLength); if (sqrLength > 4.5f) { animator.SetBool("IsRun", true); animator.SetBool("isWalk", false); } else if (sqrLength > 0 && sqrLength <= 4.5f) { animator.SetBool("isWalk", true); animator.SetBool("IsRun", false); } if (data.LinearVelocity.x < 0.01f && data.LinearVelocity.z < 0.01f) { animator.SetBool("IsRun", false); animator.SetBool("isWalk", false); } }
void IConvertGameObjectToEntity.Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { if (enabled) { ref PhysicsWorld world = ref World.Active.GetExistingSystem <BuildPhysicsWorld>().PhysicsWorld; var componentData = new CharacterControllerComponentData { Gravity = Gravity, MovementSpeed = MovementSpeed, RotationSpeed = RotationSpeed, JumpSpeed = JumpSpeed, MaxSlope = MaxSlope, MaxIterations = MaxIterations, CharacterMass = CharacterMass, ContactTolerance = ContactTolerance, AffectsPhysicsBodies = AffectsPhysicsBodies, }; var userInputData = new CharacterControllerUserInputData(); var internalData = new CharacterControllerInternalData { Entity = entity }; dstManager.AddComponentData(entity, componentData); dstManager.AddComponentData(entity, userInputData); dstManager.AddComponentData(entity, internalData); }
private void HandleUserInput(CharacterControllerComponentData ccComponentData, float3 up, float3 surfaceVelocity, ref CharacterControllerInternalData ccInternalData, ref float3 linearVelocity) { // Reset jumping state and unsupported velocity if (ccInternalData.SupportedState == CharacterSupportState.Supported) { ccInternalData.IsJumping = false; ccInternalData.UnsupportedVelocity = float3.zero; } // Movement and jumping bool shouldJump = false; float3 requestedMovementDirection = float3.zero; { float3 forward = math.forward(quaternion.identity); float3 right = math.cross(up, forward); float horizontal = ccInternalData.Input.Movement.x; float vertical = ccInternalData.Input.Movement.y; bool jumpRequested = ccInternalData.Input.Jumped > 0; bool haveInput = (math.abs(horizontal) > float.Epsilon) || (math.abs(vertical) > float.Epsilon); if (haveInput) { float3 localSpaceMovement = forward * vertical + right * horizontal; float3 worldSpaceMovement = math.rotate(quaternion.AxisAngle(up, ccInternalData.CurrentRotationAngle), localSpaceMovement); requestedMovementDirection = math.normalize(worldSpaceMovement); } shouldJump = jumpRequested && ccInternalData.SupportedState == CharacterSupportState.Supported; } // Turning { float horizontal = ccInternalData.Input.Looking.x; bool haveInput = (math.abs(horizontal) > float.Epsilon); if (haveInput) { ccInternalData.CurrentRotationAngle += horizontal * ccComponentData.RotationSpeed * DeltaTime; } } // Apply input velocities { if (shouldJump) { // Add jump speed to surface velocity and make character unsupported ccInternalData.IsJumping = true; ccInternalData.SupportedState = CharacterSupportState.Unsupported; ccInternalData.UnsupportedVelocity = surfaceVelocity + ccComponentData.JumpUpwardsSpeed * up; } else if (ccInternalData.SupportedState != CharacterSupportState.Supported) { // Apply gravity ccInternalData.UnsupportedVelocity += ccComponentData.Gravity * DeltaTime; } // If unsupported then keep jump and surface momentum linearVelocity = requestedMovementDirection * ccComponentData.MovementSpeed + (ccInternalData.SupportedState != CharacterSupportState.Supported ? ccInternalData.UnsupportedVelocity : float3.zero); } }
void PlayerNotOnGround(CharacterControllerInternalData data) { if (animator.GetBool("InGround")) { return; } if (data.LinearVelocity.y < 0.5) { animator.SetBool("IsFloat", true); } else { animator.SetBool("IsJump", true); animator.SetBool("IsLand", false); } }
void IConvertGameObjectToEntity.Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem) { if (enabled) { var componentData = new CharacterControllerComponentData { Gravity = Gravity, MovementSpeed = MovementSpeed, MaxMovementSpeed = MaxMovementSpeed, RotationSpeed = RotationSpeed, JumpUpwardsSpeed = JumpUpwardsSpeed, MaxSlope = math.radians(MaxSlope), MaxIterations = MaxIterations, CharacterMass = CharacterMass, SkinWidth = SkinWidth, ContactTolerance = ContactTolerance, AffectsPhysicsBodies = (byte)(AffectsPhysicsBodies ? 1 : 0), RaiseCollisionEvents = (byte)(RaiseCollisionEvents ? 1 : 0), RaiseTriggerEvents = (byte)(RaiseTriggerEvents ? 1 : 0) }; var internalData = new CharacterControllerInternalData { Entity = entity, Input = new CharacterControllerInput(), }; dstManager.AddComponentData(entity, componentData); dstManager.AddComponentData(entity, internalData); if (RaiseCollisionEvents) { dstManager.AddBuffer <StatefulCollisionEvent>(entity); } if (RaiseTriggerEvents) { dstManager.AddBuffer <StatefulTriggerEvent>(entity); dstManager.AddComponentData(entity, new StatefulTriggerEventExclude { }); } } }
private void HandleUserInput(CharacterControllerComponentData ccComponentData, float3 up, float3 surfaceVelocity, float3 position, quaternion rotation, ref CharacterControllerInternalData ccInternalData, ref float3 linearVelocity) { // Reset jumping state and unsupported velocity if (ccInternalData.SupportedState == CharacterSupportState.Supported) { ccInternalData.IsJumping = false; ccInternalData.UnsupportedVelocity = float3.zero; } // Movement and jumping bool shouldJump = false; float3 requestedMovementDirection = float3.zero; { float3 forward = math.forward(quaternion.identity); float3 right = math.cross(up, forward); float horizontal = ccInternalData.Input.Movement.x; float vertical = ccInternalData.Input.Movement.y; bool jumpRequested = ccInternalData.Input.Jumped > 0; bool haveInput = (math.abs(horizontal) > float.Epsilon) || (math.abs(vertical) > float.Epsilon); if (haveInput) { float3 localSpaceMovement = forward * vertical + right * horizontal; float3 worldSpaceMovement = math.rotate(quaternion.AxisAngle(up, ccInternalData.CurrentRotationAngle), localSpaceMovement); requestedMovementDirection = math.normalize(worldSpaceMovement); } shouldJump = jumpRequested && ccInternalData.SupportedState == CharacterSupportState.Supported; } // Turning { var raycastInput = new RaycastInput { Start = cameraWorldPoint, End = cameraWorldPoint + new float3(0, -20f, 0f), Filter = new CollisionFilter() { BelongsTo = ~0u, CollidesWith = 1u << 5, GroupIndex = 0 } }; if (!PhysicsWorld.CastRay(raycastInput, out var hit)) { return; } var playerToMouse = hit.Position - position; playerToMouse.y = 0f; playerToMouse = math.normalize(playerToMouse); var forward = math.mul(rotation, new float3(0, 0, 1)); var angle = MathHelper.SignedAngle(forward, playerToMouse, new float3(0, 1, 0)); var horizontal = math.clamp(math.remap(-180f, 180f, -ccComponentData.RotationSpeed, ccComponentData.RotationSpeed, angle), -1f, 1f); var haveInput = (math.abs(horizontal) > 0.01f); if (haveInput) { ccInternalData.CurrentRotationAngle += horizontal * ccComponentData.RotationSpeed * DeltaTime; } } // Apply input velocities { if (shouldJump) { // Add jump speed to surface velocity and make character unsupported ccInternalData.IsJumping = true; ccInternalData.SupportedState = CharacterSupportState.Unsupported; ccInternalData.UnsupportedVelocity = surfaceVelocity + ccComponentData.JumpUpwardsSpeed * up; } else if (ccInternalData.SupportedState != CharacterSupportState.Supported) { // Apply gravity ccInternalData.UnsupportedVelocity += ccComponentData.Gravity * DeltaTime; } // If unsupported then keep jump and surface momentum linearVelocity = requestedMovementDirection * ccComponentData.MovementSpeed + (ccInternalData.SupportedState != CharacterSupportState.Supported ? ccInternalData.UnsupportedVelocity : float3.zero); } }
public float3 HitPoint; //射线击中点 public bool Equals(CharacterControllerInternalData other) { throw new NotImplementedException(); }