首页 > 解决方案 > ac#统一跳转代码的一些问题

问题描述

这是我遇到问题的代码,事实上,当我按下空格键时,播放器只会重复跳跃动画,甚至右箭头也不再起作用。

请如果你能帮助你将非常受欢迎

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 ;      
        }              
    }       
}

标签: c#visual-studiounity3d2d-games

解决方案


不要忘记将跳转设置回 false

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

else当左箭头被(不)按下时,你也有一个。所以你的右箭头(不是左箭头)会将速度和速度设置为 0。


推荐阅读