Permalink
Cannot retrieve contributors at this time
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?
DMD2542-SpaceShooterPart2-AndrewSides/Space Shooter/Assets/script/PlayerController.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
47 lines (36 sloc)
1.18 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
); | |
} | |
} |