I want to rotate a cube left and right with that:
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
private Vector3 offset;
public GameObject player;
public GameObject center;
public GameObject Left;
public GameObject Right;
public int step = 9;
bool input = true;
public float speed = 0.01f;
void Update()
{
if(input == true)
{
if (Input.GetKey("a"))
{
StartCoroutine("moveLeft");
input = false;
} }
if (input == true)
{
if (Input.GetKey("d"))
{
StartCoroutine("moveRight");
input = false;
}
}
}
IEnumerator moveLeft()
{
for(int i = 0; i < (90/step); i++)
{
player.transform.RotateAround(Left.transform.position, Vector3.forward, step);
yield return new WaitForSeconds(speed);
}
center.transform.position = player.transform.position;
input = true;
}
IEnumerator moveRight()
{
for (int i = 0; i < (90 / step); i++)
{
player.transform.RotateAround(Right.transform.position, Vector3.back, step);
yield return new WaitForSeconds(speed);
}
center.transform.position = player.transform.position;
input = true;
}
}
and the problem is when I press a and d cube move left or right but + 0.5 up.
I also have tried this update method, but now it turns only on the y rotation axis:
public float rotationSpeed = 10f;
public GameObject player;
void Update()
{
if(Input.GetAxis("Horizontal") < 0)
{
player.transform.Rotate(
0,
Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime,
0);
}
if (Input.GetAxis("Horizontal") > 0)
{
player.transform.Rotate(
0,
Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime,
0);
}
}