I am making a top down space shooter, in the vein of games like R-Type. I have the player moving just fine, however I am having trouble keeping them within the bounds of the level. If anyone could give me tips on what I am doing wrong it would be greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour {
float maxSpeed = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// returns a float from -1.0 to 1.0
// move player forward and backward
Vector3 pos = transform.position;
//
pos.y += Input.GetAxis("Vertical") * maxSpeed * Time.deltaTime;
transform.position = pos;
// move player left and right
pos.x += Input.GetAxis("Horizontal") * maxSpeed * Time.deltaTime;
transform.position = pos;
// RESTRICT player to camer boundries
if(pos.y >= Camera.main.orthographicSize)
{
pos.y = Camera.main.orthographicSize;
}
}
}