// Update is called once every x seconds (multiple times per frame) void FixedUpdate() { if (air == AirState.Rising) { if (jump == JumpRequest.Stop) { // TODO: Only start falling when moving in direction of gravity? //air = AirState.Neutral; air = AirState.Falling; } } else { if (jump == JumpRequest.Jump) { StartJump(); air = AirState.Rising; } } if (moveDirection == 0.0f) { } else { float velocity = body.velocity.x; DEBUG_speedDisplay = velocity; float negativeIfChangingDirections = Math.Sign(velocity) * Math.Sign(moveDirection); StartMove(moveDirection, Math.Abs(velocity) / maxSpeed * negativeIfChangingDirections); } }
void CheckForGroundTouch(Collision2D potentialFloor) { // Checking contact points ensures only jumping off supporting // (gravity-interfering) surfaces. Vector2 avg_normal = new Vector2(0, 0); /*~ * // TODO: make contacts a member variable to prevent alloc * ContactPoint2D[] contacts = new ContactPoint2D[8]; * int count = potentialFloor.GetContacts(contacts); */ ContactPoint2D[] contacts = potentialFloor.contacts; int count = contacts.Length; for (int i = 0; i < count; ++i) { var c = contacts[i]; avg_normal += c.normal; } avg_normal /= count; float dot = Vector2.Dot(Physics.gravity, avg_normal); bool isOnGround = dot < 0; if (isOnGround) { air = AirState.OnGround; } else if (air != AirState.Rising) { // Probably walked off a ledge. air = AirState.Falling; } }
public override void Repair() { base.Repair(); _state = AirState.Move; _dropPos = Random.Range(transform.position.x, 42.0f); Rigid.velocity = Vector2.zero; }
private static Expression AirCondition(AirState value) { switch (value) { case AirState.AirOnly: return(ThisExpr.Instance.MakeIndex("isAir")); case AirState.GroundOnly: return(new UnOpExpr(ThisExpr.Instance.MakeIndex("isAir"), UnOpExpr.Op.Not)); case AirState.Any: default: return(new ConstNumberExpr(1)); } }
public void CheckAirState() { if (Grounded) { air = AirState.Ground; } else if (Input.Jet) { air = AirState.Flying; } else { air = AirState.Jumping; } }
// UNITY private void Update() { if (Mathf.Abs(ENDDESTINATION) - Mathf.Abs(transform.position.x) <= 0.1f) { gameObject.SetActive(false); } else { Rigid.velocity = (_moveDir * Stat.Speed); if (Mathf.Abs(_dropPos) - Mathf.Abs(transform.position.x) <= 0.1f && _state == AirState.Move) { GetSupply(transform.position); _state = AirState.DropComplete; } } }
// Update is called once per frame void Update() { //movement related calculations Vector2 moveForce = Vector2.zero; if (input.GetKey(InputMap.LEFT)) { direction = Direction.LEFT; moveForce.x = moveAccel * Time.deltaTime * (int)direction; } else if (input.GetKey(InputMap.RIGHT)) { direction = Direction.RIGHT; moveForce.x = moveAccel * Time.deltaTime * (int)direction; } else if (!input.GetKey(InputMap.LEFT) && !input.GetKey(InputMap.RIGHT) && dashState != DashState.DASHING) { //instant falloff of x-axis movement speed rb.velocity = new Vector2(0.0f, rb.velocity.y); } if (airState == AirState.FLOOR && input.GetKey(InputMap.JUMP)) { Debug.Log(string.Format("{0} is jumping", name)); input.SetKey(InputMap.JUMP, false); airState = AirState.JUMPING; beginJumpY = rb.position.y; beginJumpTime = Time.time; } //fixed jump height if (airState == AirState.JUMPING) { timeElapsed = Time.time - beginJumpTime; recordedJumpHeight = rb.position.y - beginJumpY; if (timeElapsed < timeToJumpApex) { float jumpModifier = 1.5f; float jumpVelPercent = Mathf.InverseLerp(0.0f, jumpHeight * jumpModifier, recordedJumpHeight); float newJumpVelocity = Mathf.Lerp(0.0f, jumpSpeed, 1.0f - jumpVelPercent); rb.velocity = new Vector2(rb.velocity.x, newJumpVelocity); } else { airState = AirState.FALLING; } recordedJumpHeight = rb.position.y - beginJumpY; } if (airState == AirState.FALLING) { Vector2 footPos = new Vector2(rb.position.x, rb.position.y - sr.bounds.extents.y); bool isOnFloor = Physics2D.OverlapCircle(footPos, 0.1f, floorMask); if (isOnFloor) { airState = AirState.FLOOR; } } Vector2 dashForce = Vector2.zero; if (dashState == DashState.READY && input.GetKey(InputMap.DASH)) { Debug.Log(string.Format("{0} is dashing", name)); input.SetKey(InputMap.DASH, false); dashState = DashState.DASHING; dashForce = new Vector2(dashAccel, 0f); } rb.AddForce(moveForce, ForceMode2D.Impulse); if (dashState != DashState.DASHING) { //clamp move speed rb.velocity = (Mathf.Abs(rb.velocity.x) > maxMoveSpeed) ? new Vector2(maxMoveSpeed * (int)direction, rb.velocity.y) : rb.velocity; } if (dashState == DashState.DASHING) { dashTimer += Time.deltaTime; if (dashTimer >= dashActiveDuration) { dashTimer = 0f; dashState = DashState.COOLDOWN; } else { rb.AddForce(dashForce * (int)direction, ForceMode2D.Impulse); } } if (dashState == DashState.COOLDOWN) { dashTimer += Time.deltaTime; if (dashTimer <= dashCDDuration) { dashTimer = 0f; dashState = DashState.READY; } else { rb.AddForce(dashForce * (int)direction * -1, ForceMode2D.Impulse); } } if (Input.GetKey(InputMap.EMIT_DASH) || Input.GetKey(InputMap.EMIT_JUMP)) { cr.broadcastSignal(); } }
void Start() { airState = AirState.jump; animationState = AnimationState.fall; }