unity 2D 人物按钮控制移动

unity 2D 人物按钮控制移动

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Animator))]
public class CharacterControllerScript : MonoBehaviour
{
    
    
    public float x;
    public float y;
    public float moveSpeed = 1.0f;
    public bool isMoving = false;
    public Animator animator;
    public Rigidbody2D rigidbody2;
    private void Awake()
    {
    
    
        animator = this.transform.GetComponent<Animator>();
        rigidbody2 = this.transform.GetComponent<Rigidbody2D>();
    }
     void Update()
     {
    
    
         if (Input.GetKeyDown(KeyCode.S))
         {
    
    
             x = -1;
             y = 0;
             isMoving = true;
         }
         else if (Input.GetKeyDown(KeyCode.A))
         {
    
    
             x = 0;
             y = -1;
             isMoving = true;
         }
         else if (Input.GetKeyDown(KeyCode.D))
         {
    
    
             x = 0;
             y = 1;
             isMoving = true;
         }
         else if (Input.GetKeyDown(KeyCode.W))
         {
    
    
             x = 1;
             y = 0;
             isMoving = true;
         }
         else if(Input.GetKeyUp(KeyCode.W)|| Input.GetKeyUp(KeyCode.S)|| Input.GetKeyUp(KeyCode.A)|| Input.GetKeyUp(KeyCode.D))
         {
    
    
            isMoving = false;
            animator.SetFloat("X", x);
            animator.SetFloat("Y", y);
            animator.SetLayerWeight(1, 0);

        }
         if (isMoving)
         {
    
    
            animator.SetFloat("X", x);
            animator.SetFloat("Y", y);
            animator.SetLayerWeight(1, 1);
             Vector3 old = rigidbody2.transform.position;
             Vector3 vector3 = new Vector3(y, x, 0);
             rigidbody2.transform.position = Vector3.Lerp(old, old + vector3.normalized*0.1f , moveSpeed * Time.deltaTime);
         }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Animator))]
public class CharacterButtonControl : MonoBehaviour
{
    
    
    public Button buttonUp;
    public Button buttonDown;
    public Button buttonleft;
    public Button buttonRight;
    public GameObject player;
    public Rigidbody2D rigidbody2D;
    private IEnumerator moveCoroutine;
    public Animator animator;
    // Start is called before the first frame update
    private void Awake()
    {
    
    
        rigidbody2D = player.GetComponent<Rigidbody2D>();
        animator = this.transform.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
    
    
        buttonUp.onClick.AddListener(buttonUP);
        buttonDown.onClick.AddListener(buttonDOWN); 
        buttonleft.onClick.AddListener(buttonLEFT); 
        buttonRight.onClick.AddListener(buttonRIGHT);
    }
    public void buttonUP() {
    
    
        if (moveCoroutine != null)
        {
    
    
            StopCoroutine(moveCoroutine);
        }
        animator.SetLayerWeight(1, 1);
        animator.SetFloat("X", 1);
        animator.SetFloat("Y", 0);
        moveCoroutine = MoveCoroutine(new Vector3(0,1f,0), 2);
        StartCoroutine(moveCoroutine);
    }
    public void buttonDOWN()
    {
    
    
        if (moveCoroutine != null)
        {
    
    
            StopCoroutine(moveCoroutine);
        }
        animator.SetLayerWeight(1, 1);
        animator.SetFloat("X", -1);
        animator.SetFloat("Y", 0);
        moveCoroutine = MoveCoroutine(new Vector3(0, -1f, 0), 2);
        StartCoroutine(moveCoroutine);
    }
    public void buttonLEFT()
    {
    
    
        if (moveCoroutine != null)
        {
    
    
            StopCoroutine(moveCoroutine);
        }
        animator.SetLayerWeight(1, 1);
        animator.SetFloat("X", 0);
        animator.SetFloat("Y", -1);
        moveCoroutine = MoveCoroutine(new Vector3(-1f, 0, 0), 2);
        StartCoroutine(moveCoroutine);
    }
    public void buttonRIGHT()
    {
    
    
        if (moveCoroutine != null)
        {
    
    
            StopCoroutine(moveCoroutine);
        }
        animator.SetLayerWeight(1, 1);
        animator.SetFloat("X", 0);
        animator.SetFloat("Y", 1);
        moveCoroutine = MoveCoroutine(new Vector3(1f, 0, 0), 2);
        StartCoroutine(moveCoroutine);
    }
    private IEnumerator MoveCoroutine(Vector3 newPositon, float time)
    {
    
    
        Vector3 startPos = rigidbody2D.transform.position;
        Vector3 endPos = rigidbody2D.transform.position + newPositon;
        for (float t = 0; t < time; t += Time.deltaTime)
        {
    
    
            rigidbody2D.transform.position = Vector3.Lerp(startPos, endPos, t / time);
            yield return 0;
        }
        rigidbody2D.transform.position = endPos;
        animator.SetLayerWeight(1, 0);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_36382679/article/details/113446307