如何为对象旋转设置2个限制? [英] How to set 2 limits to the rotation of an object?

查看:53
本文介绍了如何为对象旋转设置2个限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当按住D向上和W向下但要限制两个方向的旋转时,我需要使对象在z轴上旋转,使用下面提供的代码,我设法使对象在按下时旋转,但是达到我的变量设置的2个限制中的任何一个时,都不要停止旋转.

I need to make the object rotate in the z axis when holding D to go up and W to go down but limit the rotation in both directions, with the code provided below i managed to make the object rotate when pressed but it does´t stop rotating when hit any of the 2 limits set with my variables.

我是编码领域的新手,希望您能帮助我解决和理解我的问题.谢谢您的宝贵时间.

I am new in the world of coding, hopefully you can help me to solve and understand my problem. Thank you for your time in advance.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GyroDiscControls : MonoBehaviour{

public GameObject AltNeedleBright;
public float MaxAltNeedleRotation = -65f;
public float MinAltNeedleRotation = 135f ;
public void update (){

if (Input.GetAxisRaw("Vertical") > 0 & 
AltNeedleBright.transform.rotation.z > MaxAltNeedleRotation)
    {
        AltNeedleBright.transform.Rotate(0f, 0f, +15f * Time.deltaTime);
    }
if (Input.GetAxisRaw("Vertical") < 0 & 
AltNeedleBright.transform.rotation.z < MinAltNeedleRotation)
    {
        AltNeedleBright.transform.Rotate(0f, 0f, -15f * Time.deltaTime);
    }

}

推荐答案

首先,您要处理

First of all you are dealing with Quaternion. Quaternion has 4 components x, y, z and w and does not behave like you expect it here - never change or check a rotation.z value directly.

第二个错误:默认情况下, transform.Rotate 会在本地空间中进行旋转.因此,无论如何检查 transform.rotation 都是错误的..如果应该是 transform.localRotation .

The second mistake: transform.Rotate by default does the rotation in local Space .. so checking transform.rotation is wrong anywway .. if something it should be transform.localRotation.

然后,您实际要检查的值是 transform.localEulerAngles eulerAngles

Then the value you actually wanted to check is the transform.localEulerAngles or eulerAngles.

一种简单的替代方法是存储已旋转的值并钳制在该值上:

A simple alternative would be to store the already rotated value and clamp on that instead:

// Your naming is very confusing ... Min should be the smaller value
public float MaxAltNeedleRotation = 135f;
public float MinAltNeedleRotation = -65f;

private float alreadyRotated = 0;

public void update ()
{ 
    var direction = 0;

    // use && here .. you don't want to do a bitwise & on bools
    if (Input.GetAxisRaw("Vertical") > 0 && alreadyRotated < MaxAltNeedleRotation)
    {
        direction = 1;
    }
    // use else if since anyway only one of the cases can be true
    else if (Input.GetAxisRaw("Vertical") < 0 && alreadyRotated > MinAltNeedleRotation)
    {
        direction = -1;
    }

    // use the rotation and riection
    // but clamp it so it can minimum be MinAltNeedleRotation - alreadyRotated
    // and maximum MaxAltNeedleRotation - alreadyRotated
    var rotation = Mathf.Clamp(direction * 15f * Time.deltaTime, MinAltNeedleRotation - alreadyRotated, MaxAltNeedleRotation - alreadyRotated);

    if(rotation != 0)
    {
        AltNeedleBright.transform.Rotate(Vector3.forward * rotation);

        // update the alreadyRotated value
        alreadyRotated += rotation;
    }
}

这篇关于如何为对象旋转设置2个限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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