首页 > 解决方案 > 统一更改按钮按下时的游戏对象速度

问题描述

因此,我正在尝试将破折号机制添加到正在构建的游戏角色中。但是由于某种原因,我无法让游戏对象的速度真正改变。我尝试使用有效的 addForce,但我必须添加很多才能获得所需的效果,并且有时表现得很奇怪!

我是否需要对游戏对象的速度做任何比我已经做的事情?

这是我的脚本:

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

public class PlayerController : MonoBehaviour
{

    public float dashSpeed;
    private int dashDirection;
    private float dashCoolDown;
    public float startDashCoolDown;
    private float dashTime;
    public float startDashTime;
    public GameObject dashEffect;

    public float speed;
    public float jumpForce;
    private float moveInput;

    private Rigidbody2D rb;
    private bool isFacingRight = true;

    private Animator anim;
    private bool isGrounded;
    public Transform groundCheck;
    public float checkRadius;
    public LayerMask whatIsGround;

    private int extraJumps;
    public int extraJumpsValue;

    public GameObject dustEffect;
    public GameObject trailEffect;

    private void Awake()
    {
        // Setting up references.
        isGrounded = transform.Find("GroundCheck");
        anim = GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();
    }

    void Start()
    {
        extraJumps = extraJumpsValue;
        rb = GetComponent<Rigidbody2D>();
        dashTime = startDashTime;
        dashCoolDown = startDashCoolDown;
    }

    void FixedUpdate()
    {

        isGrounded = false;

        // Check to see if grounded
        Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, checkRadius, whatIsGround);
        for (int i = 0; i < colliders.Length; i++)
        {
            if (colliders[i].gameObject != gameObject)
            {
                isGrounded = true;
                anim.SetBool("Ground", isGrounded);
            }
        }

        // Check if movement is allowed
        if (!GameMaster.disableMovement)
        {
            // Move character
            moveInput = Input.GetAxis("Horizontal");
            rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
            Instantiate(trailEffect, groundCheck.position, Quaternion.identity);
            anim.SetFloat("Speed", Mathf.Abs(moveInput));
        }

        // Flip character
        if (isFacingRight == false && moveInput > 0)
        {
            Flip();
        }
        else if (isFacingRight == true && moveInput < 0)
        {
            Flip();
        }
    }

    private void Update()
    {
        // Check if the player is grounded
        if (isGrounded == true)
        {
            extraJumps = extraJumpsValue;
        }

        // Check if movement is allowed
        if (!GameMaster.disableMovement)
        {

            // Check for jump
            // If the player has more than one jump available
            if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0)
            {
                isGrounded = false;
                anim.SetBool("Ground", isGrounded);
                rb.velocity = Vector2.up * jumpForce;
                extraJumps--;
                Instantiate(dustEffect, groundCheck.position, Quaternion.identity);
            }

            // If the player only has one jump available
            if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true)
            {
                isGrounded = false;
                anim.SetBool("Ground", isGrounded);
                rb.velocity = Vector2.up * jumpForce;
            }

            // Check for dash
            if (dashCoolDown <= 0)
            {
                if (Input.GetKeyDown(KeyCode.LeftShift))
                {
                    anim.SetBool("Dash", true);
                    Dash();
                    dashTime = startDashTime;
                }
            }
            else
            {
                dashCoolDown -= Time.deltaTime;
            }
        }

    }

    void Dash()
    {
        if (dashTime <= 0)
        {
            dashCoolDown = startDashCoolDown;
            anim.SetBool("Dash", false);
        }
        else
        {
            dashTime -= Time.deltaTime;

            if (isFacingRight)
            {
                rb.velocity = Vector2.right * dashSpeed;
            }
            else if (!isFacingRight)
            {
                rb.velocity = Vector2.left * dashSpeed;
            }
        }
    }

    void Flip()
    {
        isFacingRight = !isFacingRight;

        // Multiply the player's x local scale by -1.
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }
}

标签: c#unity3d

解决方案


如果您没有通过 Rigidbody.velocity 获得所需的破折号效果,您可以尝试使用 Rigidbody.addForce()。我知道您说过您已经使用过它,但是您可以在此处应用多种不同的力模式。我建议使用这个,因为它从一开始就全力以赴,然后将其放下。您可以在此处自行阅读更多相关信息。

所以你可以像这样修改你的代码:

void Dash()
    {
        if (isFacingRight)
        {
            rb.AddForce(Vector2.right*dashSpeed,  ForceMode.VelocityChange);
        }
        else if (!isFacingRight)
        {
            rb.AddForce(Vector2.left*dashSpeed,  ForceMode.VelocityChange);
        }
    }

请注意,您也不需要反复调用 dash 来衰减速度。这个函数现在应该只在按下破折号(shift)按钮时调用。

希望这有帮助!


推荐阅读