// Update is called once per frame void Update() { // angle += 45 * Time.deltaTime; float distance = 5.5f; //Angle Target to rotate box Quaternion angleAxis = Quaternion.AngleAxis(-angle, Vector3.up); Vector3 angleDirection = transform.position + (transform.forward * distance); Vector3 angleTarget = RotateAroundPoint(angleDirection, transform.position, angleAxis); //Projection of the box Vector3 origin = transform.position; Vector3 halfExtents = Vector3.one / 2f; Vector3 direction = angleTarget - origin; //Transform.forward Quaternion orientation = Quaternion.LookRotation(direction); //Transform.rotation RaycastHit hitInfo; if (Physics.BoxCast(origin, halfExtents, direction, out hitInfo, orientation, distance)) { ExtDebug.DrawBoxCastOnHit(origin, halfExtents, orientation, direction, hitInfo.distance, Color.red); } ExtDebug.DrawBoxCastBox(origin, halfExtents, orientation, direction, distance, Color.green); }
private void CheckForInteractables() { Ray interactableRay = new Ray(transform.position, transform.forward); ExtDebug.DrawBoxCastBox(transform.position, boxcast_HalfExtends, transform.rotation, transform.forward, RAY_LENGTH, Color.red); RaycastHit hitPoint; if (Physics.BoxCast(transform.position, boxcast_HalfExtends, transform.forward, out hitPoint, transform.rotation, RAY_LENGTH, interactableMask)) { if (hitPoint.transform.GetComponent <InteractableObject>() != null) { InteractableObject observedObject = hitPoint.transform.GetComponent <InteractableObject>(); if (observedObject.IsLocked == false && HasAccessToInteract(observedObject)) { if (currentSelected == null) { Select(observedObject); } else if (currentSelected != observedObject) { DeSelect(currentSelected); Select(observedObject); } } return; } } if (currentSelected != null) { DeSelect(currentSelected); } }
void OnCollisionEnter2D(Collision2D collision) { //If Character Hits the Bottom of the Mystery Box Spawn Item of Correct Type ExtDebug.DrawBoxCastBox(transform.position, bounds, Quaternion.identity, Vector2.down, boxCastCheckDistance, Color.white); if (Physics2D.BoxCast(transform.position, bounds, 0, Vector2.down, boxCastCheckDistance, Player)) { if (collision.gameObject.CompareTag("Player") && boxActive) { switch (boxState) { case BoxState.coin: SpawnItem(0); break; case BoxState.mushroom: SpawnItem(1); break; case BoxState.fireFlower: SpawnItem(2); break; case BoxState.oneUp: SpawnItem(3); break; case BoxState.star: SpawnItem(4); break; } } } }
void OnCollisionEnter2D(Collision2D collision) { //Check if player collided with box and if player is not in small mario state destroy box ExtDebug.DrawBoxCastBox(transform.position, bounds, Quaternion.identity, Vector2.down, boxCastCheckDistance, Color.white); if (Physics2D.BoxCast(transform.position, bounds, 0, Vector2.down, boxCastCheckDistance, Player)) { if (collision.gameObject.CompareTag("Player") && characterController.characterState != CharacterController2Dimensional.CharacterState.smallMario) { Destroy(gameObject); } } }
bool ladderCheck() { RaycastHit2D[] rhit = new RaycastHit2D[256]; int size = Physics2D.BoxCast(transform.position, bx2d.bounds.extents, 0, Vector2.zero, filter2D, rhit); ExtDebug.DrawBoxCastBox(transform.position, bx2d.bounds.extents, Quaternion.identity, Vector2.zero, Mathf.Infinity, Color.red); if (size == 0) { return(false); } return(true); }
private void DrawRayCasts() { for (int x = 0; x < Box.ScaleX; x++) { for (int z = 0; z < Box.ScaleZ; z++) { Vector3 rayOrigin = new Vector3(Box.Min.x + x, Box.Max.y, Box.Min.z + z); ExtDebug.DrawBoxCastBox(rayOrigin, new Vector3(0.5f, 0.5f, 0.5f), Quaternion.identity, Vector3.down, 10f, Color.blue); } } }
public static RaycastWallHit GetWallHitBox(Vector3 origin, Vector3 direction, Vector3 extents, Quaternion orientation, float distance, bool debug = false) { RaycastHit hitInfo; if (debug) { ExtDebug.DrawBoxCastBox(origin, extents, orientation, direction, distance, Color.green); } if (Physics.BoxCast(origin, extents, direction, out hitInfo, orientation, distance, LayerMask.GetMask("Wall"))) { Wall wall = hitInfo.transform.GetComponent <Wall> (); if (wall != null) { return(new RaycastWallHit(wall, hitInfo.normal, hitInfo.point)); } } return(new RaycastWallHit()); }
} // End Move override public System.Nullable<RaycastHit> Cast(Vector3 direction, float distance) { bool hit; RaycastHit hitInfo; hit = Physics.BoxCast( hitInfo: out hitInfo, center: transform.position, halfExtents: castBoxSize, direction: direction, layerMask: collisionMask.constValue, orientation: transform.rotation, queryTriggerInteraction: QueryTriggerInteraction.Ignore, maxDistance: distance ); #if UNITY_EDITOR if(debugCasts.constValue) { ExtDebug.DrawBoxCastBox( origin: transform.position, halfExtents: castBoxSize, orientation: transform.rotation, direction: direction, distance: distance, duration: debugCastLifetime, color: (hit ? debugCastAttempt : debugCastMiss) ); if(hit) { ExtDebug.DrawBoxCastOnHit( origin: transform.position, halfExtents: castBoxSize, orientation: transform.rotation, direction: direction, hitInfoDistance: hitInfo.distance, duration: debugCastLifetime, color: debugCastHit ); } } #endif return new System.Nullable<RaycastHit>(hitInfo); } // End Cast
bool isAtTop() { RaycastHit2D[] rhit = new RaycastHit2D[256]; Vector3 lowerPosition = transform.position; lowerPosition.x -= bx2d.size.y / 4; Vector3 smallBounds = bx2d.bounds.extents; smallBounds.y /= 2; int size = Physics2D.BoxCast(lowerPosition, smallBounds, 0, Vector2.zero, filter2D, rhit); ExtDebug.DrawBoxCastBox(lowerPosition, smallBounds, Quaternion.identity, Vector2.zero, Mathf.Infinity, Color.blue); if (size == 0) { return(false); } return(true); }
bool IsGrounded() { ContactFilter2D filter = new ContactFilter2D(); filter.layerMask = terrainLayerMask; filter.useLayerMask = true; Vector2 box = coll2d.bounds.extents; box -= new Vector2(0.02f, 0); Vector2 lowerDown = coll2d.bounds.center; lowerDown.y -= coll2d.bounds.size.y; RaycastHit2D[] rhits = new RaycastHit2D[256]; int size = Physics2D.BoxCast(lowerDown, box, 0f, Vector2.zero, filter, rhits); ContactPoint2D[] point = new ContactPoint2D[256]; int contSize = rb2d.GetContacts(point); for (int i = 0; i < size; i++) { for (int j = 0; j < contSize; j++) { if (point[j].collider == rhits[i].collider) { //Debug.Log("Yes I am grounded!"); //Debug.Log(rhits[i].collider); return(true); } } } ExtDebug.DrawBoxCastBox(lowerDown, box, Quaternion.identity, Vector2.zero, 0, Color.red); return(false); }
} // End Cast override public RaycastHit[] CastAll(Vector3 direction, float distance) { RaycastHit[] hitInfoAr; hitInfoAr = Physics.BoxCastAll( center: transform.position, halfExtents: castBoxSize, direction: direction, layerMask: collisionMask.constValue, orientation: transform.rotation, queryTriggerInteraction: QueryTriggerInteraction.Ignore, maxDistance: distance ); #if UNITY_EDITOR if(debugCasts.constValue) { ExtDebug.DrawBoxCastBox( origin: transform.position, halfExtents: castBoxSize, orientation: transform.rotation, direction: direction, distance: distance, duration: debugCastLifetime, color: (hitInfoAr.Length > 0 ? debugCastAttempt : debugCastMiss) ); foreach(RaycastHit hitInfo in hitInfoAr) { ExtDebug.DrawBoxCastOnHit( origin: transform.position, halfExtents: castBoxSize, orientation: transform.rotation, direction: direction, hitInfoDistance: hitInfo.distance, duration: debugCastLifetime, color: debugCastHit ); } } #endif return hitInfoAr; } // End CastAll
public bool IsPositionAheadClear(Vector2 movementDir, float lookAhead) { float sizeScale = .5f; int layer = gameObject.layer; gameObject.layer = LayerMask.NameToLayer("Ignore Raycast"); RaycastHit2D hit = Physics2D.BoxCast(transform.position, new Vector2(boxCollider.size.x * transform.localScale.x * sizeScale, boxCollider.size.y * transform.localScale.y * sizeScale), 0, movementDir, lookAhead, mask); //ExtDebug.DrawBoxCastBox(transform.position, new Vector2(boxCollider.size.x * transform.localScale.x * sizeScale, boxCollider.size.y * transform.localScale.y * sizeScale), Quaternion.identity, movementDir, lookAhead, Color.green, .1f); if (hit.collider != null) { gameObject.layer = layer; hitCollider = hit.collider; Debug.DrawRay((Vector2)transform.position - (Vector2) new Vector2(boxCollider.size.x * transform.localScale.x * sizeScale, boxCollider.size.y * transform.localScale.y * sizeScale), 2 * new Vector2(boxCollider.size.x * transform.localScale.x * .9f, boxCollider.size.y * transform.localScale.y * .9f), Color.red, 1); //Debug.DrawLine(transform.position, hit.collider.gameObject.transform.position, Color.red, 1); ExtDebug.DrawBoxCastBox(transform.position, new Vector2(boxCollider.size.x * transform.localScale.x * sizeScale, boxCollider.size.y * transform.localScale.y * sizeScale), Quaternion.identity, movementDir, lookAhead, Color.red, 1); Debug.DrawLine(transform.position, hit.collider.gameObject.transform.position, Color.red, 1); return(false); } ExtDebug.DrawBoxCastBox(transform.position, new Vector2(boxCollider.size.x * transform.localScale.x * sizeScale, boxCollider.size.y * transform.localScale.y * sizeScale), Quaternion.identity, movementDir, lookAhead, Color.green, .1f); gameObject.layer = layer; return(true); }
void LateUpdate() { if (targetT != null && !buildingPlaced) { if (Input.GetKeyDown(KeyCode.Escape)) { Destroy(targetT.gameObject); buildingPlaced = true; targetT = null; return; } else if (Input.GetKeyDown(KeyCode.Comma)) { rot -= 90; } else if (Input.GetKeyDown(KeyCode.Period)) { rot += 90; } targetT.position = new Vector3(((int)PointAndClick.TerrainMouseLocation.x), (int)PointAndClick.TerrainMouseLocation.y, ((int)PointAndClick.TerrainMouseLocation.z)); targetT.rotation = Quaternion.Euler(90, rot, 0); ExtDebug.DrawBoxCastBox(new Vector3(targetT.position.x, targetT.position.y + 7, targetT.position.z), new Vector3(area.size.x / 2, 0.01f, area.size.y / 2), Quaternion.Euler(0, rot, 0), Vector3.down, 6.9f, new Color(0, 0, 1, 0.7f)); if (Physics.BoxCast(new Vector3(targetT.position.x, targetT.position.y + 7, targetT.position.z), new Vector3(area.size.x / 2, 0.01f, area.size.y / 2), Vector3.down, Quaternion.Euler(0, rot, 0), 6.9f)) { targetT.GetComponent <Renderer>().material.mainTexture = area.RedTex; } else { targetT.GetComponent <Renderer>().material.mainTexture = area.GreenTex; if (Input.GetMouseButtonDown(0)) { buildingPlaced = true; } } } }
void Crosshair() { Vector3 center = col.bounds.center; center.y += 1.0f; center.z += 4.0f; ExtDebug.DrawBoxCastBox(center, transform.localScale * 3f, transform.rotation, transform.forward * -1, 100f, Color.red); /* * if(m_HitDetect = Physics.BoxCast(col.bounds.center,transform.localScale * 3f, transform.forward * -1, out crossTrigger)){ * if(crossTrigger.collider.tag == "Meteor"){ * Vector3 crosshairScreen = Camera.main.WorldToScreenPoint(crosshair.transform.position); * Vector3 meteorScreen = Camera.main.WorldToScreenPoint(crossTrigger.collider.transform.position); * crosshairScreen.x = meteorScreen.x; * crosshairScreen.y = meteorScreen.y; * crosshair.transform.position = Vector3.Lerp(crosshair.transform.position, Camera.main.ScreenToWorldPoint(crosshairScreen),1.0f); * autoaim = true; * * } * } else { * crosshair.transform.localPosition = Vector3.Lerp(crosshair.transform.localPosition,constCrossPos,1.0f); * autoaim = false; * } */ }
public bool CheckGrounded() { //Check if Grounded ExtDebug.DrawBoxCastBox(transform.position, GetComponent <Collider2D>().bounds.extents - new Vector3(0.01f, 0.01f), Quaternion.identity, Vector2.down, groundedCheckHeight, Color.white); return(Physics2D.BoxCast(transform.position, GetComponent <Collider2D>().bounds.extents - new Vector3(0.01f, 0.01f), 0, Vector2.down, groundedCheckHeight, groundLayer)); }
// jam horror bool IsGroundedButLeft(bool isItActuallyLeft) { ContactFilter2D filter = new ContactFilter2D(); filter.layerMask = terrainLayerMask; filter.useLayerMask = true; Vector2 box = coll2d.bounds.extents; box += new Vector2(0, 0.02f) * (isItActuallyLeft ? -1.0f : 1.0f); Vector2 leftUp = coll2d.bounds.center; Vector2 leftDown = coll2d.bounds.center; leftUp.x += coll2d.bounds.size.x * (isItActuallyLeft ? -1.0f : 1.0f); leftDown.x += coll2d.bounds.size.x * (isItActuallyLeft ? -1.0f : 1.0f); leftUp.y -= coll2d.bounds.size.y; leftDown.y += coll2d.bounds.size.y; { RaycastHit2D[] rhits = new RaycastHit2D[256]; int size = Physics2D.BoxCast(leftUp, box, 0f, Vector2.zero, filter, rhits); ContactPoint2D[] point = new ContactPoint2D[256]; int contSize = rb2d.GetContacts(point); for (int i = 0; i < size; i++) { for (int j = 0; j < contSize; j++) { if (point[j].collider == rhits[i].collider) { Debug.Log("Yes I am grounded!"); Debug.Log(rhits[i].collider); return(true); } } } } { RaycastHit2D[] rhits = new RaycastHit2D[256]; int size = Physics2D.BoxCast(leftDown, box, 0f, Vector2.zero, filter, rhits); ContactPoint2D[] point = new ContactPoint2D[256]; int contSize = rb2d.GetContacts(point); for (int i = 0; i < size; i++) { for (int j = 0; j < contSize; j++) { if (point[j].collider == rhits[i].collider) { Debug.Log("Yes I am grounded!"); Debug.Log(rhits[i].collider); return(true); } } } } ExtDebug.DrawBoxCastBox(leftUp, box, Quaternion.identity, Vector2.zero, 0, Color.green); ExtDebug.DrawBoxCastBox(leftDown, box, Quaternion.identity, Vector2.zero, 0, Color.green); return(false); }