Skip to content
Permalink
0ef37fc948
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
47 lines (36 sloc) 1.18 KB
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, yMin, yMax;
}
public class PlayerController : MonoBehaviour
{
public float speed;
public Boundary boundary;
public GameObject Shot;
public Transform ShotSpawn;
public float fireRate;
private float nextFire;
void Update()
{
if (Input.GetKey(KeyCode.Space) && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(Shot, ShotSpawn.position, ShotSpawn.rotation); //as GameObject;
GetComponent<AudioSource>().Play ();
}
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector2 movement = new Vector2(moveHorizontal, moveVertical);
GetComponent<Rigidbody2D>().velocity = movement * speed;
GetComponent<Rigidbody2D>().position = new Vector2 (
Mathf.Clamp(GetComponent<Rigidbody2D>().position.x, boundary.xMin, boundary.xMax),
Mathf.Clamp(GetComponent<Rigidbody2D>().position.y, boundary.yMin, boundary.yMax)
);
}
}