- Home /
Question by
connorwforman · Jan 06, 2018 at 05:11 PM ·
scripting problemfreeze
Unity freezes when I try to click play
I recent edited a script from this: public class FPSController : MonoBehaviour {
public float Speed = 5.0f;
public float StaticSpeed = 5.0f;
//StaticSpeed MUST be equal to Speed.
public float SprintSpeed = 8.0f;
public float Sensitivity = 2.0f;
CharacterController player;
public GameObject Eyes;
public GameObject Player;
private bool isSprinting = false;
private bool isMouseLocked = true;
float moveFB;
float moveLR;
float rotX;
float rotY;
void Start () {
player = GetComponent<CharacterController> ();
}
void Update () {
if (Input.GetButtonDown ("Unlock")) {
isMouseLocked = !isMouseLocked;
}
if (isMouseLocked == true) {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
if (isMouseLocked == false) {
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
moveFB = Input.GetAxis ("Vertical") * Speed;
moveLR = Input.GetAxis ("Horizontal") * Speed;
if (Input.GetButtonDown ("Sprint")) {
isSprinting = true;
}
if (Input.GetButtonUp ("Sprint")) {
isSprinting = false;
}
if (isSprinting == true) {
Speed = SprintSpeed;
}
if (isSprinting == false) {
Speed = StaticSpeed;
}
rotY = Input.GetAxis ("Mouse Y") * Sensitivity;
rotX = Input.GetAxis ("Mouse X") * Sensitivity;
Vector3 Movement = new Vector3 (moveLR, 0, moveFB);
Player.transform.Rotate (0, rotX, 0);
Eyes.transform.Rotate (-rotY, 0, 0);
Movement = transform.rotation * Movement;
player.Move (Movement * Time.deltaTime);
}
}
To this:
public class FPSController : MonoBehaviour {
public float Speed = 5.0f;
public float StaticSpeed = 5.0f;
//StaticSpeed MUST be equal to Speed.
public float SprintSpeed = 8.0f;
public float Sensitivity = 2.0f;
public float jumpVelocity = 5.0f;
public float Gravity = 2.0f;
CharacterController player;
public GameObject Eyes;
public GameObject Player;
private bool isSprinting = false;
private bool isMouseLocked = true;
float moveFB;
float moveLR;
float rotX;
float rotY;
void Start () {
player = GetComponent<CharacterController> ();
}
void Update () {
if (Input.GetButtonDown ("Unlock")) {
isMouseLocked = !isMouseLocked;
}
while (player.isGrounded == true) {
if (Input.GetButtonDown ("Jump")) {
Jump ();
}
}
while (player.isGrounded == false) {
transform.Translate (Vector3.down * Gravity * Time.deltaTime);
}
if (isMouseLocked == true) {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
if (isMouseLocked == false) {
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
moveFB = Input.GetAxis ("Vertical") * Speed;
moveLR = Input.GetAxis ("Horizontal") * Speed;
if (Input.GetButtonDown ("Sprint")) {
isSprinting = true;
}
if (Input.GetButtonUp ("Sprint")) {
isSprinting = false;
}
if (isSprinting == true) {
Speed = SprintSpeed;
}
if (isSprinting == false) {
Speed = StaticSpeed;
}
rotY = Input.GetAxis ("Mouse Y") * Sensitivity;
rotX = Input.GetAxis ("Mouse X") * Sensitivity;
Vector3 Movement = new Vector3 (moveLR, 0, moveFB);
Player.transform.Rotate (0, rotX, 0);
Eyes.transform.Rotate (-rotY, 0, 0);
Movement = transform.rotation * Movement;
player.Move (Movement * Time.deltaTime);
}
void Jump () {
transform.Translate (Vector3.up * jumpVelocity * Time.deltaTime);
}
}
And now whenever I try to play with this script, the Unity Editor goes dark like it's going to play, but never does. Why did this happen, and how do I fix it?
Comment
Best Answer
Answer by YoucefB · Jan 06, 2018 at 05:58 PM
You have two infinite while loops which are causing unity to freeze, there's is no need for them you can change them into an if statement.
if (player.isGrounded) {
if (Input.GetButtonDown ("Jump")) {
Jump ();
}
} else {
transform.Translate (Vector3.down * Gravity * Time.deltaTime);
}