using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { [SerializeField] private float speed = 1.0f; void Start() { // Take the current position = new position (0, 0, 0) transform.position = new Vector3(0, 0, 0); } void Update() { CalculateMovement(); } private void CalculateMovement() { float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); transform.Translate(new Vector3(horizontalInput, verticalInput, 0) * speed * Time.deltaTime); // Set boundaries on top and bottom //if (transform.position.y >= 0) //{ // transform.position = new Vector3(transform.position.x, 0, 0); //} //else if (transform.position.y < -3.8f) //{ // transform.position = new Vector3(transform.position.x, -3.8f, 0); //} // Same as the if statement above but cleaner. transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -3.8f, 0), 0); // Pass through on the left and right side if (transform.position.x >= 11.3f) { transform.position = new Vector3(-11.3f, transform.position.y, 0); } else if (transform.position.x < -11.3f) { transform.position = new Vector3(11.3f, transform.position.y, 0); } } }