Reputation:
I'm struggling with the following issue in my game:
I have a grid represented by a map and I've attached to it a script called "Movement". The function "MoveToTile" works well:
public void MoveToTile()// Function to move the players
{
if (Input.GetMouseButtonDown(0))
{
//raycast checks and returns an object, if it's a tile, the unit moves
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
ismoving = true;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag == "Tile")
{
//checks if the tile is available based on the max movement of the unit
Tile tileAux = hit.transform.gameObject.GetComponent<Tile>();
Jogador scriptJog = player.GetComponent<Jogador>();
if ((tileAux.TilePostion.x - scriptJog.GridPosition.x) + (tileAux.TilePostion.y - scriptJog.GridPosition.y) >= -movMax && (tileAux.TilePostion.x - scriptJog.GridPosition.x) + (tileAux.TilePostion.y - scriptJog.GridPosition.y) <= movMax)
{
if ((tileAux.TilePostion.x - scriptJog.GridPosition.x) - (tileAux.TilePostion.y - scriptJog.GridPosition.y) >= -movMax && (tileAux.TilePostion.x - scriptJog.GridPosition.x) - (tileAux.TilePostion.y - scriptJog.GridPosition.y) <= movMax)
{
targetPosition = (hit.transform.position);
targetPosition.y = posY;
moveEnabled = true;
}
}
}
}
}
//player moves until reaches the position
if (player.transform.position != targetPosition)
{
player.transform.position = Vector3.MoveTowards(player.transform.position, targetPosition, velocity);
if (player.transform.position == targetPosition)
ismoving = false;
}
//if player reaches position, it's deselected
if (moveEnabled && !ismoving)
{
player.GetComponent<Jogador>().isPlayer = false;
moveEnabled = false;
if (gameManager.playerTurn == 1)
{
gameManager.PontoAcaop1--;
}
if (gameManager.playerTurn == 2)
{
gameManager.PontoAcaop2--;
}
if (gameManager.playerTurn == 3)
{
gameManager.PontoAcaop1--;
}
}
if (gameManager.PontoAcaop1 == 0)
{
gameManager.playerTurn = 2;
TurnEnd();
gameManager.PontoAcaop1 = 4;
}
if (gameManager.PontoAcaop2 == 0)
{
gameManager.playerTurn = 1;
TurnEnd();
gameManager.PontoAcaop2 = 4;
}
}
But in this script there're other two functions called "TeslaShooting" and "CrossAttack". I call them inside the Update method. However, the problem is that they are executed at the same time, for instance, when the player shoots with the character Tesla, I just want "TeslaShooting" to be executed, but it doesn't work.
"TeslaShooting" is this method here:
if (Input.GetMouseButtonDown(1)&& SpawBala.tag == "Bala1" && gameManager.playerTurn == 1)
{
//raycast checks and returns an object, if it's a tile, the unit shoots
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//checks if the tile is available based on the line and column of the unit
Tile tileAux = hit.transform.gameObject.GetComponent<Tile>();
Jogador scriptJog = player.GetComponent<Jogador>();
if (tileAux.TilePostion.x == scriptJog.GridPosition.x || tileAux.TilePostion.y == scriptJog.GridPosition.y)
{
if (tileAux.TilePostion.x > scriptJog.GridPosition.x)
tileAux.TilePostion.x = 5;
else
tileAux.TilePostion.x = 0;
if (tileAux.TilePostion.y > scriptJog.GridPosition.y)
tileAux.TilePostion.y = 5;
else
tileAux.TilePostion.y = 0;
//instantiates the bullet
GameObject tiro = Instantiate(projetil, SpawBala.transform.position, SpawBala.transform.rotation);
Rigidbody BalaRigid = tiro.GetComponent<Rigidbody>();
BalaRigid.velocity = SpawBala.transform.forward * ProjetilVeloc * Time.deltaTime;
gameManager.PontoAcaop1--;
}
}
}
And "CrossAttack" is this one:
if (Input.GetMouseButtonDown(1)&&SpawCruz.tag=="SpawnCruz"&&gameManager.playerTurn==1)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag == "Tile")
{
SpawCruz.transform.position = hit.transform.position;
Instantiate(Cruz, SpawCruz.transform.position, Quaternion.identity);
gameManager.PontoAcaop1--;
}
}
}
I set the playerTurn is this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jogador : MonoBehaviour
{
[SerializeField] private Mapa mapa;
public Vector2Int GridPosition;
public bool isPlayer = false;
public float Health = 50f;
public GameObject Spawnador1;
public int isWhichPlayer;
public TurnManager gameManager;
private void Awake()
{
if (!mapa) mapa = FindObjectOfType<Mapa>();
}
// Start is called before the first frame update
void Start()
{
foreach (var tile in mapa.GridMatriz)
{
//tile.jogador = this;
}
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit) && hit.transform == this.transform && (isWhichPlayer == gameManager.playerTurn))
{
mapa.GetComponent<Movimentação>().TurnEnd();
isPlayer = true;
mapa.GetComponent<Movimentação>().TurnStart();
Debug.Log("clicou");
}
}
CheckHealth();
}
public void CheckHealth()//If the player's health is 0 he goes to the base
{
if (Health <= 0 && this.tag == "Player")
{
this.transform.position = new Vector3(Spawnador1.transform.position.x, Spawnador1.transform.position.y, Spawnador1.transform.position.z);
Health = 50;
}
if (Health <= 0 && this.tag == "Player2")
{
this.transform.position = new Vector3(Spawnador1.transform.position.x, Spawnador1.transform.position.y, Spawnador1.transform.position.z);
Health = 50;
}
}
}
I don't know how to solve it.
Upvotes: 0
Views: 54
Reputation: 326
I don't fully understant your code, but i see that this two if's
if (Input.GetMouseButtonDown(1)&& SpawBala.tag == "Bala1" && gameManager.playerTurn == 1
if (Input.GetMouseButtonDown(1)&&SpawCruz.tag=="SpawnCruz"&&gameManager.playerTurn==1)
Can be true both in the same time. Shouldn't you add somthing more in this if statement? In each method you shoot the ray, this is can be problem too. I think you could get RayHit when you click on Enemy only once, and after that you should check what character should do: shoot tesla or crossAttack. In code you pasted i can't see that.
Upvotes: 1