Trouble with box colliders

Right so I’ve been trying to figure out how to do this for a while now and I’m getting pretty confused and frustrated.

In the picture I have a cube which has a player controller script attached. The box colliders surrounding it are child empty game objects to the cube. Each one has it’s own script to detect whether it is touching a box collider within the white cube. The white cross is made up of individual cubes each with its own box collider attached. What the player colliders are trying to do is detect whether it is touching a box collider of the floor, if it is then it lets the player move. However the way I have done it might have been a bit wrong. Here is the script attached to the box collider at the front lower side of the cube:

using UnityEngine;
using System.Collections;

public class ColliderFB : MonoBehaviour {
	void OnTriggerEnter(Collider bcFloor)
	{
		CubeController.fwd = 1;
	}
}

This box collider attached to the floor gameobject is bcFloor and it detects it fine however once it detects it then CubeController.fwd is always set to 1 which means even if the two box colliders aren’t colliding anymore then it is still = 1 which is not what I need. Is there a way to detect if the two box colliders are no longer touching? I’ve tried OnTriggerExit but this just causes the player cube to not move. Here is how I instigate it into my PlayerController script.

	public static float fwd;
	public static float fwd2;

	void Update ()
	{
		if (Input.GetButtonDown ("VerticalFwd"))
		{
			MoveForward();
		}
}

	void MoveForward ()
	{
		if (fwd == 1  fwd2 == 0) {
			amountToMove.y = 1f;
			transform.Translate (amountToMove);
		} else {

		}
	}

fwd2 is the box collider detecting any colliders directly infront of the player. fwd is detecting them infront -1.
So if there is a collision in the BoxCollider script then fwd = 1 in the PlayerController script and the player can only move if fwd = 1. What I’ve been trying to do is check if in the BoxCollider script the collision is no longer happening, therefore fwd = 0 not allowing the player to move in that direction any longer until there is a collision between the two collider again, ie. the player moves back causing the colliders to collide again. Any pointers would be helpful, if I’m going about all this completely incorrectly then let me know. I’m using all the box colliders because I’m not a very logical person and it seemed like the most logical way to achieve what I have in mind.

1454380--78859--$coliders.png

There’ s a lot of text there to work through so maybe I missed something, but did you try using OnTriggerStay? That should fire for every frame the trigger is overlapping the collider. That seems to be closer to what you want that trying to track OnTriggerExit/OnTriggerEnter yourself.

Is PlayerController your own custom character controller? Usually, you write a controller to always allow the player to move and then react to collisions. You calculate where your character wants to move, and then use the physics system to check for collisions in a volume. If you detect collisions, then you handle them (sliding the character around obstacles, bouncing, riding moving platforms, applying forces to objects you hit, etc.).

In general, there are two approaches to writing character controller: kinematic and dynamic rigidbody. A kinematic controller is where you move the character manually and fake physics. The dynamic one is where you simply use a rigidbody under control of physics and try to constrain the movement to give your player control. Which one you use depends on your scenario. A 2D platformer with precise controls would usually choose a kinematic one whereas a spaceships with thrusters bouncing around an asteroid field would likely choose a rigidbody controller.

It looks to me like you’re building a kinematic controller? Is there a specific reason you aren’t using the default character controller? It’s not great but it’s got a lot of functionality to build upon if it works for you.

I’ve got it sorted, was relatively simple considering how long I was whacking my brains about it.
Here’s the script I’ve got attached to each box collider attached to the player object. Obviously the var changes with each collider

void OnTriggerStay(Collider bcFloor)
	{
		if (bcFloor.collider.name == "bcFloor")
		{
			//Debug.Log("Working");
			CubeController.right = 1;
		}
	}
	
	void OnTriggerExit(Collider bcFloor)
	{
		//Debug.Log ("Exit");
		CubeController.right = 0;
	}

It works nicely now, the OnTriggerStay only runs when the two colliders are hitting. When they aren’t anymore OnTriggerExit runs once, then when the two collider colide again then OnTriggerStay starts running again. Working exactly how I wanted it to :slight_smile:
Thanks for the pointer.