【问题标题】:C# shoot towards mobile touchC# 面向移动触控
【发布时间】:2015-07-21 17:49:23
【问题描述】:

我目前正在开发一款游戏,用户点击屏幕(敌人),然后子弹应该从相机移动到触摸的位置以杀死敌人。距离摄像机(枪)10 个单位的敌人。

假设用户在 (x,y) = 5,5 处触摸,那么子弹应该从相机位置 0,0,-10 移动到 5,5,0。

我创建了一个 spawnPoint 并附加到相机,还将射击脚本附加到 spawnPoint。刚体弹丸是子弹预制件。

请帮我解决这个问题。

【问题讨论】:

  • 我不清楚你的问题到底是什么。您是在要求我们为您提供物理代码吗?还是有些事情没有按预期工作?
  • 您需要更具体地说明您的疑问,正如您所描述的那样,很难理解您迷路的地方。
  • 如何实现这是我的问题
  • 如何获取触摸输入的坐标?

标签: c# android mobile unity3d touch


【解决方案1】:
float distance = 10f;

void Update()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    Shoot(ray.GetPoint(distance));
}

public void Shoot(float point)
{
    // shoot from camera location to point here
}

【讨论】:

    【解决方案2】:

    以下方法非常适合基于触摸的输入。

    Vector3 fingerPos;

    void Update () 
    {
        if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    
        {
            fingerPos =  Input.GetTouch(0).position;        
            Vector3 pos = fingerPos;
            pos.z = 5;
            Vector3 realWorldPos = Camera.main.ScreenToWorldPoint(pos);
            transform.position = realWorldPos;
        }
    }
    

    【讨论】:

    • 我正在使用上面的代码获取触摸输入的真实位置。但是当用户触摸屏幕时,如何从相机位置(0,0,-10)向真实世界位置(x,x,5)射击子弹。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多