【问题标题】:Main camera rotation by swipe滑动主摄像机旋转
【发布时间】:2019-05-19 16:48:08
【问题描述】:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour
{

    public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    public RotationAxes axes = RotationAxes.MouseXAndY;
    public float sensitivityX = 15F;
    public float sensitivityY = 15F;

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

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

    float rotationY = 0F;

    void Update()
    {
        if (axes == RotationAxes.MouseXAndY)
        {
            float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
            rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

            transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
        }
        else if (axes == RotationAxes.MouseX)
        {
            transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
        }
        else
        {
            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
            rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

            transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
        }
    }

    void Start()
    {
        //if(!networkView.isMine)
        //enabled = false;

        // Make the rigid body not change rotation
        //if (rigidbody)
        //rigidbody.freezeRotation = true;
    }
}

我使用一个脚本来用鼠标旋转相机,我想将它转换为 touch 。我希望我的相机在我触摸屏幕时旋转。游戏有一个第一玩家控制器。请帮我把它做成触摸屏

【问题讨论】:

    标签: c# unity3d touch


    【解决方案1】:

    获取第一个Touch,然后使用touch.deltaPosition 查找自上次更新以来触摸移动了多少。然后,您可以根据屏幕大小和灵敏度进行缩放。

    void Update()
    {
        if(Input.touchCount > 0) {
            Touch touch = Input.GetTouch(0);
            float turnAngleChange = (touch.deltaPosition.x / Screen.width) * sensitivityX; 
            float pitchAngleChange = (touch.deltaPosition.y / Screen.height) * sensitivityY;
    
            // Handle any pitch rotation
            if (axes == RotationAxes.MouseXAndY || axes == RotationAxes.MouseY) {
                rotationY = Mathf.Clamp(rotationY+pitchAngleChange, minimumY, maximumY);
                transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0f));
            }
    
            // Handle any turn rotation
            if (axes == RotationAxes.MouseXAndY || axes == RotationAxes.MouseX) {
                transform.Rotate(0f, turnAngleChange , 0f);
            }
        }
    }
    

    作为旁注,将俯仰旋转称为rotationY 可能会令人困惑,即使它们实际上是沿x 轴的旋转,反之亦然。

    【讨论】:

    • 我有一个用于移动的操纵杆,它会影响前进和后退
    猜你喜欢
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    相关资源
    最近更新 更多