2D平面游戏角色移动代码(PC端)

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

public class controler : MonoBehaviour
{
    private Rigidbody2D rigidbody2D;
    private Animator animator;
    private float speed = 20f; // 移动速度

    // Start is called before the first frame update
    void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        float horizontalInput = Input.GetAxisRaw("Horizontal"); // 使用 GetAxisRaw
        float verticalInput = Input.GetAxisRaw("Vertical");     // 使用 GetAxisRaw

        // 设置动画参数
        if (horizontalInput != 0)
        {
            animator.SetFloat("Horizontal", horizontalInput);
            animator.SetFloat("Vertical", 0);
        }
        if (verticalInput != 0)
        {

            animator.SetFloat("Vertical", verticalInput);
            animator.SetFloat("Horizontal", 0);
        }

        Vector2 dir = new Vector2(horizontalInput, verticalInput);

        animator.SetFloat("Speed", dir.magnitude);
        // 初始化移动向量为零向量
        Vector2 moveDirection = Vector2.zero;

        // 检查水平输入
        if (horizontalInput != 0)
        {
            moveDirection.x = horizontalInput;
        }

        // 检查垂直输入
        if (verticalInput != 0)
        {
            moveDirection.y = verticalInput;
        }

        // 检查是否只按了一个方向键
        if ((Mathf.Abs(horizontalInput) + Mathf.Abs(verticalInput)) == 1)
        {
            // 计算新的位置
            Vector2 newPosition = rigidbody2D.position + (moveDirection * speed * Time.deltaTime);

            // 使用 MovePosition 移动角色
            rigidbody2D.MovePosition(newPosition);
        }
    }
}

2D平面游戏角色移动代码(移动端)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MobilePlayerController : MonoBehaviour
{
    private Rigidbody2D rigidbody2D;
    private Animator animator;
    [SerializeField]
    private float speed = 3f; // 移动速度
    private float horizontalInput = 0;
    private float verticalInput = 0;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        // 设置动画参数
        if (horizontalInput != 0)
        {
            animator.SetFloat("Horizontal", horizontalInput);
            animator.SetFloat("Vertical", 0);
        }
        if (verticalInput != 0)
        {

            animator.SetFloat("Vertical", verticalInput);
            animator.SetFloat("Horizontal", 0);
        }

        Vector2 dir = new Vector2(horizontalInput, verticalInput);
        animator.SetFloat("Speed", dir.magnitude);

        MovePlayer(dir);

    }

    // 向上移动
    public void onPointerUp()
    {
        verticalInput = 1;
    }

    // 向下移动
    public void onPointerDown()
    {
        verticalInput = -1;
    }

    // 向左移动
    public void onPointerLeft()
    {
        horizontalInput = -1;
    }

    // 向右移动
    public void onPointerRight()
    {
        horizontalInput = 1;
    }

    // 如果没有按任何按钮则静止
    public void onIdle()
    {
        // 在按钮释放时停止移动
        horizontalInput = 0;
        verticalInput = 0;
    }

    private void MovePlayer(Vector2 direction)
    {
        // 计算新的位置
        Vector2 newPosition = rigidbody2D.position + (direction * speed * Time.deltaTime);

        // 使用 MovePosition 移动角色
        rigidbody2D.MovePosition(newPosition);
    }
}