I’m making a small game for myself and I’m struggling to find a solution to a problem:
When I make a black obstacle with tag “Obstacle” the player pass through it but the player cannot move through the barrier that have the same tag
In this video the blue one pass through the barriers while the red can’t
The CheckMovement function should give me true if there’s no “Obstacle” tag objects in the direction I’m going and if there is don’t move the player but sometimes it doesn’t work like in the video while I can set my player position inside other objects
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerMovement : MonoBehaviour
{
private Vector3 startPos, endPos;
[SerializeField] private LayerMask mask;
[SerializeField] GameObject redPlayer;
[SerializeField] GameObject bluePlayer;
private Vector3 pos;
void Update()
{
CheckPlayerPos();
if (Input.GetMouseButtonDown(0))
{
startPos = ClickedPoint();
}
if (Input.GetMouseButtonUp(0))
{
endPos = ClickedPoint();
Vector3 distance = endPos - startPos;
if (distance.z > 0 && Mathf.Abs(distance.z) > Mathf.Abs(distance.x))
{
Debug.Log("Top");
pos = Vector3.forward;
MovePlayers();
return;
}
else if (distance.z < 0 && Mathf.Abs(distance.z) > Mathf.Abs(distance.x))
{
Debug.Log("Bot");
pos = Vector3.back;
MovePlayers();
return;
}
else if (distance.x < 0 && Mathf.Abs(distance.z) < Mathf.Abs(distance.x))
{
Debug.Log("Left");
pos = Vector3.left;
MovePlayers();
return;
}
else if (distance.x > 0 && Mathf.Abs(distance.z) < Mathf.Abs(distance.x))
{
Debug.Log("Right");
pos = Vector3.right;
MovePlayers();
return;
}
}
}
private void CheckPlayerPos()
{
if (redPlayer.transform.position.y < 0 || bluePlayer.transform.position.y < 0)
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
private void MovePlayers()
{
if (CheckMovement(redPlayer.transform.position))
redPlayer.transform.position += pos;
if (CheckMovement(bluePlayer.transform.position))
bluePlayer.transform.position -= pos;
}
private bool CheckMovement(Vector3 playerPos)
{
Vector3 tempPos = playerPos + pos;
Collider[] fromPos = Physics.OverlapSphere(tempPos, .25f);
foreach (Collider collPos in fromPos)
{
if (collPos.CompareTag("Obstacle"))
{
return false;
}
}
return true;
}
private Vector3 ClickedPoint()
{
Vector3 pos = Vector3.zero;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new();
if (Physics.Raycast(ray, out hit, Mathf.Infinity, mask))
{
pos = hit.point;
}
return pos;
}
}
The code used for swiping and move the player is this but is really inaccurate. Since I’m making a 3D isometric game with movement based on screen swipe (Mobile) this works but sometimes I get the wrong input
if (Input.GetMouseButtonDown(0))
{
startPos = ClickedPoint();
}
if (Input.GetMouseButtonUp(0))
{
endPos = ClickedPoint();
Vector3 distance = endPos - startPos;
if (distance.z > 0 && Mathf.Abs(distance.z) > Mathf.Abs(distance.x))
{
Debug.Log("Top");
pos = Vector3.forward;
MovePlayers();
return;
}
else if (distance.z < 0 && Mathf.Abs(distance.z) > Mathf.Abs(distance.x))
{
Debug.Log("Bot");
pos = Vector3.back;
MovePlayers();
return;
}
else if (distance.x < 0 && Mathf.Abs(distance.z) < Mathf.Abs(distance.x))
{
Debug.Log("Left");
pos = Vector3.left;
MovePlayers();
return;
}
else if (distance.x > 0 && Mathf.Abs(distance.z) < Mathf.Abs(distance.x))
{
Debug.Log("Right");
pos = Vector3.right;
MovePlayers();
return;
}
}
This is what I would like to achieve