Hi all. So I’m very confused on why this isn’t working. I have created some integers which I want to use to rotate a cube in my game. The three integers called ‘rotx’, ‘roty’, and ‘rotz’ are meant to be used to tell the object what the rotation should be. I have tried using the lines of code ‘this.transform.eulerAngles = new Vector3(rotx, roty, rotz);’ and ‘this.transform.rotation = Quaternion.Euler(rotx, roty, rotz);’, but neither work the way I want them to. What I want to happen is for example if the value of rotx was 90 and the other two were 0, then the gameobject should rotate to (90, 0, 0). I have even made the integers public, and I can indeed see when in play mode that the integers do equal what they are meant to, but the rotation coordinates of the object don’t. What’s even weirder is that the y coordinate even goes to -180 sometimes, and ‘roty’ never ever changes from 0 in my script.
Here’s my script incase I’ve made any error somewhere else in my script but I can’t spot any errors. Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerMovement : MonoBehaviour {
public Camera cameraScript;
private float pos;
public float rotx;
public float roty;
public float rotz;
public float desrotx;
public float desrotz;
public static bool playerMoved;
public static bool canMove;
public static int moves;
public Text movesText;
private void Start()
{
moves = 0;
playerMoved = false;
canMove = true;
pos = 0;
rotx = 0;
roty = 0;
rotz = 0;
desrotx = 0;
desrotz = 0;
}
private void Update()
{
this.transform.rotation = Quaternion.Euler(rotx, roty, rotz);
//this.transform.eulerAngles = new Vector3(rotx, roty, rotz);
if (movesText != null)
{
movesText.text = moves.ToString();
}
}
public void LeftButton()
{
if (GameMaster.endTheLevel == false && canMove == true)
{
moves = moves + 1;
playerMoved = true;
desrotx = rotx - 90;
StartCoroutine(MoveLeft());
StartCoroutine(RotateLeft());
}
}
public void RightButton()
{
if (GameMaster.endTheLevel == false && canMove == true)
{
moves = moves + 1;
playerMoved = true;
desrotx = rotx + 90;
StartCoroutine(MoveRight());
StartCoroutine(RotateRight());
}
}
public void UpButton()
{
if (GameMaster.endTheLevel == false && canMove == true)
{
moves = moves + 1;
playerMoved = true;
desrotz = rotz + 90;
StartCoroutine(MoveUp());
StartCoroutine(RotateUp());
}
}
public void DownButton()
{
if (GameMaster.endTheLevel == false && canMove == true)
{
moves = moves + 1;
playerMoved = true;
desrotz = rotz - 90;
StartCoroutine(MoveDown());
StartCoroutine(RotateDown());
}
}
IEnumerator MoveLeft()
{
Camera.enableArrows = false;
cameraScript.greenArrow.interactable = false;
cameraScript.blueArrow.interactable = false;
cameraScript.yellowArrow.interactable = false;
cameraScript.redArrow.interactable = false;
pos = this.transform.position.z - 0.4f;
while (this.transform.position.z > pos)
{
this.transform.position = this.transform.position + new Vector3(0f, 0f, -0.06f);
yield return new WaitForSeconds(0.005f);
}
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, pos);
pos = 0;
yield return new WaitForSeconds(0.05f);
Camera.enableArrows = true;
cameraScript.greenArrow.interactable = true;
cameraScript.blueArrow.interactable = true;
cameraScript.yellowArrow.interactable = true;
cameraScript.redArrow.interactable = true;
}
IEnumerator RotateLeft()
{
while(rotx > desrotx)
{
rotx = rotx - 8;
yield return new WaitForSeconds(0.005f);
}
rotx = desrotx;
if (desrotx <= -360)
{
desrotx = 0;
rotx = 0;
}
}
IEnumerator MoveUp()
{
Camera.enableArrows = false;
cameraScript.greenArrow.interactable = false;
cameraScript.blueArrow.interactable = false;
cameraScript.yellowArrow.interactable = false;
cameraScript.redArrow.interactable = false;
pos = this.transform.position.x - 0.4f;
while (this.transform.position.x > pos)
{
this.transform.position = this.transform.position + new Vector3(-0.06f, 0f, 0f);
yield return new WaitForSeconds(0.005f);
}
this.transform.position = new Vector3(pos, this.transform.position.y, this.transform.position.z);
pos = 0;
yield return new WaitForSeconds(0.05f);
Camera.enableArrows = true;
cameraScript.greenArrow.interactable = true;
cameraScript.blueArrow.interactable = true;
cameraScript.yellowArrow.interactable = true;
cameraScript.redArrow.interactable = true;
}
IEnumerator RotateUp()
{
while (rotz < desrotz)
{
rotz = rotz + 8;
yield return new WaitForSeconds(0.005f);
}
rotz = desrotz;
if (desrotz >= 360)
{
desrotz = 0;
rotz = 0;
}
}
IEnumerator MoveRight()
{
Camera.enableArrows = false;
cameraScript.greenArrow.interactable = false;
cameraScript.blueArrow.interactable = false;
cameraScript.yellowArrow.interactable = false;
cameraScript.redArrow.interactable = false;
pos = this.transform.position.z + 0.4f;
while (this.transform.position.z < pos)
{
this.transform.position = this.transform.position + new Vector3(0f, 0f, 0.06f);
yield return new WaitForSeconds(0.005f);
}
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, pos);
pos = 0;
yield return new WaitForSeconds(0.05f);
Camera.enableArrows = true;
cameraScript.greenArrow.interactable = true;
cameraScript.blueArrow.interactable = true;
cameraScript.yellowArrow.interactable = true;
cameraScript.redArrow.interactable = true;
}
IEnumerator RotateRight()
{
while (rotx < desrotx)
{
rotx = rotx + 8;
yield return new WaitForSeconds(0.005f);
}
rotx = desrotx;
if (desrotx >= 360)
{
desrotx = 0;
rotx = 0;
}
}
IEnumerator MoveDown()
{
Camera.enableArrows = false;
cameraScript.greenArrow.interactable = false;
cameraScript.blueArrow.interactable = false;
cameraScript.yellowArrow.interactable = false;
cameraScript.redArrow.interactable = false;
pos = this.transform.position.x + 0.4f;
while (this.transform.position.x < pos)
{
this.transform.position = this.transform.position + new Vector3(0.06f, 0f, 0f);
yield return new WaitForSeconds(0.005f);
}
this.transform.position = new Vector3(pos, this.transform.position.y, this.transform.position.z);
pos = 0;
yield return new WaitForSeconds(0.05f);
Camera.enableArrows = true;
cameraScript.greenArrow.interactable = true;
cameraScript.blueArrow.interactable = true;
cameraScript.yellowArrow.interactable = true;
cameraScript.redArrow.interactable = true;
}
IEnumerator RotateDown()
{
rotz = desrotz + 90;
while (rotz > desrotz)
{
rotz = rotz - 8;
yield return new WaitForSeconds(0.005f);
}
rotz = desrotz;
if (desrotz <= -360)
{
desrotz = 0;
rotz = 0;
}
}
}