相同的按钮会根据玩家所触摸的内容执行不同的操作

时间:2018-12-25 16:50:08

标签: c# unity3d

我正在为移动设备开发2D无限亚军游戏。游戏使用4个带有不同颜色的按钮,基本上,您必须匹配挑战的颜色。

Example

目前,我有一个基于this

的基本攻击系统

我在实施this

时遇到了困难

我的问题:

  • 当角色位于允许他加速的平台之上时,则与该颜色匹配的任何按钮颜色都不应发起攻击,而应使用加速功能。
  • 放开超速按钮会使角色跳动,但我希望角色继续保持超速的X速度,直到它再次接触地面为止。假设玩家没有按下任何按钮。

我知道参考图像的速度在逐渐提高,但是让我们忽略它,而专注于在需要时改变速度的想法。

这是我角色的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

    [Header("Movement")]
    [SerializeField] float moveSpeed = 6;
    [Header("Jumping")]
    [SerializeField] float jumpHeight = 4;
    [SerializeField] float timeToMaxJumpHeight = .4f;
    [Header("Dashing")]
    [SerializeField] float dashSpeed = 10;
    [SerializeField] float dashDuration = 1;
    [Header("Ground Detection")]
    [SerializeField] Transform groundCheck;
    [SerializeField] float checkRadius;
    [SerializeField] LayerMask whatIsGround;
    [Header("Energy Blade")]
    //[SerializeField] EnergyBlade energyBlade;
    [SerializeField] GameObject myEnergyBlade;
    [SerializeField] float bladeHitboxDuration;

    private float targetFPS = 60;

    private float moveInput;

    // Jumping and gravity calculations
    private Vector2 gravity;
    private float jumpVelocity;
    private Vector3 velocity;

    // Checking flags
    private bool isGrounded;
    private bool isDashing;
    private bool canDash;
    private bool isJumping;

    // coroutines
    private IEnumerator myCoroutine; // General coroutine for the handler
    private IEnumerator dashingCoroutine;
    private IEnumerator attackCoroutine;

    //
    Rigidbody2D myRigidBody;

    // EnergyBlade properties
    BoxCollider2D energyBladeCollider;
    EnergyBlade energyBlade;

    // Speeding properties
    WorldColors platformColor = WorldColors.Neutral;
    bool speedingCommand;

    // Use this for initialization
    void Start()
    {
        myRigidBody = GetComponent<Rigidbody2D>();


        Physics2D.gravity = new Vector2(0, -(2 * jumpHeight) / Mathf.Pow(timeToMaxJumpHeight, 2)); // gravity calculation
        jumpVelocity = Mathf.Abs(Physics2D.gravity.y) * timeToMaxJumpHeight; // Jump velocity calculation

        // Energy Blade Assignments
        energyBladeCollider = myEnergyBlade.GetComponent<BoxCollider2D>();
        energyBlade = myEnergyBlade.GetComponent<EnergyBlade>();
    }

    private void FixedUpdate()
    {
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround); // change this to raycast

        Move();
        Jump();
    }

    void Update()
    {
        movementInput();
        ActionInput();
        ifNoSpeedingCommandOnGround();

    }

    // function to control the input of the player
    private void movementInput()
    {
        // move controls
        moveInput = Input.GetAxisRaw("Horizontal");
        // jump controls
        if (Input.GetKeyDown(KeyCode.Space))
        {
            isJumping = true;
        }
    }

    // function to control the player's horizontal movement
    private void Move()
    {
        float speed;
        speed = (isDashing) ? dashSpeed : moveSpeed;
        myRigidBody.velocity = new Vector2(moveInput * (speed * targetFPS) * Time.deltaTime, myRigidBody.velocity.y);

    }

    // function to allow the player to jump only if he's touching the ground
    private void Jump()
    {
        if (!isGrounded) return;
        if (isJumping)
        {
            myRigidBody.velocity = Vector2.up * (jumpVelocity * targetFPS) * Time.deltaTime;
            isJumping = false;
        }
    }



    private void ActionInput()
    {

        //print(IsSpeedingPlatform());

        // flag for hooks
        // flag for speeding platforms

        SpeedingInput(KeyCode.F, WorldColors.Green);
        SpeedingInput(KeyCode.G, WorldColors.Purple);
        SpeedingInput(KeyCode.H, WorldColors.Orange);
        SpeedingInput(KeyCode.J, WorldColors.Blue);


        EnergyBladeInput(KeyCode.F, WorldColors.Green);
        EnergyBladeInput(KeyCode.G, WorldColors.Purple);
        EnergyBladeInput(KeyCode.H, WorldColors.Orange);
        EnergyBladeInput(KeyCode.J, WorldColors.Blue);

    }


    private void ifNoSpeedingCommandOnGround()
    {
        print(speedingCommand);
        print(isGrounded);


        if (speedingCommand == false && isGrounded && !isJumping) // TODO CHECK TRHIS TO MAKE SURE IT DOESN'T CHECK IT IF THE PLAYER IS IN JUMPING PHASE
        {
            isDashing = false;
            print("speeding command is salfe and is grounded");
        }
        else if (speedingCommand == true)
        {
            isDashing = true;
        }

    }

    // function that sets the dashing to true if the color of the input and the platform match
    private void SpeedingInput(KeyCode key, WorldColors color)
    {
        //print("Speeding input enabled");
        //if (speedingCommand) return;
        if (Input.GetKeyDown(key))
        {
            speedingCommand = true;
        }
        else if (Input.GetKeyUp(key))
        {
            //speedingCommand = false;
            if (!isGrounded) return;
            isJumping = true;
        }

    }

    private bool IsSpeedingPlatform()
    {
        Collider2D[] collidedPlatforms = Physics2D.OverlapCircleAll(groundCheck.position, checkRadius, whatIsGround);
        if (collidedPlatforms.Length <= 0) return false;

        platformColor = collidedPlatforms[0].GetComponent<SpeedingPlatform>().platformColor;

        if (platformColor != WorldColors.Neutral)
        {
            return true;
        }
        else
        {
            isDashing = false;
            return false;
        }
    }

    private WorldColors SpeedingPlatformColor()
    {
        Collider2D[] collidedPlatforms = Physics2D.OverlapCircleAll(groundCheck.position, checkRadius, whatIsGround);
        if (collidedPlatforms.Length <= 0) return platformColor; // return the last color it was on if jumping/gliding

        return platformColor = collidedPlatforms[0].GetComponent<SpeedingPlatform>().platformColor;
    }


    /*
     * Energy Blade
     */

    // function that sends the color of the energy blade depending on the pressed key
    private void EnergyBladeInput(KeyCode key, WorldColors color)
    {
        if (Input.GetKeyDown(key))
        {
            BladeCoroutineHandler(color);
        }
    }

    // this function exists to prevent repeating the lines of stopping and starting coroutines.
    // Maybe it'll be converted to become a general function if needed
    private void BladeCoroutineHandler(WorldColors bladeColor)
    {
        if (myCoroutine != null) StopCoroutine(myCoroutine); // stop coroutine if it's already running
        myCoroutine = (bladeActiveHitbox(bladeColor)); // assign the coroutine
        StartCoroutine(myCoroutine); // start the assigned coroutine
    }

    // function to keep the collider of the energy blade to be enabled for x amount of time.
    private IEnumerator bladeActiveHitbox(WorldColors bladeColor)
    {
        energyBladeCollider.enabled = false; // disable the collider if it was already running from a previous attack
        energyBlade.bladeColor = bladeColor;// set blade color

        float duration = bladeHitboxDuration;
        while (duration >= 0)
        {
            energyBladeCollider.enabled = true;
            duration -= Time.deltaTime;
            yield return null;
        }
        energyBladeCollider.enabled = false;
    }
}

0 个答案:

没有答案