【发布时间】:2019-09-04 13:58:13
【问题描述】:
2 个项目放置在 AR 中。我想在其中一项位于屏幕中间时旋转其中一项(使用触控和 Raycast)。
我希望在相机注视物体时执行代码的所有旋转部分。
这是我在场景中放置的对象上使用的脚本,移动功能有效,因为只有当您点击对象所在的屏幕并将其拖走(Raycast 到对撞机)时才会激活。但是,当我在任何地方滑动时,我的旋转对场景中的所有对象都有效,因为如果我在对撞机上滑动,我会移动对象,所以为了旋转,我必须留在对撞机之外。
public class MoveObject : MonoBehaviour
{
public bool holding;
private Component rotator;
int doubleTap = 0;
void Start()
{
holding = false;
}
void Update()
{
if (holding)
{
Move();
}
// One finger
if (Input.touchCount == 1)
{
// Tap on Object
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit hitTouch;
if (Physics.Raycast(ray, out hitTouch, 100f))
{
if (hitTouch.transform == transform)
{
holding = true;
}
}
}
// Release
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
holding = false;
}
}
if (Input.touchCount == 1 && !holding) //THIS is the rotation part
{
// GET TOUCH 0
Touch touch0 = Input.GetTouch(0);
// APPLY ROTATION
if (touch0.phase == TouchPhase.Moved)
{
transform.Rotate(0f, touch0.deltaPosition.x * 0.5f, 0f);
}
}
}
void Move()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
// The GameObject this script attached should be on layer "Surface"
if (Physics.Raycast(ray, out hit, 30.0f,
LayerMask.GetMask("Surface")))
{
transform.position = new Vector3(hit.point.x,
transform.position.y,
hit.point.z);
}
}
}
【问题讨论】:
-
不,那是为了轮换。如果我按照您的建议进行操作,我的对象将同时移动和旋转。 (我知道这是因为我之前忘记了“!”)
标签: c# android unity3d augmented-reality