簡體   English   中英

ac#統一跳躍代碼的一些問題

[英]some issues with a c# jumping code in unity

這是我遇到的代碼,實際上,當我按下空格鍵時,播放器將重復跳躍動畫,甚至右箭頭也不再起作用。

請如果您能幫助您,將不勝感激

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

using UnityEngine;

public class Player : MonoBehaviour
{
    Rigidbody2D rb2d;
    public float Speed = 30f;
    public float JumpForce = 30f;
    bool Jump;
    float speed;
    private bool  FacingRight;

    // Start is called before the first frame update
    void Start()
    {
            FacingRight = true;
            rb2d = GetComponent<Rigidbody2D>();
            rb2d.velocity = Vector2.zero;        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            GetComponent<Animator>().SetBool("Jump", true);        
        }

        if (Input.GetKey(KeyCode.RightArrow))
        {        
            GetComponent<Animator>().SetFloat("speed", 1);
            rb2d.velocity = Vector2.right * Speed * Time.deltaTime;                    
        }

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            GetComponent<Animator>().SetFloat("speed", 1);
            rb2d.velocity = Vector2.left * Speed * Time.deltaTime;                   
        }
        else
        {
            GetComponent<Animator>().SetFloat("speed", 0f);
            rb2d.velocity = Vector2.zero;                  
        }

    }

    private void FixedUpdate()
    {
        float horizontal = Input.GetAxis("Horizontal");
        Flip(horizontal);
    }

    private void Flip (float horizontal)
    {
        if (horizontal > 0 && !FacingRight || horizontal < 0 && FacingRight)
        {
            FacingRight = !FacingRight;
            Vector3 theScale = transform.localScale;
            theScale.x *= -1;
            transform.localScale = theScale;
        }
    }

     void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.name ==" Front_Buildings")
        {
            GetComponent<Animator>().SetBool("isGrounded", true);
            rb2d.velocity = Vector2.zero ;      
        }              
    }       
}

別忘了將您的跳轉設置為false

GetComponent<Animator>().SetBool("Jump", false);

你也有else時,左箭頭(不)壓制。 因此,您的向右箭頭(未向左箭頭)會將速度和速度設置為0。

好像您在此處將動畫師Jump屬性設置為'true'。

GetComponent<Animator>().SetBool("Jump", true);

您是否將其設置為false? 為了開始動畫,您只希望它一次為true,然后應將其設置為false,這樣動畫就不會無休止地重復。

暫無
暫無

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

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