public override void CharacterUpdate() { if ((transform.position - king.transform.position).magnitude < runRadius) { // The tax collector should raycast in all 4 directions and go in // whichever direction has the most space and is away from the king float[] distances = new float[4]; int indexOfGreatest = 0; for (int i = 0; i < 4; i++) { // if the ray direction is close to the king's direction //then continue to the next direction Vector3 kingPos = king.gameObject.transform.position; MovDir rayDir = (MovDir)i; MovDir kingDir; MovDir dontUseThis; Mover.WaypointDirection( this, kingPos, out kingDir, out dontUseThis); if (rayDir == kingDir) { distances[i] = 0; continue; } RaycastHit rayInfo; LayerMask defaultLayer = LayerMask.NameToLayer("Default"); Physics.Raycast( transform.position + rayStartingHeight, rayDir.Vector(), out rayInfo, 200, 1); distances[i] = rayInfo.distance; if (rayInfo.distance > distances[indexOfGreatest] || rayInfo.distance == 0) { indexOfGreatest = i; } } Mover.MoveCharacter( this, out charInTheWay, true, (MovDir)indexOfGreatest, Physics.DefaultRaycastLayers); } }
public static bool MoveCharacter( Character sourceCharacter, out Character hitCharacter, bool pushHitCharacter, MovDir direction, LayerMask layerMask) { hitCharacter = null; // set sourceCharacter.currDir to direction or else characters // won't be able to push other characters sourceCharacter.currDir = direction; if (direction == MovDir.NONE) { return(false); } layerMask &= ~LayerMask.GetMask("Corpses"); //Do raycast Vector3 dir = direction.Vector(); Vector3 sourcePos = sourceCharacter.transform.position + sourceCharacter.rayStartingHeight; RaycastHit rayInfo; Physics.Linecast(sourcePos, sourcePos + dir, out rayInfo, layerMask); //Debug.DrawLine(sourcePos, sourcePos + dir, Color.red); //If we hit something, check if it blocks our path bool isBlocked = rayInfo.transform != null; if (isBlocked) { //Check if the blocking thing is a character hitCharacter = rayInfo.transform.gameObject.GetComponent <Character>(); if (hitCharacter == null) { return(false); } //If the function cant interact with objects then ignore the hit object and dont move, if it can, do an interact if (!pushHitCharacter) { return(false); } //If the character we interact with tells us that we shouldnt move, //we return false and we dont move //if it doesnt, then we move if (!hitCharacter.Interact(sourceCharacter)) { return(false); } } //If the way is clear, then move sourceCharacter and return true //The movements should ONLY be done in 1 unit increment //ie: I move right so dir = 1,0,0 sourceCharacter.prevModelPos = sourceCharacter.transform.position; sourceCharacter.transform.position += dir; sourceCharacter.MoveModelToPos(sourceCharacter.transform.position); sourceCharacter.currDir = MovDir.NONE; return(true); }