unity-鼠标拖拽物体

鼠标发射射线,通过点击更换地面的Tag来拖动不同的物体

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

public class MouseRaycast : MonoBehaviour
{
    public Camera thisCamera;
    public Transform my_target;
    public Transform my_target1;
    public GameObject planeObject;
    bool cubeflag;
    bool sphereflag;

    private void Start()
    {
        cubeflag = false;
        sphereflag = true;
    }
    void Update()
    {
        if (Input.GetMouseButton(0)&& sphereflag)
        {
            //从摄像机发出到点击坐标的射线
            Ray ray = thisCamera.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo))
            {
                //划出射线,只有在scene视图中才能看到
                Debug.DrawLine(ray.origin, hitInfo.point);
                GameObject gameObj = hitInfo.collider.gameObject;
                Debug.Log("click object name is " + 

猜你喜欢

转载自blog.csdn.net/qq_34216631/article/details/125287000
今日推荐