using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { // configuration parameters [SerializeField] float moveSpeed = 10f; [SerializeField] float padding = 1f; [SerializeField] GameObject laserPrefab; [SerializeField] float projectileSpeed = 10f; float xMin; float xMax; float yMin; float yMax; // Start is called before the first frame update void Start() { transform.position = new Vector3(0, -8, 0); SetUpMoveBoundaries(); } // Update is called once per frame void Update() { Move(); Fire(); } private void Fire() { if (Input.GetButtonDown("Fire1")) { GameObject laser = Instantiate ( laserPrefab, transform.position, Quaternion.identity ) as GameObject ; laser.GetComponent().velocity = new Vector2(0, projectileSpeed); } } private void Move() { float deltaX = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed; float deltaY = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed; float newXPos = Mathf.Clamp(transform.position.x + deltaX, xMin, xMax); float newYPos = Mathf.Clamp(transform.position.x + deltaY, yMin, yMax); transform.position = new Vector2(newYPos, newXPos); } private void SetUpMoveBoundaries() { Camera gameCamera = Camera.main; xMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).x + padding; xMax = gameCamera.ViewportToWorldPoint(new Vector3(1, 0, 0)).x - padding; yMin = gameCamera.ViewportToWorldPoint(new Vector3(0, 0, 0)).y + padding; yMax = gameCamera.ViewportToWorldPoint(new Vector3(0, 1, 0)).y - padding; } }