Перемещение двух объектов игрока одновременно

Я могу заставить оба объекта двигаться одновременно, но я хочу отключить движение одного, пока другой движется. Я не могу заставить его работать должным образом. Либо один объект игрока по-прежнему получает входные данные, в то время как другой все еще движется, либо одновременно перемещается только один. Как я могу сделать эту работу?

Скрипт контроллера:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
    public float speed;
    public bool isMoving;
    public bool countMove;
    public float distance;
    public Vector3 endPos;
    public int par;
    public int moves;
    public int playerMoves;
    bool upDetect;
    bool downDetect;
    bool rightDetect;
    bool leftDetect;
    Vector3 upLine;
    Vector3 downLine;
    Vector3 leftLine;
    Vector3 rightLine;
    Vector3 actionLine;
    Rigidbody rb;
    GameObject Player2;
    PlayerController script;
    GameObject movesText;
    Scoring scoreScript;

    void Start()
    {
        isMoving = false;
        par = Counter.levelPar;
        endPos = transform.position;
        rb = GetComponent<Rigidbody>();

        if (this.name == "Player")
        {
            Player2 = GameObject.Find("Player2");
        }

        if (this.name =="Player2")
        {
            Player2 = GameObject.Find("Player");
        }

        if (Player2 != null)
        {
            script = Player2.GetComponent<PlayerController>();
            script.moves = moves;
            script.par = par;
            movesText = GameObject.Find("movesText");
            scoreScript = movesText.GetComponent<Scoring>();
        }
    }

    private void FixedUpdate()
    {
        upLine = new Vector3(transform.position.x, transform.position.y, transform.position.z + 1);
        upDetect = Physics.Linecast(transform.position, upLine);
        Debug.DrawLine(transform.position, upLine);

        downLine = new Vector3(transform.position.x, transform.position.y, transform.position.z - 1);
        downDetect = Physics.Linecast(transform.position, downLine);
        Debug.DrawLine(transform.position, downLine);

        leftLine = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z);
        leftDetect = Physics.Linecast(transform.position, leftLine);
        Debug.DrawLine(transform.position, leftLine);

        rightLine = new Vector3(transform.position.x + 1, transform.position.y, transform.position.z);
        rightDetect = Physics.Linecast(transform.position, rightLine);
        Debug.DrawLine(transform.position, rightLine);

       // actionLine = new Vector3(transform.position.x, transform.position.y - 1, transform.position.z);
       // actionCheck = Physics.Linecast(transform.position, actionLine);
       // Debug.DrawLine(transform.position, actionLine);

        if (Input.GetKey("left") && isMoving == false && leftDetect == false)
        {
            if (Player2 == null || script.isMoving == false)
            {
                isMoving = true;
                RaycastHit hit;
                Ray leftRay = new Ray(transform.position, Vector3.left);
                if (Physics.Raycast(leftRay, out hit))
                {
                    if (hit.collider != null && hit.collider.tag != "Player")
                    {
                        endPos = new Vector3(hit.collider.transform.position.x + 1, endPos.y, endPos.z);
                    }

                    if (hit.collider.tag == "Player")
                    {
                        GameObject oPlayer = hit.collider.gameObject;
                        endPos = GetOtherEndPos(oPlayer);
                        endPos = new Vector3(endPos.x + 1, endPos.y, endPos.z);
                    }
                }

                countMove = true;
            }
        }

        if (Input.GetKey("right") && isMoving == false && rightDetect == false)
        {
            if (Player2 == null || script.isMoving == false)
            {
                isMoving = true;
                RaycastHit hit;
                Ray rightRay = new Ray(transform.position, Vector3.right);
                if (Physics.Raycast(rightRay, out hit))
                {
                    if (hit.collider != null && hit.collider.tag != "Player")
                    {
                        endPos = new Vector3(hit.collider.transform.position.x - 1, endPos.y, endPos.z);
                    }

                    if (hit.collider.tag == "Player")
                    {
                        GameObject oPlayer = hit.collider.gameObject;
                        endPos = GetOtherEndPos(oPlayer);
                        endPos = new Vector3(endPos.x - 1, endPos.y, endPos.z);
                    }
                }
                countMove = true;
            }
        }

        if (Input.GetKey("up") && isMoving == false && upDetect == false)
        {
            if (Player2 == null || script.isMoving == false)
            {
                isMoving = true;
                RaycastHit hit;
                Ray upRay = new Ray(transform.position, Vector3.forward);
                if (Physics.Raycast(upRay, out hit))
                {
                    if (hit.collider != null && hit.collider.tag != "Player")
                    {
                        endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z - 1);
                    }

                    if (hit.collider.tag == "Player")
                    {
                        GameObject oPlayer = hit.collider.gameObject;
                        endPos = GetOtherEndPos(oPlayer);
                        endPos = new Vector3(endPos.x, endPos.y, endPos.z - 1);
                    }
                }
                countMove = true;
            }
        }

        if (Input.GetKey("down") && isMoving == false && downDetect == false)
        {
            if (Player2 == null || script.isMoving == false)
            {
                isMoving = true;
                RaycastHit hit;
                Ray downRay = new Ray(transform.position, -Vector3.forward);
                if (Physics.Raycast(downRay, out hit))
                {
                    if (hit.collider != null && hit.collider.tag != "Player")
                    {
                        endPos = new Vector3(endPos.x, endPos.y, hit.collider.transform.position.z + 1);
                    }

                    if (hit.collider.tag == "Player")
                    {
                        GameObject oPlayer = hit.collider.gameObject;
                        endPos = GetOtherEndPos(oPlayer);
                        endPos = new Vector3(endPos.x, endPos.y, endPos.z + 1);
                    }
                }
                countMove = true;
            }
        }

        distance = Vector3.Distance(transform.position, endPos);
        //Debug.Log(distance);
        //Debug.Log("Name: " + this.name + " distance = " + distance + " vel = " + rb.velocity.magnitude + " isMoving: " + isMoving);
        //Debug.Log(rightDetect);
        //Debug.Log(leftDetect);
        //Debug.Log(upDetect);
        //Debug.Log(downDetect);
        //Debug.Log(isMoving);
        //Debug.Log("Velocity = " + rb.velocity.magnitude);

        if (distance < 0.1)
        {
            distance = 0;
        }

        if (distance > 0)
        {
            transform.position = Vector3.Lerp(
                transform.position, endPos,
                Time.deltaTime * speed / distance);
            transform.rotation = Quaternion.identity;
        }

        else
        {
            isMoving = false;
            countMove = false;
            endPos = transform.position;
        }

    }

    Vector3 GetOtherEndPos(GameObject oPlayer)
    {
        script = oPlayer.GetComponent<PlayerController>();
        return script.endPos;
    }

    /*void countMove()
    {
        playerMoves++;
        moves--;
    }

    void setMoves()
    {
        moveText.text = "Moves: " + moves.ToString();
    }*/



}

0 ответов

Другие вопросы по тегам