using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DragObjectManager : MonoBehaviour
{
bool isDraging;
GameObject curGameObject;
Vector3 followPos;
public static DragObjectManager instance;
public float height;
// Start is called before the first frame update
void Start()
{
instance = this;
}
public GameObject GetcurGameObject()
{
return curGameObject;
}
// Update is called once per frame
void Update()
{
if (curGameObject)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//transform.position = hit.point + new Vector3(0, 0.5f, 0);
followPos = hit.point + new Vector3(0, height, 0);
}
curGameObject.transform.position = Vector3.Lerp(curGameObject.transform.position,followPos,Time.deltaTime*5);
}
}
public void SetCurGameObject(GameObject gameObject)
{
curGameObject = gameObject;
gameObject.GetComponent<Collider>().enabled = false;
}
public void SetCurGameObject(GameObject GameObject,float height)
{
this.height = height;
curGameObject = GameObject;
GameObject.GetComponent<Collider>().enabled = false;
}
public void ReleaseCurGameObject()
{
curGameObject.GetComponent<Collider>().enabled = true;
curGameObject = null;
}
}
Unity拖拽物体-在一个平面拖拽
于 2024-03-05 10:11:21 首次发布