使对象移动到鼠标单击的位置 2d
make object move to the mouse clicked position 2d
这是unity中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");
}
}
这里有两点可能是错误的:
1) 您可能不会获得 Raycast
命中率
Raycast 场景中需要 3D 个碰撞体。如果你没有它们,你就不会受到打击,因此代码的移动部分永远不会被激活。
2) 您可能混合了 2D 和 3D
如果您使用的是纯 2D 设置,则必须使用 Physics2D
而不是 Physics
。所以它将是 Physics2D.Raycast.
这也将要求您使用 2D 碰撞器。一个例子是 PolygonCollider2D
基本上:如果 class 不以 2D 结尾,它将不适用于 Physics2D。
假设这段代码可以正常工作,而您要做的就是让它在 2D 中工作
RaycastHit
should be RaycastHit2D
Physics.Raycast
should be Physics.Raycast2D
您所做的就是将 2D
添加到那些 API 的末尾,然后在编辑器中将所有碰撞器更改为 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);
}
和
Vector2 ray = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray, Vector2.zero);
if (hit)
{
flag = true;
endPoint = hit.point;
endPoint.y = yAxis;
Debug.Log(endPoint);
}
这是unity中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");
}
}
这里有两点可能是错误的:
1) 您可能不会获得 Raycast
命中率
Raycast 场景中需要 3D 个碰撞体。如果你没有它们,你就不会受到打击,因此代码的移动部分永远不会被激活。
2) 您可能混合了 2D 和 3D
如果您使用的是纯 2D 设置,则必须使用 Physics2D
而不是 Physics
。所以它将是 Physics2D.Raycast.
这也将要求您使用 2D 碰撞器。一个例子是 PolygonCollider2D
基本上:如果 class 不以 2D 结尾,它将不适用于 Physics2D。
假设这段代码可以正常工作,而您要做的就是让它在 2D 中工作
RaycastHit
should be RaycastHit2D
Physics.Raycast
should be Physics.Raycast2D
您所做的就是将 2D
添加到那些 API 的末尾,然后在编辑器中将所有碰撞器更改为 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);
}
和
Vector2 ray = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray, Vector2.zero);
if (hit)
{
flag = true;
endPoint = hit.point;
endPoint.y = yAxis;
Debug.Log(endPoint);
}