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.
 
 

69 lines
2.1 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField]
private float speed = 1.0f;
[SerializeField]
private GameObject laserPrefab;
[SerializeField]
private float fireRate = 0.15f;
private float nextFire = -1f;
void Start()
{
// Take the current position = new position (0, 0, 0)
transform.position = new Vector3(0, 0, 0);
}
void Update()
{
CalculateMovement();
if (Input.GetButton("Fire1") && Time.time > nextFire)
{
ShootLaser();
}
}
private void ShootLaser()
{
//if (Input.GetKeyDown(KeyCode.Space))
//{
// Instantiate(laserPrefab, transform.position, Quaternion.identity);
//}
nextFire = Time.time + fireRate;
Instantiate(laserPrefab, transform.position + new Vector3(0, 0.8f, 0), Quaternion.identity);
}
private void CalculateMovement()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
transform.Translate(new Vector3(horizontalInput, verticalInput, 0) * speed * Time.deltaTime);
// 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);
//}
// 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);
}
}
}