Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

c# - Unity player moves opposite direction

Well I am currenty programming a 2D game in Unity. I have a Player which you can rotate to the left or to the right with the arrow keys. If you press the right arrow key the player should rotate right and if you press left arrow key the player should rotate to the left. Right now if the player press left arrow key it rotates right and if right is pressed it rotates to the left. This is my Code:

private void RotatePlayer()
{  
   float horizontal = Input.GetAxisRaw("Horizontal");
   float rotation = horizontal * speed;
   transform.Rotate(Vector3.forward * rotation);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could just invert the input:

float horizontal = Input.GetAxisRaw("Horizontal") * -1;

Also - have you seen the new input system? https://docs.unity3d.com/Packages/[email protected]/manual/QuickStartGuide.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...