移动相机在地形上的统一的3D使用触摸输入 [英] Move Camera Over Terrain Using Touch Input in Unity 3D

查看:152
本文介绍了移动相机在地形上的统一的3D使用触摸输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的团结,我试图找出如何移动相机在使用触摸输入地图/地形。相机会低头看用的旋转(90,0,0)地形。地势上一层8.我有没有问题,得到它与键盘的感动,现在我试图移动触摸,如果你想保持预期使用iOS上是非常不同的。

我能想到的一个内置的iOS应用的最好的例子就是地图,用户将触摸屏幕,并在地图上该点会留在手指下,只要手指停留在屏幕上。所以当用户移动其手指地图似乎与手指移动。我一直没能找到说明如何做这样的例子。我已经看到了移动相机或人物用鼠标五月的例子,但他们似乎并没有很好地转化为这种风格。

也被张贴在Unity3D回答:

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

解决方案

下面应该是你所需要的。请注意,这是棘手的手指/光标和地形之间的1对1的对应使用透视摄像机时。如果你改变你的相机正交,下面的脚本应该给你的手指/光标位置和地图运动之间一个完美的地图。随着视角,你会发现有轻微的偏移。

您也可以做到这一点与光线追踪,但我发现,路由马虎,而不是直观的。

相机设置进行测试(值从检查员拉使应用它们存在):

  1. 您的位置:0,20,0
  2. 定位:90,0,0
  3. 投影:透视/正交

 使用UnityEngine;
System.Collections中使用;

公共类ViewDrag:MonoBehaviour {
Vector3类型hit_position = Vector3.zero;
Vector3类型current_position = Vector3.zero;
Vector3类型camera_position = Vector3.zero;
浮Z = 0.0;

//使用这个初始化
无效的start(){

}

无效更新(){
    如果(Input.GetMouseButtonDown(0)){
        hit_position = Input.mousePosition;
        camera_position = transform.position;

    }
    如果(Input.GetMouseButton(0)){
        current_position = Input.mousePosition;
        LeftMouseDrag();
    }
}

无效LeftMouseDrag(){
    //从Unity3D文档:z位置是在从相机世界单位。在我来说,我使用y轴的高度
    //我的相机对着背下来y轴。您可以忽略此当相机orthograhic。
    current_position.z = hit_position.z = camera_position.y;

    //获取运动的方向。 (注意:不要正常化,变化的幅度将是Vector3.Distance(current_position-hit_position)
    //反正。
    Vector3类型方向= Camera.main.ScreenToWorldPoint(current_position) -  Camera.main.ScreenToWorldPoint(hit_position);

    //反转方向到地形似乎用鼠标移动。
    方向=方向* -1;

    Vector3类型位置= camera_position +方向;

    transform.position =位置;
}
}
 

I am new to Unity and I am trying to figure out how to move the camera over a map/terrain using touch input. The camera would be looking down at the terrain with a rotation of (90,0,0). The terrain is on layer 8. I have had no problem getting it moving with keyboard, now I am trying to move to touch and it is very different if you want to keep expected usage on iOS.

The best example I can think of on a built in iOS app is Maps where the user would touch the screen and that point on the map would stay under the finger as long as the finger stayed on the screen. So as the user moves their finger the map appears to be moving with the finger. I have not been able to find examples that show how to do it this way. I have seen may examples of moving the camera or character with the mouse but they don't seem to translate well to this style.

Also posted on Unity3D Answers:

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

解决方案

Below should be what you need. Note that it's tricky to get a 1 to 1 correspondence between finger/cursor and the terrain when using a "perspective" camera. If you change you camera to orthographic, the script below should give you a perfect map between finger/cursor position and map movement. With "perspective" you'll notice a slight offset.

You could also do this with ray tracing but I've found that route to be sloppy and not as intuitive.

Camera settings for testing (values are pulled from the inspector so apply them there):

  1. Position: 0,20,0
  2. Orientation: 90,0,0
  3. Projection: Perspective/Orthographic


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;
}
}

这篇关于移动相机在地形上的统一的3D使用触摸输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆