private void ProcessPhysicItem ( float dt, ref DefStDodgeOnGroundSettings settings, ref DefStDodgeInput input, ref DefStDodgeOnGroundProcessData process, ref StVelocity velocity, ref CharacterControllerState state ) { /*if (!state.IsGrounded() && math.any(process.Direction != float3.zero) && process.IsDodging == 1) * { * var previousVelocity = velocity.Value; * var previousFlatVel = previousVelocity.ToGrid(1); * var previousSpeed = Mathf.Max(previousFlatVel.magnitude, settings.MinSpeed) + (process.InertieDelta * dt); * * velocity.Value = math.normalize(process.Direction) * previousSpeed; * velocity.Value.y = previousVelocity.y; * }*/ // TODO: Remove hardcoded character slopelimit (45) /*if (MvUtils.OnGround(state, velocity) && Vector3.Angle(state.AngleDir, Vector3.up) < 45 && process.IsDodging == 1) * { * process.IsDodging = 0; * * var oldY = velocity.Value.y; * var addSpeed = Mathf.Max(velocity.Value.ToGrid(1).magnitude - process.StartAfterDodgeFlatSpeed, 0); * var speed = Mathf.Min(velocity.Value.ToGrid(1).magnitude, process.StartFlatSpeed + addSpeed); * * velocity.Value = velocity.Value.ToGrid(1).normalized * speed; * * Debug.Log("nice"); * * velocity.Value.y = oldY; * }*/ process.InertieDelta -= state.IsGrounded() ? dt * 50f : dt; process.CooldownBeforeNextDodge -= dt; input.TimeBeforeResetState -= dt; }
/// <summary> /// Move the character with the Srt (CPMA based) algorithm. /// </summary> /// <param name="initialVelocity">The initial velocity to use</param> /// <param name="direction">The movement direction</param> /// <param name="settings">The movement settings</param> /// <param name="dt">Delta time</param> /// <returns>Return the new position</returns> private static float3 SrtMove(float3 initialVelocity, float2 initialDirection, float3 direction, DefStGroundRunSettings settings, CharacterControllerState state, float dt) { // Fix NaN errors direction = SrtFixNaN(direction); // Set Y axe to zero initialVelocity.y = 0; var previousSpeed = math.length(initialVelocity); var friction = SrtGetFrictionPower ( previousSpeed, settings.FrictionSpeedMin, settings.FrictionSpeedMax, settings.FrictionMin, settings.FrictionMax ); var angleInvertedDir = 1 - state.AngleDir.y; if (math.abs(angleInvertedDir) > 0.4f) { friction *= math.clamp(math.abs(angleInvertedDir), 0.4f, 1f); } var velocity = SrtApplyFriction(initialVelocity, direction, friction, settings.SurfaceFriction, settings.Acceleration, settings.Deacceleration, dt); var wishSpeed = math.length(direction) * settings.BaseSpeed; if (float.IsNaN(wishSpeed)) { wishSpeed = 0; } var strafeAngleNormalized = SrtGetStrafeAngleNormalized(direction, math.normalize(initialVelocity)); if (wishSpeed > settings.BaseSpeed && wishSpeed < previousSpeed) { wishSpeed = math.lerp(previousSpeed, wishSpeed, math.max(math.distance(wishSpeed, previousSpeed), 0) * dt); } if (initialDirection.y > 0.5f) { if (previousSpeed >= settings.BaseSpeed - 0.25f) { wishSpeed = settings.SprintSpeed; settings.Acceleration = 6f; } } if (state.AngleDir.y >= 0) { wishSpeed *= math.clamp(state.AngleDir.y + 0.1f, 0.1f, 1f); } velocity = SrtAccelerate(velocity, direction, wishSpeed, settings.Acceleration, math.min(strafeAngleNormalized, 0.25f), dt); var nextSpeed = math.length(math.float3(velocity.x, 0, velocity.z)); if (previousSpeed > nextSpeed && nextSpeed < settings.BaseSpeed && strafeAngleNormalized > 0.1f && strafeAngleNormalized < 0.9f) { velocity.y = 0; velocity = Vector3.Normalize(velocity) * math.lerp(nextSpeed, previousSpeed, math.max(1 - strafeAngleNormalized, 0.8f)); } return(velocity); }
private bool ProcessItem ( ref Entity entity, ref StVelocity velocity, ref DefStJumpSettings setting, ref DefStRunInput runInput, ref DefStJumpInput input, ref DefStWallJumpProcessData process, ref CharacterControllerState state, Transform transform ) { var action = input.State == InputState.Down && !MvUtils.OnGround(state, velocity) && Time.time > process.TimeBeforeNextWJ; if (!action) { return(false); } var originalVelocity = velocity.Value; var fwd = transform.forward; var pos = transform.position + new Vector3(0, 0.4f + 0.1f); // hardcoded value (stepoffset) var rot = transform.rotation; var rd = 0.3f + 0.075f; // (radius) var sw = 0.07f + 0.025f; // hardcoded value (skinwidth) var height = 2f - 0.4f; // harcoded value (height and stepOffset) var subheight = (height * 0.75f) - 0.005f; CPhysicSettings.Active.SetGlobalCollision(transform.gameObject, false); var direction = (Vector3)SrtComputeDirection(fwd, rot, runInput.Direction); var rayTrace = UtilityWallRayTrace.RayTrace(ref direction, ref pos, ref rd, ref sw, ref height, ref subheight, transform.GetComponent <Collider>()); Debug.DrawRay(rayTrace.point, rayTrace.normal, Color.red, 10); CPhysicSettings.Active.SetGlobalCollision(transform.gameObject, true); var success = rayTrace.normal != Vector3.zero && Mathf.Abs(rayTrace.normal.y) < 0.2f; if (success) { rayTrace.normal = rayTrace.normal.ToGrid(1).normalized; velocity.Value = RaycastUtilities.SlideVelocityNoYChange(velocity.Value, rayTrace.normal); velocity.Value.y = math.max(math.min(velocity.Value.y + 6.5f, 12f), 0); var previousVelocity = velocity.Value; var bounceDir = rayTrace.normal * 6f; var minSpeed = bounceDir.magnitude; velocity.Value += bounceDir; var flatVelocity = velocity.Value.ToGrid(1); var oldY = velocity.Value.y; velocity.Value = Vector3.ClampMagnitude(flatVelocity, Mathf.Max(previousVelocity.magnitude, minSpeed)); velocity.Value.y = oldY; process.TimeBeforeNextWJ = Time.time + DefaultCooldown; input.TimeBeforeResetState = -1f; input.State = InputState.None; BroadcastNewEntity(PostUpdateCommands, true); PostUpdateCommands.AddComponent(new DefStWallJumpEvent(Time.time, Time.frameCount, entity, originalVelocity, rayTrace.normal)); MvDelegateEvents.InvokeCharacterWalljump(entity); } return(success); }
private bool ProcessItem ( ref Entity entity, ref StVelocity velocity, ref DefStRunInput runInput, ref DefStDodgeOnGroundSettings setting, ref DefStDodgeInput input, ref DefStDodgeOnGroundProcessData process, ref CharacterControllerState state, Transform transform ) { var doDodge = input.State != InputState.None && process.CooldownBeforeNextDodge <= 0f && MvUtils.OnGround(state, velocity); if (input.TimeBeforeResetState <= 0f) { input.State = InputState.None; } // We expect the developpers to check for staminas or things like that for this command. EntityManager.SetComponentData(m_CmdDoDodge, new EntityCommandTarget(entity)); DiffuseCommand(m_CmdDoDodge, m_CmdDoDodgeResult, doDodge, CmdState.Begin); doDodge = GetCmdResult(m_CmdDoDodgeResult); if (!doDodge) { return(false); } process.StartFlatSpeed = velocity.Value.ToGrid(1).magnitude; var direction = SrtComputeDirection(transform.forward.normalized, transform.rotation, runInput.Direction); velocity.Value.y = 0f; var addVelocity = SrtDodge(velocity.Value, direction, setting.AdditiveSpeed, setting.MinSpeed, setting.MaxSpeed); var momentum = transform.GetComponent <CharacterControllerMotor>().Momentum; if (Vector3.Dot(velocity.Value.normalized, math.normalizesafe(addVelocity)) >= 0.9f) { addVelocity = (momentum.normalized + ((Vector3)addVelocity).normalized).normalized * math.length(addVelocity); Debug.Log(addVelocity); } velocity.Value = addVelocity; velocity.Value += Vector3.up * setting.VerticalPower; input.TimeBeforeResetState = -1f; input.State = InputState.None; process.CooldownBeforeNextDodge = 0.5f; process.InertieDelta = 0.1f; process.Direction = direction; process.IsDodging = 1; process.StartAfterDodgeFlatSpeed = velocity.Value.ToGrid(1).magnitude; // We except developpers to just clean the pre-command phase, and not applying things like reducing stamina... DiffuseCommand(m_CmdDoDodge, m_CmdDoDodgeResult, true, CmdState.End); // Send dodge message to clients BroadcastNewEntity(PostUpdateCommands, true); PostUpdateCommands.AddComponent(new DefStDodgeEvent(Time.time, Time.frameCount, entity)); MvDelegateEvents.InvokeCharacterDodge(entity); return(true); }
public static bool OnGround(CharacterControllerState state, Vector3 velocity) { return(velocity.y <= 0 && state.GroundFlags == 1); }
public static bool OnGround(CharacterControllerState state, StVelocity velocity) { return(velocity.Value.y <= 0 && state.GroundFlags == 1); }
private bool ProcessItem ( ref Entity entity, ref StVelocity velocity, ref DefStJumpSettings setting, ref DefStRunInput runInput, ref DefStJumpInput input, ref DefStJumpProcessData process, ref CharacterControllerState state, Transform transform ) { if (MvUtils.OnGround(state, velocity)) { process.ComboCtx = 0; } if (input.ContinueJump == 0) { process.NeedToChain = 0; } var inputCanJump = input.State == InputState.Down || (input.ContinueJump == 1 && process.NeedToChain == 1); var doJump = inputCanJump && (process.ComboCtx < setting.MaxCombo && process.CooldownBeforeNextJump <= 0f) && (MvUtils.OnGround(state, velocity) || process.ComboCtx > 0); var airJump = doJump && !state.IsGrounded() && !state.IsSliding(); if (input.TimeBeforeResetState <= 0f) { input.State = InputState.None; } process.CooldownBeforeNextJump -= Time.deltaTime; input.TimeBeforeResetState -= Time.deltaTime; // We expect the developpers to check for staminas or things like that for this command. DiffuseCommand(m_CmdDoJump, m_CmdDoJumpResult, doJump, CmdState.Begin); doJump = GetCmdResult(m_CmdDoJumpResult); if (!doJump) { return(false); } var direction = SrtComputeDirection(transform.forward, transform.rotation, runInput.Direction); var strafeAngle = SrtGetStrafeAngleNormalized(direction, velocity.Value); if (math.all(runInput.Direction == float2.zero)) { strafeAngle *= 0.5f; } if (input.State == InputState.Down) { process.NeedToChain = 1; } velocity.Value.y = math.max(velocity.Value.y, 0); if (!airJump) { var motor = transform.GetComponent <CharacterControllerMotor>(); velocity.Value += Vector3.up * (setting.JumpPower * (input.State != InputState.Down ? 0.75f : 1f)); //if (motor.Momentum.y > 0) velocity.Value += Vector3.up * (motor.Momentum.normalized.y * 6f); } else { velocity.Value = Vector3.up * setting.JumpPower; } if (airJump) { velocity.Value = math.lerp(velocity.Value, SrtAirDash(velocity.Value, direction), 1f); } else if (input.State != InputState.Down) { velocity.Value += (Vector3)(direction * (strafeAngle * 5f)); var oldY = velocity.Value.y; var currSpeed = velocity.Value.ToGrid(1).magnitude; var newSpeed = math.min(currSpeed + strafeAngle * 2f, math.max(currSpeed, 18f)); velocity.Value = velocity.Value.ToGrid(1).normalized *newSpeed; velocity.Value.y = oldY; } else { var oldY = velocity.Value.y; //velocity.Value = velocity.Value.ToGrid(1).normalized * (velocity.Value.ToGrid(1).magnitude - 2.5f); velocity.Value.y = oldY; } input.TimeBeforeResetState = -1f; input.State = InputState.Pressed; process.ComboCtx++; process.CooldownBeforeNextJump = 0.1f; // We except developpers to just clean the pre-command phase, and not applying things like reducing stamina... DiffuseCommand(m_CmdDoJump, m_CmdDoJumpResult, true, CmdState.End); // Send event to clients BroadcastNewEntity(PostUpdateCommands, true); PostUpdateCommands.AddComponent(new DefStJumpEvent(Time.time, Time.frameCount, entity)); MvDelegateEvents.InvokeCharacterJump(entity); return(true); }
/// <summary> /// Unity's method called as soon as this entity is created. /// It will be called even if the entity is disabled. /// </summary> public void Awake() { // Creates the original state State = new CharacterControllerState(); // Initializes the parameter's stack and lists _overrideParameters = new Stack<CharacterControllerParameters>(); _collisions = new List<Collider>(); _listeners = new List<CharacterControllerListener>(); // Recovers the desired components _transform = transform; _controller = GetComponent<CharacterController>(); _characterSize = GetComponent<CharacterSize>(); // Ignores the collisions with the hat layer Physics.IgnoreLayerCollision(LayerMask.NameToLayer("Character"), LayerMask.NameToLayer("Cloth"), true); }