public void Flip() { // Switch the way the player is labelled as facing. m_FacingRight = !m_FacingRight; if (isHolding) { holding.SendMessage("changeFrontTransform", m_FacingRight ? m_pickupR : m_pickupL); holding.SendMessage("flipSpriteX", !m_FacingRight); } if (charAnim != null) { charAnim.FlipX(!m_FacingRight); } if (spriteFlipMethod == flipType.scale) { // Multiply the player's x local scale by -1. Vector3 theScale = transform.localScale; theScale.x *= -1; transform.localScale = theScale; } if (spriteFlipMethod == flipType.spriteRenderer) { SpriteRenderer spr = gameObject.GetComponent <Renderer>() as SpriteRenderer; spr.flipX = !m_FacingRight; //Flip child objects that have sprites Transform[] ts = gameObject.GetComponentsInChildren <Transform>(); foreach (Transform t in ts) { SpriteRenderer renderer = t.gameObject.GetComponent(typeof(SpriteRenderer)) as SpriteRenderer; if (renderer != null) { renderer.flipX = !m_FacingRight; } } } if (canPickup && (m_rangeColliderL != null && m_rangeColliderR != null)) { m_rangeColliderR.enabled = m_FacingRight; m_rangeColliderL.enabled = !m_FacingRight; } }
public void Move(float move, bool crouch, bool jump, bool pickup = false, float climb = 0, bool dropDown = false, bool dialog = false) { if (isDead || controlTaken || pause) { return; } bool justPickedUp = false; //Handle drop down platforms if (dropDown && onDropPlatformScript != null && canUseDropDownPlatforms) { onDropPlatformScript.DropObject(gameObject); } // If crouching, check to see if the character can stand up if (!crouch && canCrouch) { // If the character has a ceiling preventing them from standing up, keep them crouching Collider2D col = Physics2D.OverlapCircle(m_CeilingCheck.position, k_CeilingRadius, m_WhatIsGround); if (col) { GameObject holdingGo = null; if (holdingSomething()) { holdingGo = holding.gameObject; } if (!col.isTrigger && col.gameObject != holdingGo) { crouch = true; } } } //Climbing Code m_Rigidbody2D.gravityScale = initialGravityScale; if (charAnim != null) { charAnim.climb = 0f; } if ((climb != 0 || isClimbing) && canClimb && (!isHolding || canCarryWhileClimbing)) //We meet some conditions for climbing, so let's check for something to climb { //check for climbing up and down the climb surface Collider2D collider = Physics2D.OverlapCircle(m_GroundCheck.position, k_GroundedRadius, m_WhatIsClimb); if (collider == null) { isClimbing = false; } else { isClimbing = true; m_Grounded = true; m_Rigidbody2D.gravityScale = 0; m_Rigidbody2D.velocity = new Vector3(0, 0, 0); numJumps = 0; if (charAnim != null) { charAnim.climb = climb; charAnim.jump = false; //Sometimes the jump animation sticks. This fixes that. } // Move the character by finding the target velocity //Vector3 climbVelocity = new Vector2(m_Rigidbody2D.velocity.x, m_ClimbSpeed); // And then smoothing it out and applying it to the character //m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, climbVelocity, ref m_Velocity, m_MovementSmoothing); if (climb != 0) { m_Rigidbody2D.MovePosition((Vector2)gameObject.transform.position + new Vector2(0, climb)); } } } if (charAnim != null) { charAnim.isClimbing = isClimbing; } //only control the player if grounded or airControl is turned on if (m_Grounded || m_AirControl) { // If crouching if (crouch && canCrouch && (!isClimbing || !canClimb)) { if (!m_wasCrouching) { m_wasCrouching = true; OnCrouchEvent.Invoke(true); } // Reduce the speed by the crouchSpeed multiplier move *= m_CrouchSpeed; if (charAnim != null) { charAnim.crouch = true; } // Disable one of the colliders when crouching if (m_CrouchDisableCollider != null) { m_CrouchDisableCollider.enabled = false; } } else { // Enable the collider when not crouching if (m_CrouchDisableCollider != null) { m_CrouchDisableCollider.enabled = true; } if (m_wasCrouching) { m_wasCrouching = false; OnCrouchEvent.Invoke(false); if (charAnim != null) { charAnim.crouch = false; } } } if (charAnim != null) { charAnim.pushing = false; } //Start pushing logic if (move == 0) { pushTimer = -1; isPushing = false; timerComplete = false; } if (m_Grounded && (move > 0 || move < 0) && canPush && (!isClimbing || !canClimb)) //If we're in a situation where we can potentially push { bool pushingLeft = false; bool pushingRight = false; Collider2D[] leftColAll = Physics2D.OverlapCircleAll(m_SideCheckL.position, k_SideRadius, m_WhatIsGround); Collider2D[] rightColAll = Physics2D.OverlapCircleAll(m_SideCheckR.position, k_SideRadius, m_WhatIsGround); Collider2D leftCol = null, rightCol = null; GameObject holdingGo = null; if (holdingSomething()) { holdingGo = holding.gameObject; } foreach (var l in leftColAll) { if (l != null && l.gameObject != holdingGo && !l.isTrigger && move < 0) { pushingLeft = true; leftCol = l; break; } } foreach (var r in rightColAll) { if (r != null && r.gameObject != holdingGo && !r.isTrigger && move > 0) { rightCol = r; pushingRight = true; break; } } if (pushingLeft || pushingRight) { if (pushTimer == -1 && !timerComplete) { pushTimer = m_PushWait; } pushTimer -= Time.fixedDeltaTime; if (pushTimer <= 0 && !timerComplete) { timerComplete = true; } if (timerComplete) { if (charAnim != null) { charAnim.pushing = true; } isPushing = true; if (pushingLeft) { if (leftCol.attachedRigidbody != null) { leftCol.attachedRigidbody.AddForceAtPosition(new Vector2(-m_PushForce, 0f), new Vector2(m_SideCheckL.position.x, m_SideCheckL.position.y)); } } if (pushingRight) { if (rightCol.attachedRigidbody != null) { rightCol.attachedRigidbody.AddForceAtPosition(new Vector2(m_PushForce, 0f), new Vector2(m_SideCheckR.position.x, m_SideCheckR.position.y)); } } } } else { pushTimer = -1; isPushing = false; timerComplete = false; } } // Move the character by finding the target velocity Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y); // And then smoothing it out and applying it to the character m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing); if (inWater) { m_Rigidbody2D.velocity *= new Vector3(waterMultiplier, 1, 1); } if (charAnim != null) { charAnim.speed = Mathf.Abs(move); } //We're about to flip the character. If we're aiming with the mouse then we don't want to flip the character while walking. Let's check for that first. var dontFlip = false; if (isHolding) { if (mouseAim && currentlyMouseAiming && holding.flipCharacterWithMouseAim) { dontFlip = true; } } // If the input is moving the player right and the player is facing left... if (move > 0 && !m_FacingRight && !dontFlip) { // ... flip the player. Flip(); } // Otherwise if the input is moving the player left and the player is facing right... else if (move < 0 && m_FacingRight && !dontFlip) { // ... flip the player. Flip(); } //Start pickup item code if (canPickup && !isHolding && m_Grounded && actionObjectInRange != null && pickup && (!isClimbing || !canClimb)) //We're in range of something, can pick it up, and trying to pick it up { holding = actionObjectInRange.GetComponent <pickupObject>() as pickupObject; if (holding != null) { currentlyMouseAiming = false; holding.pickMeUp(gameObject, m_pickupTop, m_FacingRight ? m_pickupR : m_pickupL); isHolding = true; setHoldingUIText(holding.name); setActionObjectInRange(null); justPickedUp = true; if (charAnim != null) { charAnim.pickingUp = true; } holding.SendMessage("flipSpriteX", !m_FacingRight); //Update the held object's facing direction } } //Start dialog initiation code if (dialog && !isHolding && m_Grounded && actionObjectInRange != null && (!isClimbing || !canClimb)) //We're in range of something that we can talk to, and are pressing the dialog button { if (sayDialogBox == null) { DialogRange d = actionObjectInRange.GetComponent <DialogRange>() as DialogRange; if (d != null) { m_Rigidbody2D.velocity = new Vector2(0f, 0f); d.Initiate(CharacterName, gameObject, m_DialogTop, m_DialogBottom); isTalking = true; pause = true; setActionObjectInRange(null); } } } //Code for other range triggers (RangeTriggerEvent) if (pickup && !isHolding && m_Grounded && actionObjectInRange != null && (!isClimbing || !canClimb)) //We're in range of something, so we'll check to see if it has a RangeTriggerEvent. Pickup is used to check if the action key (pickup key) was pressed { RangeTriggerEvent d = actionObjectInRange.GetComponent <RangeTriggerEvent>() as RangeTriggerEvent; if (d != null) { m_Rigidbody2D.velocity = new Vector2(0f, 0f); d.Activate(CharacterName, gameObject); setActionObjectInRange(null); } } } //END if Grounded || Air Control charAnim.carryTop = false; charAnim.carryFront = false; if (isHolding && charAnim != null) { if (holding.mCarryType == pickupObject.carryType.Top) { charAnim.carryTop = true; } if (holding.mCarryType == pickupObject.carryType.Front) { charAnim.carryFront = true; } } // If the player should jump... if (jump && (!justPickedUp || !pickupWithJump || !canPickup) && (!isClimbing || !climbWithJump || !canClimb)) { if (infiniteJump || (canDoubleJump && numJumps < 2) || m_Grounded || (inWater && infiniteWaterJump) || temporaryExtraJump) { // Add a vertical force to the player. m_Grounded = false; numJumps++; //With the temporary extra jump things tend to work more consistently when we cancel out any current y velocities and start at zero. This gives us better control over the jump. //Also fixes a glitch that makes conveyor belts "sticky" when trying to jump on them. if (temporaryExtraJump || isOnConveyor) { m_Rigidbody2D.velocity = new Vector3(m_Rigidbody2D.velocity.x, 0f, 0f); } temporaryExtraJump = false; if (charAnim != null) { charAnim.jump = true; if (numJumps > 1) { charAnim.doubleJump = true; } } //m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce)); m_Rigidbody2D.velocity += (new Vector2(0f, m_JumpVelocity * (inWater ? m_WaterJumpMultiplier : 1))); //Sometimes infinite jump or water jump can be exploited to lead to high speeds. This clamps the velocity down. if (m_Rigidbody2D.velocity.y > m_MaxJumpVelocity) { m_Rigidbody2D.velocity = new Vector2(m_Rigidbody2D.velocity.x, m_MaxJumpVelocity); } if (jumpSound) { global.audio.RandomSoundEffect(jumpSound); } } } inWater = false; }