jegatheesan
Published

Unity Multiplayer 3D Hologram Game and Hologram Projector

Develop Cheap 3D hologram for PC with Own Game in Unity.

IntermediateFull instructions provided24 hours42
Unity Multiplayer 3D Hologram Game and Hologram Projector

Things used in this project

Hardware components

Computer Monitor
×1
Laptop or PC
×1
3/4" Wiring PVC pipe, Elbow, Tee
×1

Software apps and online services

Unity
Unity

Hand tools and fabrication machines

HackSaw blade

Story

Read more

Schematics

Dimension for Hologram

Code

BallScript.cs

C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI ;
using TMPro;

public class BallScript : MonoBehaviour {
    public Text Player1score;
    public Text Player2score;
    TextMesh P1S;
    TextMesh P2S; 
    private Vector3 direction;
    float speed;
    bool goal;
    bool player2point;
    bool player1point;
    int p1point = 0;
    int p2point = 0;
    bool gameend;
    int maxscore;

    TextMeshPro p1p2s;
    TextMeshPro p2p2s;
    TextMeshPro p1p1s;
    TextMeshPro p2p1s;

	void Start () {
        maxscore = 10;
        this.direction = new Vector3(1.0f,0.0f,1.0f).normalized;
        this.speed = 0.1f;
        goal = false;
        player2point = false;
        player1point = false;
        p1point = 0;
        p2point = 0;
        p1p2s = GameObject.Find("P1Player2score").GetComponent<TextMeshPro>();
        p2p2s = GameObject.Find("P2Player2score").GetComponent<TextMeshPro>();
        p1p1s = GameObject.Find("P1Player1score").GetComponent<TextMeshPro>();
        p2p1s = GameObject.Find("P2Player1score").GetComponent<TextMeshPro>();
        gameend = false;
	}
    void Update()
    {
        if (goal == false)
        {
            this.transform.position += direction * speed;
        }
        else if(player1point==true) 
        {            
            this.transform.position = new Vector3(GameObject.Find("Player2").transform.position.x, 0.430f, GameObject.Find("Player2").transform.position.z - 0.625f); 
        }
        else if (player2point == true)
        {
            this.transform.position = new Vector3(GameObject.Find("Player1").transform.position.x, 0.430f, GameObject.Find("Player1").transform.position.z + 0.625f);   
        }
        if (gameend == false)
        {
            if (Input.GetKeyUp(KeyCode.UpArrow) && player2point == true)
            {
                player2point = false;
                goal = false;
                this.direction = new Vector3(1.0f, 0.0f, 1.0f).normalized;
                this.speed = 0.1f;
            }
            if (Input.GetKeyUp(KeyCode.W) && player1point == true)
            {
                player1point = false;
                goal = false;
                this.direction = new Vector3(1.0f, 0.0f, -1.0f).normalized;
                this.speed = 0.1f;
            }
        }
        else
        {
            if (Input.GetKeyUp(KeyCode.Space))
            {
                gameend = false;
                p1p2s.SetText ("Player2");
                p2p2s.SetText("Player2");
                p1p1s.SetText("Player1");
                p2p1s.SetText("Player1");
                p1point = 0;
                p2point = 0;
                this.speed = 0.1f;
            }
        }
    }
    void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.name == "Player1")
        {
            speed = speed + 0.02f;
        }
        else if (collision.collider.name == "Player2")
        {
            speed = speed + 0.02f;
        }
        else if (collision.collider.name == "WallP1")
        {
            if (player2point == false)
            {
                p2point++;
                p1p2s.SetText("Player2-" + p2point.ToString());
                p2p2s.SetText("Player2-" + p2point.ToString());
                if (p2point == maxscore)
                {
                    p2p2s.SetText("WIN");
                    p2p1s.SetText("WIN");
                    p1p2s.SetText("LOSS");
                    p1p1s.SetText("LOSS");
                    gameend = true;
                }
            }
            this.speed = 0.1f;
            player2point = true;
            goal = true;
            this.transform.position = new Vector3(GameObject.Find("Player1").transform.position.x, 0.430f, GameObject.Find("Player1").transform.position.z + 0.625f); 
        }
        else if (collision.collider.name == "WallP2")
        {
            if (player1point == false)
            {
                p1point++;
                p1p1s.SetText("Player1-" + p1point.ToString());
                p2p1s.SetText("Player1-" + p1point.ToString());
                if (p1point == maxscore)
                {
                    p2p2s.SetText("LOSS");
                    p2p1s.SetText("LOSS");
                    p1p2s.SetText("WIN");
                    p1p1s.SetText("WIN");
                    gameend = true;
                }
            }
            this.speed = 0.1f;
            player1point = true;
            goal = true;
            this.transform.position = new Vector3(GameObject.Find("Player2").transform.position.x, 0.430f, GameObject.Find("Player2").transform.position.z - 0.625f); 
        }
        if (goal == false)
        {
            Vector3 normal = collision.contacts[0].normal;
            direction = Vector3.Reflect(direction, normal);
        }
    }
 }

Player1movement.cs

C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player1movement : MonoBehaviour {
    public float speed;
    public bool pcoll1;
    public bool pcoll2;
	void Start () {
        pcoll1 = false;
        pcoll2 = false;
	}

    void OnCollisionEnter(Collision collision)
    {
        Debug.Log(collision.collider.name);
        if (collision.collider.name == "Wall2")
        {
            pcoll1 = true;
        }
        if (collision.collider.name == "Wall1")
        {
            pcoll2 = true;
        }
    }
    void OnCollisionExit(Collision collision)
    {
        Debug.Log(collision.collider.name);
        if (collision.collider.name == "Wall2")
        {
            pcoll1 = false;
        }
        if (collision.collider.name == "Wall1")
        {
            pcoll2 = false;
        }
    }

	void Update () {
        if (Input.GetKey(KeyCode.RightArrow) && pcoll1 ==false)
        {
            transform.Translate(Vector3.left * speed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.LeftArrow) && pcoll2 == false)
        {
            transform.Translate(Vector3.right  * speed * Time.deltaTime);
        }
	}
}

Player2movement.cs

C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player2movement : MonoBehaviour
{
    public float speed;
    public bool pcoll1;
    public bool pcoll2;
    void Start()
    {
        pcoll1 = false;
        pcoll2 = false;
    }

    void OnCollisionEnter(Collision collision)
    {
        Debug.Log(collision.collider.name);
        if (collision.collider.name == "Wall2")
        {
            pcoll1 = true;
        }
        if (collision.collider.name == "Wall1")
        {
            pcoll2 = true;
        }
    }
    void OnCollisionExit(Collision collision)
    {
        Debug.Log(collision.collider.name);
        if (collision.collider.name == "Wall2")
        {
            pcoll1 = false;
        }
        if (collision.collider.name == "Wall1")
        {
            pcoll2 = false;
        }
    }

    void Update()
    {
        if (Input.GetKey(KeyCode.A) && pcoll1 == false)
        {
            transform.Translate(Vector3.left * speed * Time.deltaTime);
        }
        if (Input.GetKey(KeyCode.D) && pcoll2 == false)
        {
            transform.Translate(Vector3.right * speed * Time.deltaTime);
        }
    }
}

Credits

jegatheesan

jegatheesan

18 projects • 66 followers
Simply A Mechatronics Lover.

Comments