我试图在Unity中创建一个基本形状,在键盘输入的某些约束内转动。但是,当我试图从最左侧转向右侧时。根据我所有的理解,它似乎是由于函数没有返回true而被调用的输入似乎没有被调用。
https://i.stack.imgur.com/5Aldd.png
任何人都知道为什么这不是真的吗?如果需要,这是脚本的其余部分:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControlScript : MonoBehaviour {
public Rigidbody shipRig;
public int rotationSpeed = 1;
public int retRotationSpeed = 1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Rigidbody tempShipRig = GetComponent<Rigidbody>();
Vector3 shipEuler = shipRig.rotation.eulerAngles;
//up input
if (Input.GetKey(KeyCode.W) && CheckBounds(shipEuler.x, 0, 25) ||
Input.GetKey(KeyCode.W) && CheckBounds(shipEuler.x, 335, 360))
{
shipEuler.x = shipEuler.x + (-rotationSpeed * Time.deltaTime);
}
//down input
else if (Input.GetKey(KeyCode.S) && CheckBounds(shipEuler.x, 0, 25) ||
Input.GetKey(KeyCode.S) && CheckBounds(shipEuler.x, 335, 360))
{
shipEuler.x = shipEuler.x + (rotationSpeed * Time.deltaTime);
}
else if(shipEuler.x !=0) //idle return to center
{
if(shipEuler.x < 180)
{
shipEuler.x = shipEuler.x - (retRotationSpeed * Time.deltaTime);
if(shipEuler.x < 0)
{
shipEuler.x = 0;
}
}
if (shipEuler.x > 180)
{
shipEuler.x = shipEuler.x + (retRotationSpeed * Time.deltaTime);
if (shipEuler.x > 360)
{
shipEuler.x = 0;
}
}
}
//left input
if (Input.GetKey(KeyCode.A) && CheckBounds(shipEuler.z, 0, 45) ||
Input.GetKey(KeyCode.A) && CheckBounds(shipEuler.z, 315, 360))
{
shipEuler.z = shipEuler.z + (rotationSpeed * Time.deltaTime);
}
//right input
else if (Input.GetKey(KeyCode.D) && CheckBounds(shipEuler.z, 0, 45) ||
Input.GetKey(KeyCode.D) && CheckBounds(shipEuler.z, 315, 360))
{
shipEuler.z = shipEuler.z + (-rotationSpeed * Time.deltaTime);
}
if (shipEuler.x > 25 && shipEuler.x < 335) //fixes the rotation getting stuck when they pass over constraints
{
if(shipEuler.x < 180)
{
shipEuler.x = 25;
}
if(shipEuler.x > 180)
{
shipEuler.x = 335;
}
}
if(shipEuler.z > 45 && shipEuler.z < 315)
{
if(shipEuler.z < 180)
{
shipEuler.z = 45;
}
if(shipEuler.z > 180)
{
shipEuler.z = 315;
}
}
shipRig.rotation = Quaternion.Euler(shipEuler);
shipRig = tempShipRig;
Debug.Log(shipEuler);
}
bool CheckBounds (float orNr, float min, float max) //returns true if numbers are within constraints
{
return (orNr <= max && orNr >= min);
}
}