private void MovementHandler() { float horizontal = Input.GetAxis("Horizontal"); float vertical = Input.GetAxis("Vertical"); if (!Mathf.Approximately(vertical, 0.0f) || !Mathf.Approximately(horizontal, 0.0f)) { Vector3 direction = new Vector3(horizontal, vertical, 0.0f); direction = Vector3.ClampMagnitude(direction, 1.0f); if (direction.x > 0.00f) { FaceDirection(Vector2.right); } else { FaceDirection(Vector2.left); } direction.x = direction.x * mMoveSpeedX; direction.y = direction.y * mMoveSpeedY; //Jump Check if (mJumping) { direction.x /= 1.2f; direction.y /= 2.0f; mGroundY += direction.y * Time.deltaTime; direction.y = 0; } transform.Translate(direction * Time.deltaTime, Space.World); if (!mJumping) { mGroundY = transform.position.y; } mMoving = true; mWalking = inStory; mRunning = (!inStory); // if u pass the bottom of the floor boundary if (!mJumping && mGroundY < mFloorBoundary [Floor.Y_MIN_INDEX] && Input.GetKey(KeyCode.Space)) { int newFloorIndex = mFloorControllerRef.NextFloorDown(mFloorIndex); if (newFloorIndex != mFloorIndex) { mFloorIndex = newFloorIndex; mFloorControllerRef.GetCurrentFloorBoundary(mFloorBoundary, mFloorIndex, mSpriteRenderer); mJumping = true; mFalling = true; mGroundY = mFloorBoundary [Floor.Y_MAX_INDEX]; mRigidBody.useGravity = true; } } else if (!mJumping && mGroundY > mFloorBoundary [Floor.Y_MAX_INDEX] && Input.GetKey(KeyCode.Space)) { int newFloorIndex = mFloorControllerRef.NextFloorUp(mFloorIndex); if (newFloorIndex != mFloorIndex) { // check if the player has even reached the next level in terms of animation. float[] newFloorBoundary = new float[4]; mFloorControllerRef.GetCurrentFloorBoundary(newFloorBoundary, newFloorIndex, mSpriteRenderer); //if (transform.position.y > newFloorBoundary [Floor.Y_MIN_INDEX]) { mFloorIndex = newFloorIndex; mFloorBoundary = newFloorBoundary; mGroundY = mFloorBoundary [Floor.Y_MIN_INDEX]; mJumping = true; //} } } mGroundY = Mathf.Clamp(mGroundY, mFloorBoundary [Floor.Y_MIN_INDEX], mFloorBoundary [Floor.Y_MAX_INDEX]); } }
void Update() { if (!floorBoundaryInitialized) { // get current boundary mFloorControllerRef.GetCurrentFloorBoundary(mFloorBoundary, mFloorIndex, mSpriteRenderer); floorBoundaryInitialized = true; } ResetBoolean(); if (mNormalAttack > 0 && mAnimator.GetCurrentAnimatorStateInfo(0).IsName("Idle")) { mNormalAttack = 0; mHitting = false; } if (Input.GetKeyDown("n")) { mHealthBarRef.LoseHealth(20.0f); } else if (Input.GetKeyDown("m")) { mHealthBarRef.GainHealth(15.0f); } if (Input.GetKeyDown("z")) { //if (!mAnimator.GetCurrentAnimatorStateInfo (0).IsName ("Idle")) mNormalAttack++; mHitting = true; } else if (Input.GetKey("w")) { mStrongAttack++; //mHitting = true; } if (mJumping && transform.position.y <= 0) { mJumping = false; } else if (mGetHit && mAnimator.GetCurrentAnimatorStateInfo(0).IsName("Idle")) { mGetHit = false; } else if (mSliding && mAnimator.GetCurrentAnimatorStateInfo(0).IsName("Idle")) { mSliding = false; } if (mAnimator.GetCurrentAnimatorStateInfo(0).IsName("Jumping")) { mFalling = true; } else { mFalling = false; } if (!mGetHit) { if (Input.GetKey("space")) { Defend(); } else if (Input.GetKeyDown("a")) { Jump(); } else { if (Input.GetButton("Left")) { MovingLeft(); } else if (Input.GetButton("Right")) { MovingRight(); } if (Input.GetButton("Up")) { MovingUp(); } else if (Input.GetButton("Down")) { MovingDown(); // if u pass the bottom of the floor boundary if (transform.position.y < mFloorBoundary[Floor.Y_MIN_INDEX]) { int newFloorIndex = mFloorControllerRef.NextFloorDown(mFloorIndex); if (newFloorIndex != mFloorIndex) { mFloorIndex = newFloorIndex; mFloorControllerRef.GetCurrentFloorBoundary(mFloorBoundary, mFloorIndex, mSpriteRenderer); mJumping = true; mFalling = true; } } } transform.position = new Vector3(Mathf.Clamp(transform.position.x, mFloorBoundary[Floor.X_MIN_INDEX], mFloorBoundary[Floor.X_MAX_INDEX]), Mathf.Clamp(transform.position.y, mFloorBoundary[Floor.Y_MIN_INDEX], mFloorBoundary[Floor.Y_MAX_INDEX]), transform.position.z); UpdateOrderInLayer(); } } if (Input.GetKeyDown("s")) { GetHit(); } else if (Input.GetKeyDown("d")) { GetKnockdown(); } else if (mMoving && Input.GetKeyDown("f")) { Slide(); } else if (Input.GetKeyDown("q")) { Dash(); } // if one is not jumping or falling, then they must be on the floor, meaning they must abide by the boundaries. if (!mJumping && !mFalling) { transform.position = new Vector3(Mathf.Clamp(transform.position.x, mFloorBoundary[Floor.X_MIN_INDEX], mFloorBoundary[Floor.X_MAX_INDEX]), Mathf.Clamp(transform.position.y, mFloorBoundary[Floor.Y_MIN_INDEX], mFloorBoundary[Floor.Y_MAX_INDEX]), transform.position.z); } UpdateOrderInLayer(); UpdateAnimator(); }