After about a days worth of time, I was able to make this. I plan on improving it by making actual levels with some detail using something from the asset store and adding more depth to the gameplay (Like more attacks)but for now, I’m pretty proud of this [even with the errors I haven’t solved yet]. Also, if somebody learning unity as well wants to see the code, I included it in a spoiler tag below the video. The code isn’t commented tho.
PlayerHandler.cs
using UnityEngine;
using UnityEngine.UI;
public class PlayerHandler : MonoBehaviour {
#region Player Variables
public GameObject Player1;
public float P1health = 100;
public GameObject Particles1;
public GameObject Emitter1;
public GameObject Player2;
public float P2health = 100;
public GameObject Particles2;
public GameObject Emitter2;
private Rigidbody rb1;
private Rigidbody rb2;
private bool isFiring1;
private bool isFiring2;
private bool grounded;
private bool grounded2;
private int BulletDirection1;
private int BulletDirection2;
#endregion
#region Shared Variables
public float speed;
public float turnspeed;
public float JumpHeight;
#endregion
#region Others
public GameObject DeathParticles;
public GameObject Bullet;
public float bulletSpeed;
public bool Death;
#endregion
void Fire(){
if(isFiring1 == true){
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Bullet,Emitter1.transform.position,Emitter1.transform.rotation) as GameObject;
if(BulletDirection1 == 1){
Temporary_Bullet_Handler.transform.rotation = Quaternion.Euler(0, 0, 90);
}
if(BulletDirection1 == -1){
Temporary_Bullet_Handler.transform.rotation = Quaternion.Euler(0,0,270);
}
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent();
Temporary_RigidBody.AddForce(new Vector3(BulletDirection1, 0, 0) * bulletSpeed);
Destroy(Temporary_Bullet_Handler, 3.0f);
}
if(isFiring2 == true){
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Bullet,Emitter2.transform.position,Emitter2.transform.localRotation) as GameObject;
if(BulletDirection2 == 1){
Temporary_Bullet_Handler.transform.rotation = Quaternion.Euler(0, 0, 90);
}
if(BulletDirection2 == -1){
Temporary_Bullet_Handler.transform.rotation = Quaternion.Euler(0,0,270);
}
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent();
Temporary_RigidBody.AddForce(new Vector3(BulletDirection2, 0, 0) * bulletSpeed);
Destroy(Temporary_Bullet_Handler, 3.0f);
}
}
void GameOver(){
if(P1health <= 0){
Destroy(Player1);
Instantiate(DeathParticles, Player1.transform.position,DeathParticles.transform.rotation);
}
if(P2health <= 0){
Debug.Log(“P2Dead”);
Destroy(Player2);
Instantiate(DeathParticles, Player2.transform.position, DeathParticles.transform.rotation);
}
}
void Start () {
rb1 = Player1.GetComponent();
rb2 = Player2.GetComponent();
BulletDirection1 = 1;
BulletDirection2 = -1;
isFiring1 = false;
isFiring2 = false;
Death = false;
}
void FixedUpdate () {
if(rb1 != null){
#region Player1Controls
if(rb1.velocity.y <= -0.1 || rb1.velocity.y >= 0.1){
grounded = false;
}
if(rb1.velocity.y == 0){
grounded = true;
}
if(rb1.velocity.x <= -0.2 || rb1.velocity.x >= 0.2){
Particles1.SetActive(true);
}
if(rb1.velocity.x == 0){
Particles1.SetActive(false);
}
//move right
if(Input.GetKey(“d”)){
rb1.AddForce(Vector3.right * speed * Time.fixedDeltaTime);
Player1.transform.rotation = Quaternion.Euler(0, Mathf.Lerp(Player1.transform.rotation.y, 0, turnspeed), 0);
BulletDirection1 = 1;
}
//move left
if(Input.GetKey(“a”)){
rb1.AddForce(Vector3.left * speed * Time.fixedDeltaTime);
//Player1.transform.rotation = Quaternion.Euler(0,180, 0);
Player1.transform.rotation = Quaternion.Euler(0, Mathf.Lerp(Player1.transform.rotation.y, 180, turnspeed), 0);
BulletDirection1 = -1;
}
//move up
if(Input.GetKeyDown(“w”) && grounded == true){
rb1.AddForce(Vector3.up * JumpHeight * Time.fixedDeltaTime);
}
}
#endregion
#region PLayer2Controls
if(rb2 != null){
if(rb2.velocity.y <= -0.1 || rb2.velocity.y >= 0.1){
grounded2 = false;
}
if(rb2.velocity.y == 0){
grounded2 = true;
}
if(rb2.velocity.x <= -0.2 || rb1.velocity.x >= 0.2){
Particles2.SetActive(true);
}
if(rb2.velocity.x == 0){
Particles2.SetActive(false);
}
//move right
if(Input.GetKey(KeyCode.RightArrow)){
rb2.AddForce(Vector3.right * speed * Time.fixedDeltaTime);
Player2.transform.rotation = Quaternion.Euler(0, 0, 0);
BulletDirection2 = 1;
}
//move left
if(Input.GetKey(KeyCode.LeftArrow)){
rb2.AddForce(Vector3.left * speed * Time.fixedDeltaTime);
Player2.transform.rotation = Quaternion.Euler(0,180, 0);
BulletDirection2 = -1;
}
//move up
if(Input.GetKeyDown(KeyCode.UpArrow) && grounded2 == true){
rb2.AddForce(Vector3.up * JumpHeight * Time.fixedDeltaTime);
}
#endregion
}
if(P1health <= 0 && Death == false || P2health <=0 && Death == false){
GameOver();
Death = true;
}
//End of FixedUpdate
}
void Update () {
if(Input.GetKeyDown(KeyCode.Space)){
isFiring1 =true;
Fire();
}
else{
isFiring1 =false;
}
if(Input.GetKeyDown(KeyCode.RightControl)){
isFiring2 =true;
Fire();
}
else{
isFiring2 =false;
}
}
}
UIcontroller.cs
using UnityEngine;
using UnityEngine.UI;
public class UIController : MonoBehaviour {
public Text Wintext;
public Text HealthDisplay;
public GameObject Players;
private PlayerHandler scriptRef;
void Start () {
scriptRef = Players.GetComponent();
Wintext.text = “”;
}
void Update () {
if(HealthDisplay != null){
HealthDisplay.text = "Player1: " + scriptRef.P1health + " Player2: " + scriptRef.P2health;
}
if(scriptRef.P1health <= 0){
Destroy(HealthDisplay);
Wintext.text = “PLAYER 2 HAS WON!”;
}
if(scriptRef.P2health <= 0){
Destroy(HealthDisplay);
Wintext.text = “PLAYER 1 HAS WON!”;
}
}
}
BulletScript.cs
using UnityEngine;
public class BulletScript : MonoBehaviour {
GameObject Players;
public PlayerHandler ph;
void Start () {
Players = GameObject.Find(“Players”);
ph = Players.GetComponent();
}
void OnTriggerEnter(Collider other){
if(other.CompareTag(“Player1”)){
ph.P1health -= 10;
Destroy(gameObject);
}
if(other.CompareTag(“Player2”)){
ph.P2health -= 10;
Destroy(gameObject);
}
if(other.CompareTag(“Environment”)){
Destroy(gameObject);
}
}
}
Any feedback about my code or the project, in general, will be very much appreciated.
Also, every since I started coding, I find myself ending sentences with a semi-colon. That’s weird