我可以使用更大的鼠标输入在轴上旋转立方体,但是不知道如何阻止短轴的输入直到鼠标移开。
脚本包含最近90度角的捕捉。不是那么优雅,但我会把它留给另一个帖子。
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class s_rotate_test : MonoBehaviour {
float mouseX;
float mouseY;
public float rotationSpeed = 100f;
public float smoothing = 2f;
// Use this for initialization
void Start () {
Debug.Log (transform.rotation.x + " | " + transform.rotation.y + " | " + transform.rotation.z);
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButton(0)) {
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");
float rotationY = -mouseX * rotationSpeed * Time.deltaTime;
float rotationX = mouseY * rotationSpeed * Time.deltaTime;
if (mouseX * mouseX > mouseY * mouseY) {
transform.Rotate (0, rotationY, 0 /*, Space.World*/);
} else if (mouseY * mouseY > mouseX * mouseX) {
transform.Rotate (rotationX, 0, 0/*, Space.World*/);
}
}
if (Input.GetMouseButtonUp (0)) {
// Y AXIS SNAPS -------------------------------------------------------------------------------------------
if (transform.rotation.eulerAngles.y > 0 && transform.rotation.eulerAngles.y <= 45) {
Quaternion targetRotation = Quaternion.Euler (0, 0, 0);
transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, smoothing);
}
}
}
//------------------------------------------------------------------------
}
答案 0 :(得分:0)
您需要一些东西来跟踪当前的拖动状态。
public enum State{
Normal,
DraggingX,
DraggingY
}
// This is a field
State draggingState = State.Normal;
使用此设置,仅在状态为Normal
时才检查最大输入。
确定正确的轴后,将状态设置为DraggingX
(或Y)。
按下鼠标时,执行以下操作:
switch(dragginState){
case State.Normal:
// Get Greatest input (suppose it's X)
draggingState = State.DraggingX;
break;
case State.DraggingX:
// Use only X input
break;
case State.DraggingY:
// Use only Y input
break;
}
发布后,您再次设置draggingState = State.Normal;
。