Hi, I have a problem while running a script in Unity. I have three scripts (for controlling spaceship), one for normal movement, one for ‘Acrobat Mode’ and one for switching between them. When I run the normal movement, all is OK, but when I switch to ‘Acrobat Mode’ and try to move (Left or Right Shift and ‘W’), Unity freezes.
Here’s the code:
using UnityEngine;
using System.Collections;
public class AcrobatMode : MonoBehaviour
{
public float rollSpeed = 15f;
public float backSpinSpeed = 13f;
public float frontSpinSpeed = 13f;
void Update ()
{
if (transform.gameObject.GetComponent<GlobalModeController>().acrobatMode == true)
{
// Back spin
if ((Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) && Input.GetKey (KeyCode.W))
{
while ((Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) && Input.GetKey (KeyCode.W))
{
transform.Rotate (backSpinSpeed, 0f, 0f);
transform.Translate (Vector3.forward * backSpinSpeed * Time.deltaTime);
}
}
// Front spin
if ((Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) && Input.GetKey (KeyCode.S))
{
while ((Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) && Input.GetKey (KeyCode.S))
{
transform.Rotate (frontSpinSpeed, 0f, 0f);
transform.Translate (Vector3.forward * frontSpinSpeed * Time.deltaTime);
}
}
// Right roll
if ((Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) && Input.GetKey (KeyCode.D))
{
while ((Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) && Input.GetKey (KeyCode.D))
{
transform.Rotate (0f, rollSpeed, 0f);
if (Input.GetKey (KeyCode.W))
{
transform.Translate (Vector3.forward * rollSpeed * Time.deltaTime);
}
}
}
// Left roll
if ((Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) && Input.GetKey (KeyCode.A))
{
while ((Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) && Input.GetKey (KeyCode.A))
{
transform.Rotate (0f, rollSpeed, 0f);
if (Input.GetKey (KeyCode.W))
{
transform.Translate (Vector3.forward * rollSpeed * Time.deltaTime);
}
}
}
}
}
}
Please help I am out of ideas what is wrong…