我对团结真的很陌生,我正在尝试打牌,而且我遇到了坐标问题。我试图获得触摸的位置并使精灵移动到屏幕中的特定位置。这是我在c#中的解决方法脚本:
using UnityEngine;
using System.Collections;
public class CardMovement : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Rect recta = new Rect (-4.71f,-3.98f,4.52f,6.8f);
Touch To = new Touch ();
Camera C = GetComponent<Camera>();
Vector3 p = new Vector3 ();
p = C.ScreenToWorldPoint (To.position);
if (recta.Contains(p)== true){
transform.Translate(0.79f,-1.13f,0f);
}
}
}
问题是我不能让它移动到那个特定的位置。是因为坐标不匹配?有没有办法直接获取精灵的坐标而不键入它们?感谢您的帮助;)
答案 0 :(得分:0)
如果你想要做的是检测用户敲击表面/区域的位置,该对象必须有一个对撞机,你应该使用Raycast来检测碰撞的位置。
Moving an Agent to a Position Clicked by the Mouse
在此示例中,他们使用统一的navmesh,但您不必,您要查找的信息位于 hit.point
答案 1 :(得分:0)
I managed to move the card to the position I wanted ( not exactly ) And used another code that i built myself , your proposition I didn't use because I didn't understand or i guess it's using 3D script ( If i'm wrong please correct me ) so here is the updated version of my code :
using UnityEngine;
using System.Collections;
public class CardMovement : MonoBehaviour {
public RectTransform rectangle;
// Use this for initialization
void Start () {
rectangle = GetComponent<RectTransform> ();
}
// Update is called once per frame
void Update () {
if (rectangle.rect.Contains (Input.GetTouch (0).deltaPosition)){
Vector2 V = new Vector2 (0.125f, 0.125f);
transform.position = V;
}
}
}
Now the main problems that i face :
Camera.ScreenToWorldpoint
or Camera.ScreenToViewportpoint
the card didn't respond to the touch ( i guess it a problem of coordinates ) NOW THE MAIN QUESTION : WHAT IS THE DIFFERENCE BETWEEN THESE COORDINATES : World coordinates , screen coordinates and view port coordinates . I didn't find an explanation in the unity manual .
Thanks for help :) .