How to change player movement pc to android ui button platformer unity2d

问题描述 投票:0回答:0

当玩家停止运行时,我无法让玩家在空闲状态下左右移动,使用此脚本,当游戏开始时,玩家将开始动画状态 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

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

c# unityscript
© www.soinside.com 2019 - 2024. All rights reserved.