gpt4 book ai didi

unity3d - Unity - 用鼠标右键拖动?

转载 作者:行者123 更新时间:2023-12-04 08:42:44 26 4
gpt4 key购买 nike

我正在 Unity 2019.4.12f1 中创建构建,需要使用鼠标右键拖动对象。到目前为止,我的 C# 技能非常有限,但我尝试了。

这个脚本是我尝试通过按住鼠标右键并在游戏中拖动来旋转游戏对象。

但这是错误的。谁能帮我更正一下?´


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

public class mouseM : MonoBehaviour
{
bool dragging = false;

void Start()
{
if (Input.GetMouseButtonDown(1))
{
dragging = true;
}
}

void Update()
{
if (Input.GetMouseButtonDown(1))
{
yourOnMouseDownFunction();
dragging = true;
}
if (Input.GetMouseButtonUp(1))
{
yourOnMouseUpFunction();
dragging = false;
}

if (dragging)
{
yourOnMouseDragFunction();
}
}
}

最佳答案

我知道您需要在 3d 世界中拖动对象,所以这是我的代码,只需创建新脚本并附加到您想要拖动的对象即可

Your Object that You need to Drag it should has a collider

using UnityEngine;
public class DragableObject : MonoBehaviour
{
private Vector3 mOffset;
private float mZCoord;

private void OnMouseDrag()
{
transform.position = GetMouseWorldPos() + mOffset;
}

private void OnMouseDown()
{
mZCoord = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
mOffset = transform.position - GetMouseWorldPos();
}

private Vector3 GetMouseWorldPos()
{
Vector3 mosePoint = Input.mousePosition;
mosePoint.z = mZCoord;
var result = Camera.main.ScreenToWorldPoint(mosePoint);
return result;
}
}

你可以通过将它添加到球体或立方体来尝试它,如果你使用自定义形状,你应该确保对撞机的大小合适或有一个 meshcollier Demo

编辑

using UnityEngine;
public class DragableObject : MonoBehaviour
{
private bool isMouseDragging;
private Vector3 screenPosition;
private Vector3 offset;
private GameObject target;

GameObject ReturnClickedObject(out RaycastHit hit)
{
GameObject targetObject = null;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction * 10, out hit))
{
targetObject = hit.collider.gameObject;
}
return targetObject;
}

void Update()
{
if (Input.GetMouseButtonDown(1))
{
RaycastHit hitInfo;
target = ReturnClickedObject(out hitInfo);
if (target != null)
{
isMouseDragging = true;
Debug.Log("our target position :" + target.transform.position);
//Here we Convert world position to screen position.
screenPosition = Camera.main.WorldToScreenPoint(target.transform.position);
offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z));
}
}

if (Input.GetMouseButtonUp(1))
{
isMouseDragging = false;
}

if (isMouseDragging)
{
//tracking mouse position.
Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);

//convert screen position to world position with offset changes.
Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace) + offset;

//It will update target gameobject's current postion.
target.transform.position = currentPosition;
}

}
}

关于unity3d - Unity - 用鼠标右键拖动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64481530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com