簡體   English   中英

在 Unity 中與游戲對象發生碰撞時,如何讓我的玩家加速?

[英]How do I make my player speed up when collide with a game object in Unity?

我正在 Unity 中做一個學校項目。 我和我的團隊決定制作一個無盡的 2D 游戲。 但是,因為是第一次使用C#,不知道如何讓我的播放器在Unity中與游戲對象發生碰撞時加速。 我只知道在發生碰撞時如何破壞玩家的健康。 請幫我! 謝謝!

在沒有看到您編寫的任何代碼的情況下很難給出答案,但是由於它是 2D 並且您已經使碰撞損壞起作用,您可能已經使用了OnCollisionEnter() ,它非常相似:您測試是否' ve 碰撞(您已經完成了),然后您使用剛體 2d 向您的播放器添加力,可能類似於以下內容:

public Rigidbody2D rb;

void OnCollisionEnter2D(Collision2D collision)
{
    rb.AddForce(direction * force, ForceMode2D.Impulse); // the ForceMode2D is 
//                                                  optional, it's just so that
//                                              the velocity change is sudden.
}

這應該有效。

如果您有一個存儲游戲速度的 GameManager,您也可以這樣做:

private float gameSpeedMultiplier = 0.5f;

void OnCollisionEnter(Collision collision)
{
    if(collision.gameObject.CompareTag("Tag of the collided object")
    {
        GameManager.Instance.gameSpeed += gameSpeedMultiplier;
    }
}
public class PlayerContoler : MonoBehaviour
{
    public static PlayerContoler instance;

    public float moveSpeed;
    public Rigidbody2D theRB;
    public float jumpForce;

    private bool isGrounded;
    public Transform GroundCheckPoint;
    public LayerMask whatIsGround;

    private bool canDoubleJump;

    private Animator anim;
    private SpriteRenderer theSR;

    public float KnockbackLength, KnockbackForce;
    private float KnockbackCounter;

    public float bonceForce;

    public bool stopImput;

    private void Awake()
    {
        instance = this;
    }

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
        theSR = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if(!PauseMenu.instance.isPause && !stopImput)
        {
            if (KnockbackCounter <= 0)
            {

             theRB.velocity = new Vector2(moveSpeed * Input.GetAxis("Horizontal"), theRB.velocity.y);

             isGrounded = Physics2D.OverlapCircle(GroundCheckPoint.position, .2f, whatIsGround);

             if (isGrounded)
             {
                 canDoubleJump = true;
             }

             if (Input.GetButtonDown("Jump"))
             {
                 if (isGrounded)
                 {
                    theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
                     AudioManager.instance.PlaySFX(10);
                 }
                 else
                 {
                     if (canDoubleJump)
                     {
                        theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
                        canDoubleJump = false;
                         AudioManager.instance.PlaySFX(10);
                     }
                }
            }

            if (theRB.velocity.x < 0)
            {
                theSR.flipX = true;
            }
            else if (theRB.velocity.x > 0)
            {
                theSR.flipX = false;
            }
        } else
        {
            KnockbackCounter -= Time.deltaTime;
            if(!theSR.flipX)
            {
                theRB.velocity = new Vector2(-KnockbackForce, theRB.velocity.y);
            } else
            {
                theRB.velocity = new Vector2(KnockbackForce, theRB.velocity.y);
            }
        }

     }

        anim.SetFloat("moveSpeed", Mathf.Abs(theRB.velocity.x));
        anim.SetBool("isGrounded", isGrounded);
    }

    public void Knockback()
    {
        KnockbackCounter = KnockbackLength;
        theRB.velocity = new Vector2(0f, KnockbackForce);

        anim.SetTrigger("hurt");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM