【问题标题】:RaycastHit2D - touch for 2D gameRaycastHit2D - 触摸 2D 游戏
【发布时间】:2016-07-24 11:20:30
【问题描述】:

我正在尝试为触控设备构建 2D 游戏。现在我正在尝试对场景中的对撞机投射光线来做一些事情。例如,如果击中射线击中左键,则玩家向左移动。

我在 google 和 youtube 上到处搜索,但还没有想出如何做到这一点。我是 Unity 和一般编程的新手,但从我的搜索来看,如果我想检测 2D 游戏的屏幕触摸似乎比 3D 游戏更复杂!!!

void Update () 
{
 if (Input.touchCount > 0 )
 {
  for ( int i = 0; i < Input.touchCount; i++)
  { 
   if ( Input.GetTouch(i).phase == TouchPhase.Began)
   {
    Ray2D ray = Camera.main.ScreenToWorldPoint (Input.GetTouch(i).position);
    RaycastHit2D hit;

    if ( Physics2D.Raycast ( ray, out hit) ) )
    {
     if (hit.transform.gameobject.name == "left")
     {
      // Do Something
     }
    }
   }
  }
 }

【问题讨论】:

    标签: c# android ios unity3d touch


    【解决方案1】:

    不知道这是否有帮助,但我就是这样做的:

    //Get current worldspace position of mouse cursor
        RaycastHit2D[] hits = Physics2D.LinecastAll(clickedPos,clickedPos,theLayer);
    
        // Copy Linecast to HashSet (unique records), clean out the "Background" GameObject
        foreach (RaycastHit2D arf2D in hits) {
            //linecast_GameObject = GameObject.FindWithTag(arf2D.collider.tag);
            linecast_GameObject = GameObject.Find(arf2D.collider.name);
    
            //if (linecast_GameObject.name != "Background") {
            if (linecast_GameObject.tag != "Background") {
                linecast_GameObject_HashSet.Add(linecast_GameObject);
                clickedOnTheBackground = false;
            }
            else if (hits.Length == 1) {
                clickedOnTheBackground = true;
                fingerSelection_GO = GameObject.FindWithTag("Dummy_GameObject");
                //fingerSelection_GO = GameObject.Find("Dummy_GameObject");
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      相关资源
      最近更新 更多