# Control different speeds for moving left/right, and facing that direction

I have a game idea... and I need a script where I can control the speed for moving left, and control another speed for moving right. But, I also need to let my player face that direction.

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
void Update()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
Vector3 position = this.transform.position;
position.x -= .5f;
this.transform.position = position;
}

if (Input.GetKey(KeyCode.RightArrow))
{
Vector3 position = this.transform.position;
position.x += 1f;
this.transform.position = position;
}

if (Input.GetKey(KeyCode.A))
{
Vector3 position = this.transform.position;
position.x -= .5f;
this.transform.position = position;
}

if (Input.GetKey(KeyCode.D))
{
Vector3 position = this.transform.position;
position.x += 1f;
this.transform.position = position;
}
}
}


That is my code. I need make it better I guess... and to make the player face that direction.

• Define better. Do you want to have your code refactored? Do you want to have better conditions? Do you want to use the new input system? Does your game uses physics or is expected to collide with things? Did you check the documentation regarding flip property of sprites for your facing direction problem? Feb 11 at 21:05
• look i am just a beginner i want a code that is good for 2d platform that have the condition that i mentioned. thank you and yes my game have physics Feb 11 at 21:08
• You might be better to start with a youtube tutorial about 2D platformers, try to reimplement it and ask a specific question if you encounter an error. As a starter, moving via transform.position will ignore physics/ collisions, you would want to move around with your rigidbody Feb 11 at 21:12
• just forget the code that i sent, can you give me advices and help me to write a good code, with physics 2D. if you dont want waste your time with me thats definitly not a problem Feb 11 at 21:18
• This is not a code request stackexchange but aimed to help people with a specific problem they encounter while doing their game. You could check unity tutorials offered by unity itself for a starting 2D game that will walk you step by step. once you understand how everything works together, you can bring your own ideas into it. And if you have problems implementing them, you can ask here a question with the code you wrote, whats the error is and what you would expect it to do. learn.unity.com Feb 11 at 21:24