滑动屏幕以旋转相机 [英] Swipe screen to rotate camera

查看:121
本文介绍了滑动屏幕以旋转相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Unity3D为智能手机编写3D游戏,却不知道如何旋转相机(第一人称视角).

I am writing a 3D game for smartphone using Unity3D and have no idea on making camera rotation (First Person Perspective).

制作虚拟游戏杆太难了,所以我决定在屏幕滑动时旋转相机.

Making virtual joystick is too hard, so I decided to make camera rotate on screen swipe.

用户滑动智能手机的屏幕,然后相机转动.

User swipe smartphone's screen and camera turns around.

旋转有效,但始终从同一位置开始.

Rotating works, but always starts from the same position.

保存最后一个位置会中断所有旋转(旋转是歪斜的).

Saving last position breaks all rotating (rotating is askew).

我的代码:

using UnityEngine;
using System.Collections;

public class Look : MonoBehaviour {

public float sensitivityX = 1F;
public float sensitivityY = 1F;

public float minimumX = -360F;
public float maximumX = 360F;

public float minimumY = -25F;
public float maximumY = 25F;

float rotationX = 0F;
float rotationY = 0F;

float oldRotationX = 0F;
float oldRotationY = 0F;

float lastX = 0F;
float lastY = 0F;

Quaternion originalRotation;

void Update ()
{
    if (Input.touches.Length > 0)
    {
        if(Input.touches[0].phase == TouchPhase.Began)
        {
            lastX = Input.touches[0].position.x;
            lastY = Input.touches[0].position.y;

            rotationX = transform.localEulerAngles.x;
            rotationY = transform.localEulerAngles.y;

            oldRotationX = rotationX;
            oldRotationY = rotationY;
        }

        if(Input.touches[0].phase == TouchPhase.Moved)
        {
            rotationX = (oldRotationX + (Input.touches[0].position.x - lastX)) * sensitivityX;
            rotationY = (oldRotationY + (Input.touches[0].position.y - lastY)) * sensitivityY;

            rotationX = ClampAngle (rotationX, minimumX, maximumX);
            rotationY = ClampAngle (rotationY, minimumY, maximumY);

            Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
            Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, -Vector3.right);

            transform.localRotation = originalRotation * xQuaternion * yQuaternion;
        }

        //this should make rotating from last position, but it
        //makes rotating incorrect (askew)
        //without code below rotating works, but on always
        //starts from the same position
        if(Input.touches[0].phase == TouchPhase.Ended)
        {
            originalRotation = transform.localRotation;
        }
    }
}

void Start ()
{
    if (rigidbody)
        rigidbody.freezeRotation = true;

    originalRotation = transform.localRotation;
}

public static float ClampAngle (float angle, float min, float max)
{
    if (angle < -360F)
        angle += 360F;
    if (angle > 360F)
        angle -= 360F;
    return Mathf.Clamp (angle, min, max);
}
}

请帮助我.谢谢.

推荐答案

您可以使用输入,特别是触摸"位置.然后,您可以应用在鼠标外观脚本中获得的类似效果. .

You can use the inputs, specifically the Touch positions. You can then apply a similar effect achieved in a mouse look script.

基本上,您想要的代码是:

Essentially the code you want is:

rotationX += Input.touches[0].position.x * sensitivityX;
rotationY += Input.touches[0].position.y * sensitivityY

Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, -Vector3.right);

transform.localRotation *= xQuaternion * yQuaternion;

这篇关于滑动屏幕以旋转相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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