Unity拖拽物体-在一个平面拖拽

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;

    }

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来为你解答这个问题。 要实现鼠标拖动物体在2D平面围绕一个点旋转,你可以按照以下步骤进行操作: 1. 在Unity中创建一个2D场景,并将需要旋转的物体放置在场景中。 2. 在需要围绕的点位置上创建一个物体,作为旋转的中心点。 3. 在需要旋转的物体上添加一个脚本,用于控制鼠标拖动旋转的行为。 4. 在脚本中,使用Input.GetAxis来获取鼠标在水平和垂直方向上的移动距离。 5. 根据鼠标移动的距离,计算出旋转的角度,并将物体绕中心点旋转到该角度。 下面是一个示例代码,你可以参考一下: ```csharp public class RotateObject : MonoBehaviour { public Transform rotateCenter; // 旋转中心点 public float rotateSpeed = 5f; // 旋转速度 private Vector3 previousMousePosition; void OnMouseDown() { previousMousePosition = Input.mousePosition; } void OnMouseDrag() { Vector3 currentMousePosition = Input.mousePosition; float rotateAngle = (currentMousePosition.x - previousMousePosition.x) * rotateSpeed; transform.RotateAround(rotateCenter.position, Vector3.forward, rotateAngle); previousMousePosition = currentMousePosition; } } ``` 在上面的代码中,OnMouseDown和OnMouseDrag函数分别用于获取鼠标按下和拖动的事件。在OnMouseDrag函数中,我们通过计算鼠标移动的距离,来计算旋转的角度,并使用transform.RotateAround函数来实现绕中心点旋转的效果。 希望这个回答能够帮到你。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值