c# - 在 Unity 3D 中使用触摸输入在地形上移动相机

标签 c# ios 3d touch unity3d

我是 Unity 的新手,我正在尝试弄清楚如何使用触摸输入在 map /地形上移动相机。相机将以 (90,0,0) 的旋转角度俯视地形。地形位于第 8 层。我用键盘移动它没有问题,现在我试图移动到触摸,如果你想在 iOS 上保持预期的使用,这是非常不同的。

我能想到的关于内置 iOS 应用程序的最佳示例是 map ,用户可以触摸屏幕,只要手指停留在屏幕上, map 上的那个点就会停留在手指下方。因此,当用户移动他们的手指时, map 似乎会随着手指移动。我一直无法找到说明如何以这种方式进行操作的示例。我见过很多用鼠标移动相机或角色的例子,但它们似乎不能很好地转化为这种风格。

也发布在 Unity3D 上回答:

http://answers.unity3d.com/questions/283159/move-camera-over-terrain-using-touch-input.html

最佳答案

下面应该是你需要的。请注意,在使用透视相机时,很难在手指/光标和地形之间获得 1 对 1 的对应关系。如果您将相机更改为正交,下面的脚本应该会为您提供手指/光标位置和 map 移动之间的完美映射。通过透视,您会注意到轻微的偏移。

您也可以使用光线追踪来做到这一点,但我发现这条路线很草率而且不那么直观。

用于测试的相机设置(值是从检查器中提取的,因此将它们应用到那里):

  1. 位置:0,20,0
  2. 方向:90,0,0
  3. 投影:透视/正交

using UnityEngine;
using System.Collections;



public class ViewDrag : MonoBehaviour {
    Vector3 hit_position = Vector3.zero;
    Vector3 current_position = Vector3.zero;
    Vector3 camera_position = Vector3.zero;
    float z = 0.0f;
    
    // Use this for initialization
    void Start () {
        
    }
    
    void Update(){
        if(Input.GetMouseButtonDown(0)){
            hit_position = Input.mousePosition;
            camera_position = transform.position;
            
        }
        if(Input.GetMouseButton(0)){
            current_position = Input.mousePosition;
            LeftMouseDrag();        
        }
    }
    
    void LeftMouseDrag(){
        // From the Unity3D docs: "The z position is in world units from the camera."  In my case I'm using the y-axis as height
        // with my camera facing back down the y-axis.  You can ignore this when the camera is orthograhic.
        current_position.z = hit_position.z = camera_position.y;
        
        // Get direction of movement.  (Note: Don't normalize, the magnitude of change is going to be Vector3.Distance(current_position-hit_position)
        // anyways.  
        Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);
        
        // Invert direction to that terrain appears to move with the mouse.
        direction = direction * -1;
        
        Vector3 position = camera_position + direction;
        
        transform.position = position;
    }
}

关于c# - 在 Unity 3D 中使用触摸输入在地形上移动相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11497085/

相关文章:

c# - 如何在 app.config 中添加换行符?

c# - 对我的应用程序中的线程安全性有何建议?

c# - 使用 Razor/MVC3 将 AssemblyVersion 放入网页时出现问题

ios - 时间间隔中的 NAN

3d - 在 gnuplot 中在 pm3d 表面绘制点

opengl - 在 OpenGL 中,有没有办法获取着色器程序使用的所有制服和属性的列表?

c# - 比较两个日期时间,一个来自 Datetimepicker,一个来自 MySql 数据库

找不到 iOS MessageUI.h

javascript - 行进方 block 表演(2天)

ios - 按两个属性排序