简体   繁体   中英

Move two player objects simultaneously with Raycast

We have our player controller script setup and working for an individual player object, but when we want to add a second one, we are running into complications. I know we could potentially use a LayerMask to have the RayCast ignore a player object, but when that happens, both objects will try to move into the same space and cause problems. I'm stumped at this point.

Player Controller Script:

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

public class PlayerController : MonoBehaviour
{
    public float speed;
    bool isMoving;
    float distance;
    Vector3 endPos;
    public Text parText;
    private int par;
    public static int moves;
    bool upDetect;
    bool downDetect;
    bool rightDetect;
    bool leftDetect;
    Vector3 upLine;
    Vector3 downLine;
    Vector3 leftLine;
    Vector3 rightLine;
    public bool actionCheck;
    Vector3 actionLine;

    void Start()
    {
        isMoving = false;
        par = Counter.levelPar;
        endPos = transform.position;
        //setPar();
    }

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

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

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

        rightLine = new Vector3(transform.position.x + 0.1f, 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)
        {
            isMoving = true;
            RaycastHit hit;
            Ray leftRay = new Ray(transform.position, Vector3.left);
            if (Physics.Raycast(leftRay, out hit))
            {
                if (hit.collider != null)
                {
                    endPos = new Vector3(hit.collider.transform.position.x + 1, endPos.y, endPos.z);
                }
            }
            //countMove();
        }

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

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

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



        distance = Vector3.Distance(transform.position, endPos);
        //Debug.Log(distance);
        //Debug.Log(rightDetect);
        //Debug.Log(leftDetect);
        //Debug.Log(upDetect);
        //Debug.Log(downDetect);

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

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

        //setPar();

    }

    /*void countMove()
    {
        moves++;

        if (par > 0)
        {
            par--;
        }
    }

    void setPar()
    {
        parText.text = "Par: " + par.ToString();
    }*/

}

Figured it out. I had to use the Raycast hit to figure out what object it had hit. If the object had a "Player" tag, then it would go to that object's PlayerController script and get the value for it's end pos. Then I just had to adjust the endPos variable to compensate for the extra space. Here's my code.

PlayerController:

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

public class PlayerController : MonoBehaviour
{
    public float speed;
    bool isMoving;
    public float distance;
    public Vector3 endPos;
    public Text parText;
    private int par;
    public static int moves;
    bool upDetect;
    bool downDetect;
    bool rightDetect;
    bool leftDetect;
    Vector3 upLine;
    Vector3 downLine;
    Vector3 leftLine;
    Vector3 rightLine;
    Vector3 actionLine;
    Rigidbody rb;

    void Start()
    {

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

        //setPar();
    }

    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)
        {
            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();
        }

        if (Input.GetKey("right") && isMoving == false && rightDetect == 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();
        }

        if (Input.GetKey("up") && isMoving == false && upDetect == 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();
        }

        if (Input.GetKey("down") && isMoving == false && downDetect == 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();
        }

        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;
            endPos = transform.position;
        }

        //setPar();

    }

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

    /*void countMove()
    {
        moves++;

        if (par > 0)
        {
            par--;
        }
    }

    void setPar()
    {
        parText.text = "Par: " + par.ToString();
    }*/

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM