- Home /
transform.Rotate will not work
{ public float moveSpeed; public float rotSpeed; public Animator anim;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.W))
{
gameObject.transform.Translate(-moveSpeed * Time.deltaTime, 0, 0);
anim.SetTrigger("WalkForward");
}
if (!Input.GetKey(KeyCode.W))
{
gameObject.transform.Translate(0, 0, 0);
anim.SetTrigger("Stop");
}
if (Input.GetKey(KeyCode.A))
{
//gameObject.transform.Rotate(Vector3.up, -rotSpeed * Time.deltaTime);
gameObject.transform.Rotate(0, 0, rotSpeed * Time.deltaTime);
print("turn");
}
if (!Input.GetKey(KeyCode.A))
{
gameObject.transform.Rotate(0, 0, 0);
}
}
} I have use Transform.Rotate countless times in the past and for some reason here it will not work. I have tried going through forums and videos making a dozen tiny tweaks and none of them work. So what am I doing wrong?
Answer by Epsilon_Delta · May 30, 2019 at 09:11 PM
Code looks fine, btw "gameObject" in front of .transform is redundant and so are these two parts:
if (!Input.GetKey(KeyCode.W))
{
gameObject.transform.Translate(0, 0, 0);
}
if (!Input.GetKey(KeyCode.A))
{
gameObject.transform.Rotate(0, 0, 0);
}
You do not need to tell your gameobject not to rotate or translate.
And to help you with your problem, I would check these things (I assume that print("turn") works): isn't rotSpeed set to 0 (e.g. in the inspector)? Isn't Time.timeScale set to 0? Is this script attached to the correct gameObject?
@g_raver got rid of the gameObject part (didn't have it their originally, just something I added in an attempt to make it work) turn prints, got rid of time.deltatime, rotspeed is greater than 0, yes it is on the correct gameObject