Rob,
Rob,
I’ve checked the game objects.
The Controller Script is attached to the Player
the Enemy Behavior script is attached to the Enemy;
both beams have the projectile script attached;
all rigidbodies are ticked kinematic and use full kinematic contact,
enemy and both beams are using box colliders and ticked as triggers;
Player uses a polygon collider and it is ticked as a trigger;
Should the player and the enemy have both controller and enemy behavior scripts attached?
Like wise should the beams have the projectile script and their appropriate player/enemy script attached?
Thanks again for your help.
All the best,
William
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour {
public GameObject projectile;
public float projectileSpeed = 10f;
public float firingRate = 0.2f;
public float speed = 15.0f;
public float padding = 1f;
public float health = 250f;
private float xmax;
private float xmin;
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()
{
Vector3 offset = new Vector3 (0, 1, 0);
GameObject beam = Instantiate(projectile, transform.position + offset, Quaternion.identity);
beam.GetComponent<Rigidbody2D>().velocity = new Vector3(0, projectileSpeed, 0);
Debug.Log("Player Missiles away");
}
void Update () {
if(Input.GetKeyDown(KeyCode.Space))
{
InvokeRepeating ("Fire", 0.000001f, firingRate);
}
if (Input.GetKeyUp(KeyCode.Space))
{
CancelInvoke ("Fire");
}
// move the player left and right in the gamespace
if(Input.GetKey(KeyCode.LeftArrow)){
transform.position = new Vector3(
Mathf.Clamp(transform.position.x - speed * Time.deltaTime, xmin, xmax),
transform.position.y,
transform.position.z
);
}else if (Input.GetKey(KeyCode.RightArrow)){
transform.position = new Vector3(
Mathf.Clamp(transform.position.x + speed * Time.deltaTime, xmin, xmax),
transform.position.y,
transform.position.z
);
}
// restrct player to the gamespace
float newX = Mathf.Clamp (transform.position.x, xmin, xmax);
transform.position = new Vector3(newX, transform.position.y, transform.position.z);
}
void OnTriggerEnter2D(Collider2D coll)
{
Projectile missile = coll.gameObject.GetComponent<Projectile>();
if (missile)
{
health -= missile.GetDamage();
missile.Hit();
if (health <= 0)
{
Destroy (gameObject);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyBehavior : MonoBehaviour {
public GameObject projectile;
public float projectileSpeed = 10.0f;
public float health = 150f;
public float shotsPerSeconds = 0.5f;
private Controller controller;
void Update ()
{
float probability = Time.deltaTime * shotsPerSeconds;
if (Random.value < probability) {
Fire ();
}
}
void Fire ()
{
Vector3 startPosition = transform.position + new Vector3 (0, -1, 0);
GameObject missile = Instantiate (projectile, startPosition, Quaternion.identity);
missile.GetComponent<Rigidbody2D>().velocity = new Vector2 (0, -projectileSpeed);
Debug.Log (" Enemy Missile away");
}
void OnTriggerEnter2D(Collider2D coll)
{
Projectile missile = coll.gameObject.GetComponent<Projectile>();
if (missile)
{
health -= missile.GetDamage();
missile.Hit();
if (health <= 0)
{
Destroy (gameObject);
}
}
}
}