【发布时间】:2017-06-22 18:40:23
【问题描述】:
尝试创建类似 Connect 4 的游戏。我让电路板模型的孔与网格对齐,这样我就可以轻松地将圆圈放入其中。
问题是我不知道如何统一地使对象跟随鼠标同时也捕捉到网格。
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LockToGrid : MonoBehaviour {
public float gridSize;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
SnapToGrid(gameObject);
}
void SnapToGrid(GameObject Object) {
var currentPos = Object.transform.position;
Object.transform.position = new Vector3(Mathf.Round(currentPos.x / gridSize) * gridSize,
currentPos.y,
currentPos.z);
}
}
【问题讨论】:
-
在行上方添加一个不可见目标,如果这是统一 5,您可以在不可见图像中使用检查鼠标是否在目标上,将检查块放在目标中