Skip to content

Instantly share code, notes, and snippets.

@curious-username
Last active September 7, 2021 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curious-username/1db039c9aad1f48121ea5aa10bd28fd9 to your computer and use it in GitHub Desktop.
Save curious-username/1db039c9aad1f48121ea5aa10bd28fd9 to your computer and use it in GitHub Desktop.
moving all player movement into a single method
void Update()
{
PlayerMovement();
}
void PlayerMovement()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
transform.Translate(Vector3.right * horizontalInput * _speed * Time.deltaTime);
transform.Translate(Vector3.up * verticalInput * _speed * Time.deltaTime);
if (transform.position.y >= 5.7f)
{
transform.position = new Vector3(transform.position.x, 5.7f, 0);
}
else if (transform.position.y <= -5.7f)
{
transform.position = new Vector3(transform.position.x, -5.7f, 0);
}
if (transform.position.x >= 11.35f)
{
transform.position = new Vector3(-11.35f, transform.position.y, 0);
}
else if (transform.position.x <= -11.35f)
{
transform.position = new Vector3(11.35f, transform.position.y, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment