PC——端拿取物体

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

public class Ray_hit : MonoBehaviour {
    private GameObject demo;
    private int layer;
	// Use this for initialization
	void Start () {
        demo = new GameObject();
        layer = LayerMask.GetMask("Water");
    }
	
	// Update is called once per frame
	void Update () {
        
        Ray ratdemo = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ratdemo, out hit, Mathf.Infinity, layer))
        {
            demo.transform.position = new Vector3(hit.transform.position.x, hit.point.y, hit.point.z);


            print(hit.transform.name + "asda");

            if (Input.GetMouseButtonDown(0))
            {
                if (demo.transform.childCount == 0)
                {
                    if (hit.transform.tag == "touch")
                    {
                        hit.transform.SetParent(demo.transform);
                        print(hit.transform.name);

                        hit.transform.position = new Vector3(hit.transform.position.x, hit.point.y, hit.point.z);                       
                    }
                }
            }
            if (Input.GetMouseButtonUp(0))
            {
                hit.transform.SetParent(null);
                foreach (Transform item in demo.transform)
                {
                    item.SetParent(null);
                }
            }
        }
	}
}

猜你喜欢

转载自blog.csdn.net/fanfan_hongyun/article/details/80925193