Unity点击后物体随鼠标移动

在屏幕中鼠标左键按住不放,之后移动鼠标时脚本绑定的gameObject随鼠标移动而改变位置。

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

public class transformAndRotate : MonoBehaviour
{
    void Start()
    {

    }
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            float h = Input.GetAxis("Mouse X");
            float v = Input.GetAxis("Mouse Y");
            gameObject.transform.position += new Vector3(h, v, 0);
        }
    }
}
 

你可能感兴趣的:(Unity)