Skip to content

Instantly share code, notes, and snippets.

@ZeroCool5254

ZeroCool5254/Player.cs

Last active Mar 19, 2021
Embed
What would you like to do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField]
private float _speed = 5f;
[SerializeField]
private GameObject _laserPrefab;
[SerializeField]
private float _fireRate = 0.5f;
private float _canFire = -1f;
void Start()
{
transform.position = new Vector3(0, 0, 0);
}
void Update()
{
Movement();
Fire();
}
private void Movement()
{
//Player Movement
float hPos = Input.GetAxis("Horizontal");
float vPos = Input.GetAxis("Vertical");
transform.Translate(new Vector3(hPos, vPos, 0) * _speed * Time.deltaTime);
//Player Screen Clamping
transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -4, 0), 0);
if (transform.position.x > 10)
{
transform.position = new Vector3(-10, transform.position.y, 0);
}
else if (transform.position.x < -10)
{
transform.position = new Vector3(10, transform.position.y, 0);
}
}
private void Fire()
{
if (Input.GetButton("Fire1") && Time.time > _canFire)
{
_canFire = Time.time + _fireRate;
Instantiate(_laserPrefab, transform.position + new Vector3(0, 0.8f, 0), Quaternion.identity);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment