- Home /
Question by
AlaBeast · Jan 01, 2019 at 06:18 AM ·
2dtransform.translate
Transform.Translate moving object on Y axis for no reason,Trying to move object on x axis with Transform. Translate
I'm making a small game in Unity with C# trying to learn the basics of C# where you rotate an object which then moves it side to side while you dodge objects. I'm trying to make it so the speed is proportional to the rotation amount but for some reason it is also translating the object on the y axis.
Here is my code:
void Update()
{
currentRotation = this.transform.rotation.eulerAngles.z;
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(0, 0, turnSpeed * move * Time.deltaTime);
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(0 ,0 , -turnSpeed * move * Time.deltaTime);
}
if (currentRotation >= 131 && currentRotation <= 225)
{
transform.Rotate(0, 0, 0.1f);
move = 0;
}
else if (currentRotation >= 45 && currentRotation <= 131)
{
transform.Rotate(0, 0, -0.1f);
move = 0;
}
else
{
move = 1;
// string angle = System.Convert.ToString(this.transform.localRotation.z);
string angle = System.Convert.ToString(currentRotation);
//Debug.Log(angle);
}
if (currentRotation < 360 && currentRotation > 220)
{
netRotation = -1*(currentRotation - 315f);
}
else
{
netRotation = -1*(currentRotation + 45);
}
Debug.Log(netRotation);
transform.Translate(Vector2.right* netRotation * Time.deltaTime * 0.01f);
}
}
Comment
Best Answer
Answer by sean244 · Jan 01, 2019 at 07:18 AM
Rewrite
transform.Translate(Vector2.right* netRotation * Time.deltaTime * 0.01f);
To
transform.Translate(Vector2.right * netRotation * Time.deltaTime * 0.01f, Space.World);
Your answer