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