private bool CheckWorldCollisions(WorldChanger.WorldState worldState) { Vector3 colliderExtents = transform.lossyScale * .49f; // Evaluate the world state that player is transferring to. switch (worldState) { // cast present ray & evaluate case WorldChanger.WorldState.Present: Vector3 presentPos = new Vector3(transform.position.x, transform.position.y, 0); if (Application.isEditor) { ExtDebug.DrawBox(presentPos, colliderExtents, transform.rotation, Color.blue); } if (currentWorldState != WorldChanger.WorldState.Present && Physics.CheckBox(presentPos, colliderExtents, transform.rotation, Layers.Players)) { //Debug.Log("Colliding present..."); return(false); } break; // cast past ray & evaluate case WorldChanger.WorldState.Past: Vector3 pastPos = new Vector3(transform.position.x, transform.position.y, 50); if (Application.isEditor) { ExtDebug.DrawBox(pastPos, colliderExtents, transform.rotation, Color.blue); } if (currentWorldState != WorldChanger.WorldState.Past && Physics.CheckBox(pastPos, colliderExtents, transform.rotation, Layers.Players)) { //Debug.Log("Colliding past..."); return(false); } break; // cast future ray & evaluate case WorldChanger.WorldState.Future: Vector3 futurePos = new Vector3(transform.position.x, transform.position.y, 100); if (Application.isEditor) { ExtDebug.DrawBox(futurePos, colliderExtents, transform.rotation, Color.blue); } if (currentWorldState != WorldChanger.WorldState.Future && Physics.CheckBox(futurePos, colliderExtents, transform.rotation, Layers.Players)) { //Debug.Log("Colliding future..."); return(false); } break; } //Debug.Log("No Collide..."); return(true); }
public void RotateForDiscover() { Quaternion tempR = Quaternion.LookRotation(transform.forward); tempR = tempR * Quaternion.Euler(0, RotateNumber, 0); Vector3 tempD = tempR * Vector3.forward; hits = Physics.BoxCastAll(gameObject.transform.position + new Vector3(0, 1, 0), new Vector3(EnemySight, EnemySight, 0), tempD, Quaternion.identity, VisibilityDistance); ExtDebug.DrawBox(gameObject.transform.position + new Vector3(0, 1, 0), new Vector3(EnemySight, EnemySight, 0), tempR, Color.yellow); StartCoroutine(ChangeNumberInTime(0.2f)); }
//Check if a potential spot is too close to other objects public static int TooClose(Vector3 spot, GameObject mat, LayerMask lm) { //Quaternion ang = mat.transform.rotation; //ang = new Quaternion(ang.x, Random.Range(0, 360), ang.z, ang.w); Collider[] obstCheck = Physics.OverlapBox(spot, mat.GetComponent <BasicThrown>().size / 2, new Quaternion(), ~lm); //if (obstCheck.Length > 0) // ang = new Quaternion(); ExtDebug.DrawBox(spot, mat.GetComponent <BasicThrown>().size / 2, new Quaternion(), Color.red, 0.5f); return(obstCheck.Length); }
private void OnDrawGizmos() { Vector2 rightPos = (Vector2)transform.position + new Vector2(xWidth + centerXOffset, -0.06f); Vector2 leftPos = (Vector2)transform.position + new Vector2(-xWidth + centerXOffset, -0.06f); Vector2 bottomPos = (Vector2)transform.position + new Vector2(centerXOffset, yWidth + centerYOffset); ExtDebug.DrawBox(rightPos, new Vector2(rightWallDist / 2, rightWidth / 2), Quaternion.identity, Color.blue); ExtDebug.DrawBox(leftPos, new Vector2(leftWallDist / 2, leftWidth / 2), Quaternion.identity, Color.green); ExtDebug.DrawBox(bottomPos, new Vector2(bottomWidth / 2, floorDist / 2), Quaternion.identity, Color.cyan); //UnityEditor.Handles.Label((Vector2)transform.position + Vector2.up * 0.7f, OnAnyWall ? "On Wall" : "Not On Wall"); //UnityEditor.Handles.Label((Vector2)transform.position - new Vector2(centerXOffset, centerYOffset), OnGround ? "On Ground" : "Not On Ground"); }
void InteractionRaycast() { Collider[] colls; Vector3 pos = this.transform.position + transform.forward * box.center.z; ExtDebug.DrawBox(pos + transform.forward * RaycastLength, BoxSize / 2, this.transform.rotation, Color.black); colls = Physics.OverlapBox(pos + transform.forward * RaycastLength, BoxSize / 2, this.transform.rotation, ~(1 << LayerMask.NameToLayer("Player"))); if (colls.Length > 0) //Uhh this whole thing should run in O(n)? i think the way i set up the overlap box itself the max length <= 10? so shouldn't be too resource consuming { for (int i = 0; i < colls.Length; ++i) { if (colls[i].gameObject.GetComponent <Animal>() != null) { PickupAnimal(colls[i].gameObject); return; } } for (int i = 0; i < colls.Length; ++i) { if (colls[i].gameObject.GetComponent <Interactable>() != null) { colls[i].gameObject.GetComponent <Interactable>().Interact(); return; } } for (int i = 0; i < colls.Length; ++i) { if (colls[i].gameObject.GetComponent <HeldObject>() != null) { PickupObject(colls[i].gameObject); return; } } for (int i = 0; i < colls.Length; ++i) { if (colls[i].gameObject.GetComponent <ResourceSource>() != null) { CollectResource(colls[i].gameObject); return; } } } }
/* * * switch (FacingDirection) * { * case Directions.backwards: * ObjectInDirection.y = 0; * break; * case Directions.forward: * ObjectInDirection.y = 0; * break; * case Directions.left: * ObjectInDirection.x = 0; * break; * case Directions.right: * ObjectInDirection.x = 0; * break; * case Directions.b_left: // (transform.right + transform.forward).normalized * Debug.Log(FacingDirection.ToString()); * if (DirectionalRaycast((transform.right + transform.forward).normalized, Color.red)) * { * HitObject = true; * ObjectInDirection.x = 0; * Debug.Log("Hit Horizontally"); * } * if (DirectionalRaycast(((transform.right * -1) + transform.forward).normalized, Color.cyan)) * { * HitObject = true; * ObjectInDirection.y = 0; * Debug.Log("Hit vertically"); * } * * if (!HitObject) * { * if (DirectionalRaycast(transform.forward, Color.black)) * { * Debug.Log("Hit diagonally"); * ObjectInDirection = Vector2.zero; * } * } * * break; * case Directions.b_right: * Debug.Log(FacingDirection.ToString()); * if (DirectionalRaycast(((transform.right * -1) + transform.forward).normalized, Color.red)) * { * HitObject = true; * ObjectInDirection.x = 0; * Debug.Log("Hit Horizontally"); * } * if (DirectionalRaycast((transform.right + transform.forward).normalized, Color.cyan)) * { * HitObject = true; * ObjectInDirection.y = 0; * Debug.Log("Hit vertically"); * } * * if (!HitObject) * { * if (DirectionalRaycast(transform.forward, Color.black)) * { * Debug.Log("Hit diagonally"); * ObjectInDirection = Vector2.zero; * } * } * * break; * case Directions.f_left: * Debug.Log(FacingDirection.ToString()); * if (DirectionalRaycast(((transform.right * -1) + transform.forward).normalized, Color.red)) * { * HitObject = true; * ObjectInDirection.x = 0; * Debug.Log("Hit Horizontally"); * } * if (DirectionalRaycast((transform.right + transform.forward).normalized, Color.cyan)) * { * HitObject = true; * ObjectInDirection.y = 0; * Debug.Log("Hit vertically"); * } * * if (!HitObject) * { * if (DirectionalRaycast(transform.forward, Color.black)) * { * Debug.Log("Hit diagonally"); * ObjectInDirection = Vector2.zero; * } * } * break; * case Directions.f_right: * Debug.Log(FacingDirection.ToString()); * if (DirectionalRaycast((transform.right + transform.forward).normalized, Color.red)) * { * HitObject = true; * ObjectInDirection.x = 0; * Debug.Log("Hit Horizontally"); * } * if (DirectionalRaycast(((transform.right * -1) + transform.forward).normalized, Color.cyan)) * { * HitObject = true; * ObjectInDirection.y = 0; * Debug.Log("Hit vertically"); * } * * if (!HitObject) * { * if (DirectionalRaycast(transform.forward, Color.black)) * { * Debug.Log("Hit diagonally"); * ObjectInDirection = Vector2.zero; * } * } * * break; * } */ #endregion bool DirectionalRaycast(Vector3 dir, Color col, bool isDiagonal = false) { Collider[] colls; Vector3 pos = GetPosToBoxCastFrom() + Vector3.down * 0.7f; Vector3 size = (dir == Vector3.right || dir == Vector3.right * -1 ? new Vector3((isHoldingObject ? 0.8f : 0.6f), 0.1f, 0.1f) : new Vector3(0.1f, 0.1f, (isHoldingObject ? 0.8f : 0.6f))); float longerRay = RaycastLength; ExtDebug.DrawBox(pos + dir * (isDiagonal ? Mathf.Sqrt(Mathf.Pow(longerRay, 2)) : longerRay), size, (isDiagonal ? this.transform.rotation : Quaternion.identity), col); colls = Physics.OverlapBox(pos + dir * (isHoldingObject ? 2f : 1f) * (isDiagonal ? Mathf.Sqrt(2 * Mathf.Pow(longerRay, 2)) : longerRay), size, Quaternion.identity, ~(1 << LayerMask.NameToLayer("Player"))); if (colls.Length > 0) { return(true); } return(false); }
//private void OnCollisionEnter(Collision collision) //{ // ContactPoint[] contacts = new ContactPoint[10]; // collision.GetContacts(contacts); // foreach(var hit in contacts) // { // HandleParkour(collision, hit); // } //} private void OnTriggerStay(Collider other) { if (!StartedRegJump) { var point = other.ClosestPoint(transform.position); ExtDebug.DrawBox(point, Vector3.one / 2, Quaternion.identity, Color.blue); var dir = point - transform.position; RaycastHit hit; Debug.DrawLine(transform.position, point, Color.white); if (Physics.Raycast(transform.position, dir, out hit, wallDist, wallLayer, QueryTriggerInteraction.Ignore) && !playerController.Grounded) { Debug.DrawRay(point, hit.normal, Color.green); HandleParkour(other.gameObject, hit); } } }
private void CheckPath(Vector3 position, Vector3 target) { Quaternion rotation = Quaternion.LookRotation(target - position); Vector3 direction = target - position; float distance = Vector3.Distance(position, target); Vector3 positionz = Vector3.Lerp(position, target, 0.5f); Vector3 halfExtents = new Vector3(1.6f, 2.6f, (target - position).magnitude) / 2; ExtDebug.DrawBox(positionz, halfExtents, rotation, Color.green); // Debug.DrawRay(position, direction, result ? Color.green : Color.red); RaycastHit[] rhit = Physics.BoxCastAll(positionz, halfExtents, direction, rotation, distance); foreach (GameObject cube in colored) { if (!cube) // not valid { colored.Remove(cube); continue; } if (cube.layer != 12) { if (cube.GetComponent <Renderer>() && cube.tag == "Moonrock") { Renderer rendererToEdit = cube.GetComponent <Renderer>(); foreach (Material mat in rendererToEdit.materials) { //mat.color = Color.green; cube.layer = 0; } } else if (cube.tag == "Untagged") { cube.layer = 0; } } } colored.Clear(); foreach (RaycastHit uhit in rhit) { GameObject cube = uhit.collider.gameObject; if (cube.layer != 12) { if (cube.GetComponent <Renderer>() && cube.tag == "Moonrock") { Renderer rendererToEdit = cube.GetComponent <Renderer>(); foreach (Material mat in rendererToEdit.materials) { //mat.color = Color.red; cube.layer = 8; } colored.Add(cube); } else if (cube.tag == "Untagged") { cube.layer = 8; colored.Add(cube); } } } }
void GeneralRaycast() { Collider[] colls; Crossable cross; Pen pen; Vector3 pos = GetPosToBoxCastFrom(); Vector3 size = GetSizeOfBox(pos); colls = Physics.OverlapBox(pos + transform.forward * RaycastLength, size, this.transform.rotation, ~(1 << LayerMask.NameToLayer("Player") | 1 << LayerMask.NameToLayer("Animal"))); ExtDebug.DrawBox(pos + transform.forward * RaycastLength, size, this.transform.rotation, Color.black); CheckIfEnterOrLeftPen(colls); bool obstruction = false; if (colls.Length > 0) { for (int i = 0; i < colls.Length; ++i) { cross = colls[i].gameObject.GetComponent <Crossable>(); pen = colls[i].gameObject.GetComponent <Pen>(); if ((cross == null || !cross.IsOpen()) && pen == null) { UpdateObjectInDirection(colls[i].gameObject); obstruction = true; } } if (!obstruction) { UpdateObjectInDirection(); } /*if(colls.Length == 1) * { * cross = colls[0].gameObject.GetComponent<Crossable>(); * if ((cross != null && cross.IsOpen())) * { * UpdateObjectInDirection(); * return; * } * * pen = colls[0].gameObject.GetComponent<Pen>(); * if (pen != null) * { * UpdateObjectInDirection(); * return; * } * * * UpdateObjectInDirection(colls[0].gameObject); * * } * else if(colls.Length == 2) * { * cross = colls[0].gameObject.GetComponent<Crossable>(); * pen = colls[1].gameObject.GetComponent<Pen>(); * * if((cross != null && cross.IsOpen()) && pen != null) * { * UpdateObjectInDirection(); * return; * } * * cross = colls[1].gameObject.GetComponent<Crossable>(); * pen = colls[0].gameObject.GetComponent<Pen>(); * * if ((cross != null && cross.IsOpen()) && pen != null) * { * UpdateObjectInDirection(); * return; * } * * UpdateObjectInDirection(colls[0].gameObject); * UpdateObjectInDirection(colls[1].gameObject); * * } * else * { * * for(int i = 0; i < colls.Length; ++i) * { * cross = colls[i].gameObject.GetComponent<Crossable>(); * pen = colls[i].gameObject.GetComponent<Pen>(); * if (cross == null && pen == null) * { * UpdateObjectInDirection(colls[i].gameObject); * } * } * } * * //Checks if the object it collided with is something that can be crossed or if the player is inside the pen */ } else { UpdateObjectInDirection(); } }
private Collider[] GetCollider() { ExtDebug.DrawBox(center, new Vector3(1.6f, 0.6f, 2.2f), Quaternion.Euler(Utils.rotateRotation(rotation, colliderRotation.eulerAngles)), Color.red, 10f); return(Physics.OverlapBox(center, new Vector3(1.6f, 0.6f, 2.2f), Quaternion.Euler(Utils.rotateRotation(rotation, colliderRotation.eulerAngles)), buildableMask)); }