1.2DSprite 鼠标点击屏幕并移动
unity_物体跟随鼠标移动

//该脚本挂在Sprite上
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
    
    /// <summary>
    /// 判断玩家是否可以移动
    /// </summary>
    bool isMove;
    void Update()
    {
        //开启左右移动
        if (Input.GetMouseButtonDown(0))
            isMove = true;
        if (Input.GetMouseButtonUp(0))
            isMove = false;

        if (isMove)
        {
            Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            //限制超出屏幕
            if (pos.x < -2)
                pos.x = -2;
            else if (pos.x > 2)
                pos.x = 2;
            transform.position = new Vector3(Mathf.Lerp(transform.position.x, pos.x, 1.5f * Time.deltaTime), 0, -9);
        }
    }

相关文章:

  • 2021-07-24
  • 2022-12-23
  • 2021-07-22
  • 2021-09-07
  • 2022-12-23
  • 2022-01-10
  • 2021-05-19
猜你喜欢
  • 2022-01-10
  • 2021-08-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-14
相关资源
相似解决方案