Here is my player controller script;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed = 15.0f;
public float padding = 1f;
public GameObject projectile;
public float projectileSpeed;
public float firingRate = 0.2f;
private Rigidbody2D PlayerLaser;
float xmin;
float xmax;
void Start()
{
float distance = transform.position.z - Camera.main.transform.position.z;
Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0,0,distance));
Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distance));
xmin = leftmost.x + padding;
xmax = rightmost.x - padding;
}
void Fire()
{
GameObject beam = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
PlayerLaser.velocity = new Vector3(0, projectileSpeed, 0);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
CancelInvoke("Fire");
}
{
InvokeRepeating("Fire", 0.000001f, firingRate);
}
if (Input.GetKeyUp(KeyCode.Space))
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += Vector3.left * speed * Time.deltaTime;
print("L arrow down");
}
else if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += Vector3.right * speed * Time.deltaTime;
print("R arrow down");
}
float newX = Mathf.Clamp(transform.position.x, xmin, xmax);
transform.position = new Vector3(newX, transform.position.y, transform.position.z);
}
}
Also showing an error