我对团结很陌生,我试着玩纸牌游戏,我面临着坐标的问题。我试图获得触摸的位置,并使精灵移动到屏幕上的一个特定位置。下面是我在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);
}
}
}问题是,我不能让它移动到特定的位置,.is,因为坐标不匹配?有没有办法直接得到雪碧的坐标而不输入它们呢?谢谢你的帮助;)
发布于 2017-03-15 16:10:16
如果您要做的是检测用户正在敲击一个表面/区域的位置,这个对象必须有一个对撞机,您应该使用Raycast来检测碰撞的位置。
在本例中,他们使用了来自统一的导航网格,但您不必这样做,您要查找的信息在hit.point中。
发布于 2017-03-22 18:51:44
我设法将卡片移动到我想要的位置(不完全正确),并使用了我自己构建的另一段代码,您的建议我没有使用,因为我不明白,或者我猜它使用的是3D脚本(如果我错了,请更正),下面是我代码的更新版本:
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;
}
}
}现在我面临的主要问题是:
Camera.ScreenToWorldpoint或Camera.ScreenToViewportpoint时,卡对触摸没有反应(我猜这是一个坐标问题)现在的主要问题是:这些坐标之间的区别是什么:世界坐标、屏幕坐标和视图端口坐标。我在团结手册中没有找到解释。
谢谢你的帮助:)。
https://stackoverflow.com/questions/42795626
复制相似问题