You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.3 KiB
87 lines
2.3 KiB
1 year ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
|
||
|
public class Player : MonoBehaviour
|
||
|
{
|
||
|
[SerializeField]
|
||
|
private float speed = 1.0f;
|
||
1 year ago
|
[SerializeField]
|
||
|
private GameObject laserPrefab;
|
||
|
[SerializeField]
|
||
|
private float fireRate = 0.15f;
|
||
|
private float nextFire = -1f;
|
||
1 year ago
|
[SerializeField]
|
||
|
private int lives = 3;
|
||
|
|
||
|
private SpawnManager spawnManager;
|
||
1 year ago
|
|
||
|
void Start()
|
||
|
{
|
||
|
transform.position = new Vector3(0, 0, 0);
|
||
1 year ago
|
|
||
|
spawnManager = GameObject.Find("SpawnManager").GetComponent<SpawnManager>();
|
||
|
if(spawnManager == null )
|
||
|
{
|
||
|
Debug.LogError("Spawn Manager is null");
|
||
|
}
|
||
1 year ago
|
}
|
||
|
|
||
|
void Update()
|
||
|
{
|
||
|
CalculateMovement();
|
||
1 year ago
|
if (Input.GetButton("Fire1") && Time.time > nextFire)
|
||
|
{
|
||
|
ShootLaser();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void ShootLaser()
|
||
|
{
|
||
|
nextFire = Time.time + fireRate;
|
||
1 year ago
|
Instantiate(laserPrefab, transform.position + new Vector3(0, 1.05f, 0), Quaternion.identity);
|
||
1 year ago
|
}
|
||
|
|
||
|
private void CalculateMovement()
|
||
|
{
|
||
|
float horizontalInput = Input.GetAxis("Horizontal");
|
||
|
float verticalInput = Input.GetAxis("Vertical");
|
||
|
|
||
|
transform.Translate(new Vector3(horizontalInput, verticalInput, 0) * speed * Time.deltaTime);
|
||
|
|
||
1 year ago
|
/* 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);
|
||
|
}
|
||
|
*/
|
||
|
|
||
1 year ago
|
// 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);
|
||
|
}
|
||
|
}
|
||
1 year ago
|
|
||
|
public void Damage()
|
||
|
{
|
||
|
lives--;
|
||
|
if (lives < 1)
|
||
|
{
|
||
|
spawnManager.OnPlayerDeath();
|
||
|
Destroy(this.gameObject);
|
||
|
}
|
||
|
}
|
||
1 year ago
|
}
|