tl;dr - I’m making a block puzzle game with so far their being 2 different blocks (Horizontal & Vertical) and I’m trying to disable Blocks two way movement when something is in it’s way. I’m using both detection from the block’s collider (left side) and another collider set as a trigger (right side) to detect either side when it’s blocked. For some reason the “OnTriggerEnter/OnColiderEnter” on either separate script is setting off the bools in both separate scripts and I don’t know why as they shouldn’t be communicating.
I’m new to game development and making a simple block puzzle game. Right now I just have 2 block types, one that can move left n right, the other up n down. I’ve been trying to figure out how to get my mouse input to disable when a block is, well blocked. It’s in 3D with I think is the correct code for 3D.
Each block type has it’s own script (so 2 different scripts for the 2 types of blocks so far), their bools are different names, I don’t have anything referencing or calling each others scripts either. The scripts are similar, but with directions (Vector3.up or Vector3.right) and variable name changes (rightBlocked or upBlocked).
I need to find out which side is blocked and to then disable that sides mouse input to then disable movement. How I’m currently doing this is by having a normal box collider along with another box collider trigger on each block. (I’ve seen around it’s not good to put more than one collider on an object but I couldn’t find any other way, but I’m open to totally changing this if there is a much better simpler way of multi detection)
So, as an example, the left side of the Horizontal Block uses OnColliderEnter to detect when its blocked, while the right side uses OnTriggerEnter to detect the right side when it’s blocked. the two colliders are not touching each other.
When these methods detect being blocked, they set bools in the same script to “true” thus not allowing the If statements to activate that houses the mouse input.
PROBLEM: So things were working fine so far, but then I noticed the “OnTriggerEnter/OnColiderEnter” in the Horizontal Block set off it’s own bools (like it’s suppose to) but also set off the Vertical Block’s bools aswell. And vice versa. This is confusing to me as from what I can tell there shouldn’t be any communication between these two separate script that are on separate Objects with their bools being named different and everything.
I have tried setting all the bools and “On____Enter” methods to private and didn’t find a difference.
Thanks for any help!
SCRIPT:
- HORIZONTAL BLOCK -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HorBlockClick : MonoBehaviour
{
private float speed = 2;
public bool rightBlocked = false;
public bool leftBlocked = false;
void Start()
{
}
void Update()
{
if (!leftBlocked && Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag == "HorBlock")
{
if (hit.collider.gameObject == this.gameObject)
{
transform.Translate(Vector2.left * speed);
rightBlocked = false;
}
}
}
}
if (!rightBlocked && Input.GetMouseButtonDown(1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag == "HorBlock")
{
if (hit.collider.gameObject == this.gameObject)
{
transform.Translate(Vector2.right * speed);
leftBlocked = false;
}
}
}
}
}
//check left side
void OnCollisionEnter(Collision left)
{
leftBlocked = true;
}
//check right side
void OnTriggerEnter(Collider right)
{
rightBlocked = true;
}
}
- VERTICAL BLOCK -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VerBlockClick : MonoBehaviour
{
private float speed = 2f;
public bool topBlocked = false;
public bool bottomBlocked = false;
void Start()
{
}
void Update()
{
if (!topBlocked && Input.GetAxis("Mouse ScrollWheel") > 0f)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag == "VerBlock")
{
if (hit.collider.gameObject == this.gameObject)
{
transform.Translate(Vector2.up * speed);
bottomBlocked = false;
}
}
}
}
if (!bottomBlocked && Input.GetAxis("Mouse ScrollWheel") < 0f)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag == "VerBlock")
{
if (hit.collider.gameObject == this.gameObject)
{
transform.Translate(Vector2.down * speed);
topBlocked = false;
}
}
}
}
}
//check bottom
void OnCollisionEnter(Collision bottom)
{
bottomBlocked = true;
}
//check top
void OnTriggerEnter(Collider top)
{
topBlocked = true;
}
}