# Character automatic movement and rotation

I have problems with a character. I want to move myself at some speed, and by typing on WASD keys to rotate this object and move to the side where the face is. The object moves continuously without pressing the key. Let's say something to the snake game.This is my code, it goes, but I can not do this rotation.

private float speed = 0.01f;
private float moveSpeed = 12f;
Vector2 vector = Vector2.up;
Vector2 moveVector;
[HideInInspector] public bool IsMoving = false;
[HideInInspector] public bool canPress = true;
private void Start()
{
InvokeRepeating("Movement", 0.5f, speed);
}
void Update()
{
if (canPress)
{
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
canPress = false;
IsMoving = true;
vector = Vector2.right;
//transform.Rotate(0f, 0f, -90f);
transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y + rotateObj, transform.rotation.z);
}
else if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
canPress = false;
IsMoving = true;
vector = Vector2.up;
}
else if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
{
canPress = false;
IsMoving = true;
vector = -Vector2.up;
//transform.Rotate(0f, 0f, -90f);
}
else if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
canPress = false;
IsMoving = true;
vector = -Vector2.right;
//transform.Rotate(0f, 0f, +90f);
}
}
moveVector = vector / moveSpeed;
}
void Movement()
{
if (IsMoving)
transform.Translate(moveVector);
}


If anyone knows how to do this I would be grateful. !!!

public GameObject shipSprite;
private float speed = 0.01f;
private float moveSpeed = 12f;
Vector2 vector = Vector2.up;
Vector2 moveVector;
[HideInInspector] public bool IsMoving = false;
[HideInInspector] public bool canPress = true;
private void Start()
{
InvokeRepeating("Movement", 0.5f, speed);

}
void Update()
{
if (canPress)
{
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
canPress = false;
IsMoving = true;
vector = Vector2.right;
shipSprite.transform.Rotate(0f, 0f, -90f);
}
else if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
canPress = false;
IsMoving = true;
vector = Vector2.up;
shipSprite.transform.rotation = Quaternion.identity;
}
else if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
{
canPress = false;
IsMoving = true;
vector = -Vector2.up;
shipSprite.transform.rotation = Quaternion.identity;
}
else if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
canPress = false;
IsMoving = true;
vector = -Vector2.right;
shipSprite.transform.Rotate(0f, 0f, 90f);
}
}
moveVector = vector / moveSpeed;
}
void Movement()
{
if (IsMoving)
transform.Translate(moveVector);
}

• What did we do? ... we created a new object, and we put the ship into it, which contains just the sprite. In principle it works, but again with some disturbances.So I do not know what the problem is ... :/ – Gheorghe Ciusca Dec 5 '17 at 12:59

To Rotate your character at some specific point use this line first declare public float variable rotateObj = 90; and assign value.

 transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y + rotateObj, transform.rotation.z);


To move your Character use this line public float speed;

private Rigidbody rb;

void Start ()
{
rb = GetComponent<Rigidbody>();
}

Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);