这是3D统一的代码。我将其更改为2D形式,但对象不会在2D世界中移动。作者说如果把单词从3D改为2D就没问题,但我似乎错过了什么。
private bool flag = false;
private Vector2 endPoint;
public float duration = 50.0f;
private float yAxis;
void Start()
{
yAxis = gameObject.transform.position.y;
}
void Update()
{
if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0)))
{
RaycastHit hit;
Ray ray;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
flag = true;
endPoint = hit.point;
endPoint.y = yAxis;
Debug.Log(endPoint);
}
}
if (flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
{
gameObject.transform.position = Vector2.Lerp(gameObject.transform.position, endPoint, 1 / (duration * (Vector2.Distance(gameObject.transform.position, endPoint))));
}
else if (flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
{
flag = false;
Debug.Log("I am here");
}
}
答案 0 :(得分:0)
这里有两件事可能是错的:
Raycast
点击Raycast需要场景中的 3D 碰撞器。如果你没有它们,你将无法获得命中,因此代码的移动部分永远不会被激活。
如果您使用纯粹的2D设置,则必须使用Physics2D
代替Physics
。所以它将是Physics2D.Raycast
这也需要您使用2D碰撞器。一个例子是PolygonCollider2D
基本上:如果课程没有以2D结尾,那么它将不适用于Physics2D。
答案 1 :(得分:0)
假设此代码正常运行,您只想让它在2D中运行
Physics.Raycast
应为Physics.Raycast2D
您只需将layer {
name: "classes"
type: "InnerProduct"
bottom: "ip2"
top: "classes"
param { lr_mult: 1 }
param { lr_mult: 2 }
inner_product_param {
num_output: 9
weight_filler { type: "xavier" }
bias_filler { type: "constant" }
}
}
layer {
name: "class_loss"
type: "SigmoidCrossEntropyLoss"
bottom: "classes"
bottom: "class_label"
top: "class_loss"
}
添加到这些API的末尾,然后在编辑器中将所有对撞机更改为2D
。例如,colliders 2D
应替换为Box Collider
。
替换:
Box Collider 2D
与
RaycastHit hit;
Ray ray;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
flag = true;
endPoint = hit.point;
endPoint.y = yAxis;
Debug.Log(endPoint);
}