unity3d 从键盘输入迁移到UI按钮

46qrfjad  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(144)

我有麻烦,使球员左,右闲置时,它停止运行,与此脚本时,游戏开始时,球员将启动动画状态1,可以运行,但在第一次运行后,球员不能运行了。

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;

public class UiPlayerControl : MonoBehaviour
{
    private Rigidbody2D rb;
    private BoxCollider2D coll;

    private Animator anim;
    private RaycastHit2D hit;
    [SerializeField] private LayerMask jumpableGround;

    
    [SerializeField] private float moveSpeed = 7f;
    [SerializeField] private float jumpForce = 14f;

    private bool TurnRight, TurnLeft, stop;

    private enum MovementState { Player_Idle, Player_Walk, Player_Jump, Player_Falling }
    // Start is called before the first frame update
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<BoxCollider2D>();
  
        anim = GetComponent<Animator>();

    }

    // Update is called once per frame
    private void FixedUpdate()
    {
        {
            
            
            if (TurnRight)
            {
                rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
                transform.localScale = new Vector2((Mathf.Sign(rb.velocity.x)), transform.localScale.y);
                UpdateAnimationState();

            }

            if (TurnLeft)
            {
                rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
                transform.localScale = new Vector2((Mathf.Sign(rb.velocity.x)), transform.localScale.y);
                UpdateAnimationState();
            }
            if (stop)
            {
                rb.velocity = new Vector2(moveSpeed = 0f, rb.velocity.y);
                UpdateAnimationState();
            }
        }

        if (Input.GetButtonDown("Jump") && IsGrounded())
        {

            rb.velocity = new Vector2(rb.velocity.x, jumpForce);

        }
        UpdateAnimationState();
    }

    public void MoveRight()
    {
        TurnRight = true;
        stop = false;
    }

    public void MoveLeft()
    {
        TurnLeft = true;
        stop = false;
    }

    public void Jump()
    {
        if (rb.velocity.y == 0 && IsGrounded())
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }
    public void stopMoving()
    {
        TurnLeft = false;
        TurnRight = false;
        stop = true;
    }

    private void UpdateAnimationState()
    {
        MovementState state;

        if (moveSpeed > 0f)
        {
            state = MovementState.Player_Walk;

        }
        else if (moveSpeed < 0f)
        {
            state = MovementState.Player_Walk;

        }

        else
        {
            state = MovementState.Player_Idle;
        }

        if (rb.velocity.y > .1f)
        {
            state = MovementState.Player_Jump;
        }
        else if (rb.velocity.y < -.1f)
        {
            state = MovementState.Player_Falling;
        }

        anim.SetInteger("state", (int)state);
    }
    private bool IsGrounded()
    {
        return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
    }





}type here

我想让玩家左右移动,但不移动时也可以闲置。

jvlzgdj9

jvlzgdj91#

对于简单的运动,你不需要定义多个重复函数的变量。只要优先级正确,每个变量都用在正确的地方就足够了。在下面的代码中,你可以通过获取Input.GetAxis()来使运动轴在-1和1之间。将这个值给rb.velocity.x来解决这个问题。

private void FixedUpdate()
{
    var x = 0; // default xAxis always 0
    if (TurnRight) x++;
    if (TurnLeft) x--;

    if (IsGrounded() && rb.velocity.y < 0)
    {
        rb.velocity.y = 0;
        if (Input.GetButtonDown("Jump"))
        {
            rb.AddForce(Vector2.up * jumpForce);
        }
    }
    if (x != 0)
    {
        rb.velocity = new Vector2(x * moveSpeed, rb.velocity.y); // multiply x axis to moveSpeed for final speed result
        transform.localScale = new Vector2(x, transform.localScale.y);
    }
    UpdateAnimationState();
}

相关问题