【发布时间】:2021-08-08 11:44:21
【问题描述】:
我想重新创建一个游戏(七巧板),您可以在其中旋转和移动棋子。 游戏可以在这个网站上找到:https://de.mathigon.org/tangram
我找到了用于旋转和移动对象的脚本(一个 png 文件),但是当我将这两个脚本添加在一起时,对象会随机移动。作为程序,我使用 Unity。
移动脚本:
using UnityEngine;
using System.Collections;
public class DragDropScript : MonoBehaviour {
private Vector3 screenPoint;
private Vector3 offset;
void OnMouseDown()
{
Debug.Log("mouse down");
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
Debug.Log("mouse drag");
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}
还有旋转脚本
using UnityEngine;
using System.Collections;
public class ObjectRotator : MonoBehaviour
{
private float _sensitivity;
private Vector3 _mouseReference;
private Vector3 _mouseOffset;
private Vector3 _rotation;
private bool _isRotating;
void Start ()
{
_sensitivity = 0.4f;
_rotation = Vector3.zero;
}
void Update()
{
if(_isRotating)
{
// offset
_mouseOffset = (Input.mousePosition - _mouseReference);
// apply rotation
_rotation.z = -(_mouseOffset.x + _mouseOffset.y) * _sensitivity;
// rotate
transform.Rotate(_rotation);
// store mouse
_mouseReference = Input.mousePosition;
}
}
void OnMouseDown()
{
// rotating flag
_isRotating = true;
// store mouse
_mouseReference = Input.mousePosition;
}
void OnMouseUp()
{
// rotating flag
_isRotating = false;
}
}
到目前为止,我一直在与 Unity 斗争。在我的学士论文中,我得到了一个 HoloLens2,并且可以使用真正有助于开发应用程序的 MRTK。提前谢谢你。 最后,游戏应该构建为 WebGL。
【问题讨论】: