unity_物体跟随鼠标移动

1.2DSprite 鼠标点击屏幕并移动

//该脚本挂在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);
        }
    }

原文地址:https://www.cnblogs.com/Ms-Sake/p/11766017.html