【发布时间】:2020-09-14 18:56:59
【问题描述】:
我一直在开发 Unity 3D 游戏,其中涉及能够拾取一些物体。我目前的代码如下:
using System.Collections;
使用 System.Collections.Generic; 使用 UnityEngine;
public class pickUp : MonoBehaviour
{
public Transform dest;
void OnMouseDown() {
GetComponent<Rigidbody>().useGravity = false;
GetComponent<Rigidbody>().velocity = new Vector3(0f, 0f, 0f);
GetComponent<Rigidbody>().angularVelocity = new Vector3(0f, 0f, 0f);
this.transform.position = dest.position;
this.transform.parent = GameObject.Find("Destination").transform;
}
void OnMouseUp() {
this.transform.parent = null;
GetComponent<Rigidbody>().useGravity = true;
}
void Update() {
//where I need to detect mouseover
}
}
正如评论中所暗示的那样,我需要检测鼠标是否在对象上,否则它会关闭所有其他对象的物理。我也无法编写名称(所以使用 Ray),因为很多对象已经使用了该脚本。
谢谢!
编辑:旧版本代码,放入当前版本
【问题讨论】: