private bool GroundHit(Vector2 origin) { playerState.Set(StateLabels.IsOnSlope, false); hit = Physics2D.Raycast(origin, Vector2.down, coll.bounds.extents.y + 0.2f, 1 << LayerMask.NameToLayer("Terrain")); if (hit.collider != null) { playerState.Set(StateLabels.SlopeGradient, hit.normal.x); if (Mathf.Abs(hit.normal.y) < 0.5f) { playerState.Set(StateLabels.IsOnSlope, true); return(false); } TestObjects.BouncyObject bouncyObject = hit.collider.GetComponent <TestObjects.BouncyObject>(); if (bouncyObject != null) { StartCoroutine(SetBounciness(bouncyObject.Bounciness)); } else { MovingPlatform movingPlatform = hit.collider.GetComponent <MovingPlatform>(); if (movingPlatform != null) { playerState.Set(StateLabels.IsOnMovingPlatform, true); playerState.Set(StateLabels.PlatformVelocity, movingPlatform.GetVelocity()); } } } return(playerState.Check(StateLabels.IsOnMovingPlatform) || hit.collider != null && hit.point.y < origin.y - coll.bounds.extents.y * 0.9f); }
//should be called by control scripts FixedUpdate //ignoreGroundHandling will make Move not do the special handling it normally does to make entities "stick" to slope public void Move(ref bool hitTileX, ref bool hitTileY, bool wasOnGround = false) { if (!ignoreGravity) { setVelocityY(velocity.y + GravityAccel); } Vector2 moveVel = velocity; if (wasOnGround) { // moveVel.y -= 0.3f; } if (detectMovingPlatScript != null) { GameObject movePlat = detectMovingPlatScript.getTouchedPlat(); if (movePlat != null) { MovingPlatform movePlatScript = movePlat.GetComponent <MovingPlatform>(); if (movePlatScript != null) { m_Transform.position += movePlatScript.GetVelocity(); } } } float oldZ = m_Transform.position.z; m_Transform.position = MyGlobal.GetValidPosition(m_Transform.position, m_BoxCollider, moveVel, ref hitTileX, ref hitTileY, wasOnGround); m_Transform.position = new Vector3(m_Transform.position.x, m_Transform.position.y, oldZ); bool onGround = OnGround(); if (!wasOnGround && onGround && velocity.y < -0.05f) { // MyGlobal.PlayGlobalSound(thudNoise);// re enable once I get volume control } if (onGround) { setVelocityY(0); } }
public override void _PhysicsProcess(float delta) { if (Velocity.y >= 0) { Velocity.y += Gravity; } else { /* In platformers, you generally don't feel as strong of a gravitational pull * when you're falling vs. when you're falling */ Velocity.y += Gravity * 0.55f; } Vector2 platformVelocity = Vector2.Zero; if (IsGrounded) { Godot.Object floor = GroundRayCast.GetCollider(); if (floor is MovingPlatform) { MovingPlatform movingPlatform = (MovingPlatform)floor; platformVelocity = movingPlatform.GetVelocity(delta); } } Velocity.x += platformVelocity.x; if (Input.IsActionPressed("right")) { if (PMovementState == Still) { if (Input.IsActionPressed("sprint")) { PMovementState = SprintingRight; Velocity.x += SprintSpeed; } else { PMovementState = WalkingRight; Velocity.x += WalkSpeed; } } } if (Input.IsActionPressed("left")) { if (PMovementState == Still) { if (Input.IsActionPressed("sprint")) { PMovementState = SprintingLeft; Velocity.x -= SprintSpeed; } else { PMovementState = WalkingLeft; Velocity.x -= WalkSpeed; } } } if (PMovementState == WalkingRight) { if (Input.IsActionPressed("sprint")) { PMovementState = SprintingRight; Velocity.x += (SprintSpeed - WalkSpeed); } } if (PMovementState == WalkingLeft) { if (Input.IsActionPressed("sprint")) { PMovementState = SprintingLeft; Velocity.x -= (SprintSpeed - WalkSpeed); } } if (Input.IsActionJustReleased("right")) { if (PMovementState == WalkingRight) { PMovementState = Still; if (Velocity.x != 0) { Velocity.x -= WalkSpeed; } } if (PMovementState == SprintingRight) { PMovementState = Still; if (Velocity.x != 0) { Velocity.x -= SprintSpeed; } } } if (Input.IsActionJustReleased("left")) { if (PMovementState == WalkingLeft) { PMovementState = Still; if (Velocity.x != 0) { Velocity.x += WalkSpeed; } } if (PMovementState == SprintingLeft) { PMovementState = Still; if (Velocity.x != 0) { Velocity.x += SprintSpeed; } } } if (Input.IsActionJustReleased("sprint")) { if (PMovementState == SprintingRight) { PMovementState = WalkingRight; if (Velocity.x != 0) { Velocity.x -= (SprintSpeed - WalkSpeed); } } if (PMovementState == SprintingLeft) { PMovementState = WalkingLeft; if (Velocity.x != 0) { Velocity.x += (SprintSpeed - WalkSpeed); } } } if (JumpSpeed != 0) { Velocity.y -= JumpSpeed; JumpSpeed = 0; } if (Input.IsActionJustReleased("jump")) { if (!IsGrounded) { if (Velocity.y < 0) { Velocity.y = (Velocity.y * 0.6f); } } } Velocity = MoveAndSlide(Velocity, infiniteInertia: false); Velocity.x -= platformVelocity.x; if ((JumpSpeed == 0) && (PMovementState == Still)) { Velocity.x = 0; } }