【Unity3D】实现画板效果

2020/02/28

这里写图片描述 这里写图片描述

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

public class player : MonoBehaviour
{
    public GameObject PrintPrefab;
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition)+new Vector3(0,0,10));

        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition) + new Vector3(0, 0, 10);

        transform.position = mousePos;
        if (Input.GetMouseButton(0))
        {
            Instantiate(PrintPrefab, transform.position, transform.rotation);
        }
    }
}

代码解析: 1.屏幕坐标转化为三维坐标 Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition) + new Vector3(0, 0, 10); 2.物体跟随鼠标移动 transform.position = mousePos; 3.创建预制体 if (Input.GetMouseButton(0)) {       Instantiate(PrintPrefab, transform.position, transform.rotation); } 这里写图片描述 把脚本给一个空物体就行。空物体的坐标为(0,0,0),旋转坐标为(0,0,0)

(转载本站文章请注明作者和出处 恬静的小魔龙

Show Disqus Comments

Post Directory

扫码或搜索:沉默王二
发送 290992
即可立即永久解锁本站全部文章