Hello again,
Sorry for the late reply. Please find the script for the player script (i named the player: Clover)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Clover : MonoBehaviour {
public float speed = 15f;
public float health = 150f;
float MaxX;
float MinX;
float Padding = 0.5f;
public GameObject LaserFist;
public float LaserSpeed = 7f;
public float FiringRate;
public AudioClip InfinitySound;
// Use this for initialization
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));
MinX = leftmost.x;
MaxX = rightmost.x - Padding;
}
void Fire () {
Vector3 offsef = new Vector3 (0,1,0);
GameObject laser = Instantiate(LaserFist, transform.position + offsef, Quaternion.identity) as GameObject;
laser.GetComponent<Rigidbody2D>().velocity = new Vector3 (0,LaserSpeed,0);
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.Space)) {
InvokeRepeating ("Fire", 0.01f, FiringRate);
}
if (Input.GetKeyUp (KeyCode.Space)) {
CancelInvoke ("Fire");
}
if (Input.GetKey(KeyCode.LeftArrow))
{
// this part (speed * Time.deltaTime) makes the movement frame independant
//transform.position += Vector3.left * speed * Time.deltaTime; use THIS Or:
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.RightArrow))
{
// this part (speed * Time.deltaTime) makes the movement frame independant
//transform.position += Vector3.right * speed * Time.deltaTime; use THIS Or:
transform.position += Vector3.right * speed * Time.deltaTime;
}
// To restrict the player with in the game space. In 2D games Vector2 can be used instead of Vector3
float newX = Mathf.Clamp (transform.position.x, MinX,MaxX);
transform.position = new Vector3 (newX, transform.position.y, transform.position.z);
}
void OnTriggerEnter2D (Collider2D collider)
{
Projectile missile = collider.gameObject.GetComponent <Projectile> ();
if (missile) {
health -= missile.GetDamage ();
missile.Hit ();
if (health <= 0) {
Die();
}
print ("We Are hit!");
}
}
void Die ()
{
LevelManager man = GameObject.Find("LevelManager").GetComponent <LevelManager>();
man.GotoScene ("Win Scene");
Destroy (gameObject);
AudioSource.PlayClipAtPoint (InfinitySound, transform.position);
}
}