When we click on one of the cubes it will shoot projectiles. I have created a function to decide which capsule will shoot i.e TurnDecider()
So at the beginning Player1 is active and so only it can shoot a projectile. As soon as he shoots the function TurnDecider() is called to change turn to Player2.
So logically only Player2 should be shooting but in the game both Player2 and Player3 are shooting.
- Left: Player 2
- Center: Player 1
- Right: Player 3
The scripts are given below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManage : MonoBehaviour
{
private int playernumber;
private PlayerController a, b, c;
public bool fired = false;
public void InitialAssign()
{
playernumber = 1;
a = GameObject.Find("Player (1)").GetComponent<PlayerController>();
b = GameObject.Find("Player (2)").GetComponent<PlayerController>();
c = GameObject.Find("Player (3)").GetComponent<PlayerController>();
TurnDecider();
}
void Start()
{
InitialAssign();
}
public void PlayerSelector()
{
if (playernumber == 3)
{
playernumber = 1;
}
else
{
playernumber++;
}
TurnDecider();
}
public void TurnDecider()
{
switch (playernumber)
{
case 1:
a.isReady=true;
b.isReady = false;
c.isReady = false;
Debug.Log("Player 1 is ready!");
break;
case 2:
a.isReady = false;
b.isReady = true;
c.isReady = false;
Debug.Log("Player 2 is ready!");
break;
case 3:
a.isReady = false;
b.isReady = false;
c.isReady = true;
Debug.Log("Player 3 is ready!");
break;
}
}
// Update is called once per frame
void Update()
{
if (fired == true)
{
PlayerSelector();
Debug.Log(playernumber);
fired = false;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public GameObject projectile;
public float Power = 10f;
public float JumpPower;
private Rigidbody rb;
private Transform shooter;
public GameManage gamescript;
private string obj;
public bool isReady = false;
private bool isGround = false;
void Start()
{
rb = GetComponent<Rigidbody>();
shooter=transform.Find("Shooter");
}
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Ground"))
{
isGround = true;
}
}
public void Fire()
{
GameObject ball = Instantiate(projectile, position: shooter.position, rotation: shooter.rotation);
ball.GetComponent<Rigidbody>().velocity = (GameObject.Find(obj).transform.position - shooter.position).normalized * Power;
}
public void Jump()
{
if (isGround)
{
rb.velocity = new Vector3(0f, JumpPower, 0f);
}
isGround = false;
}
void Update()
{
if (Input.GetMouseButtonDown(0) && isReady && isGround)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag == "Target")
{
Debug.Log("Fire Called by " + transform.name);
obj = hit.transform.name.ToString();
Fire();
gamescript.GetComponent<GameManage>().fired = true;
}
}
}
}
}