Игрок не двигается после появления - PullRequest
0 голосов
/ 28 октября 2019

Я сейчас работаю над тактической RPG, и у меня есть следующая проблема:

Когда игрок уничтожен, он появляется в определенной позиции в сетке. Однако после появления он не двигается.

Я использую этот скрипт, чтобы уничтожить игрока и вызвать его. Он прикреплен к каждому игроку в игре:

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 SpawnPoint;


    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)
            {
                mapa.GetComponent<Movimentação>().TurnEnd();
                isPlayer = true;
                mapa.GetComponent<Movimentação>().TurnStart();
                Debug.Log("clicked");
            }

        }
            CheckHealth();
    }

    public void CheckHealth()// Function to check the player's health
    {
        if (Health <= 0)
        {
            Destroy(gameObject);
            Instantiate(this, Spawnador.transform.position, Quaternion.identity);
        }
    }
}

Это сценарий движения:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Movimentação : MonoBehaviour
{
    //player variables
    public GameObject player;
    public GameObject[] Personagens;

    //moving variables
    Vector3 targetPosition;
    float posY = 1;
    public float velocity = 0.2f;
    public float movMax = 3;
    public bool ismoving = false;
    public bool moveEnabled;
    public int aux;

    //bullet variables
    public GameObject projetil;
    private GameObject SpawBala;
    public float ProjetilVeloc = 500f;

    private void Start()
    {
        //sets the first unit as the active unit at the start of the game
        Personagens[0].GetComponent<Jogador>().isPlayer = true;
        TurnStart();

    }

    // Update is called once per frame
    void Update()
    {
        //left mouse button to start movement
        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;
                        }

                    }
                }
            }

        }

        //right click to shoot
        if (Input.GetMouseButtonDown(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;

                    Debug.Log(tileAux.TilePostion.x);
                    Debug.Log(tileAux.TilePostion.y);

                    //instantiates the bullet
                    GameObject tiro = Instantiate(projetil, SpawBala.transform.position, SpawBala.transform.rotation);
                    Rigidbody BalaRigid = tiro.GetComponent<Rigidbody>();

                    if (SpawBala.tag == "Bala1")
                    {
                        BalaRigid.AddForce(Vector3.forward * ProjetilVeloc);

                    }

                    if (SpawBala.tag == "Bala2")
                    {
                        BalaRigid.AddForce(Vector3.back * ProjetilVeloc);
                    }
                }
            }
        }

        //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;
        }
    }

    public void TurnStart()
    {
        //makes the selected unit the active unit
        for (int i = 0; i < Personagens.Length; i++)
        {
            if (Personagens[i].GetComponent<Jogador>().isPlayer == true)
            {
                player = Personagens[i];
                posY = player.transform.position.y;
                targetPosition = player.transform.position;
                SpawBala = player.transform.GetChild(0).gameObject;
            }
        }
    }

    public void TurnEnd()
    {
        //desactivates all units
        for (int i = 0; i < Personagens.Length; i++)
        {
            Personagens[i].GetComponent<Jogador>().isPlayer = false;
        }
    }
}

Я действительно не знаю, что происходит, ребята.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...