private void OnCollisionEnter2D(Collision2D collision) { if (Time.timeScale == 0) { return; } int casthits = 0; try { casthits = castingCollider.Cast(new Vector2(0, -1), raycastHit2s, 1); } catch { } int i = 0; while (i < casthits) { rayHit = raycastHit2s[i]; if (collision.gameObject.tag == "Ground" && System.Math.Abs(rayHit.point.y - rayHit.centroid.y) < 0.25) { JumpReady1 = true; JumpReady2 = true; Debug.Log("Jump Reset by Enter2D"); colliding = true; } i++; } }
// Update is called once per frame void Update() { if (explodesAtAll && chargesLeft > 0) { if (explodesUponContact) { bool validTrigger = false; RaycastHit2D[] rch2ds = new RaycastHit2D[10]; cc2D.Cast(Vector2.zero, rch2ds, 0, true); foreach (RaycastHit2D rch2d in rch2ds) { if (rch2d && !rch2d.collider.isTrigger && rch2d.collider.gameObject.GetComponent <Rigidbody2D>() != null) { validTrigger = true; break; } } if (validTrigger) { Debug.Log("Collision!"); if (chargeTime >= fta.maxHoldTime) { trigger(); } } } if (chargesAutomatically) { charge(Time.deltaTime); } } }
// Update is called once per frame void Update() { if (player.IsFrozen()) { return; } int hit = playerCol.Cast(new Vector2(0, -1), results); RaycastHit2D result = results[0]; for (int i = 0; i < hit; ++i) { if (results[i].collider.gameObject.tag == "Candy-Block" || results[i].collider.gameObject.tag == "Boundary-Floor") { result = results[i]; break; } } transform.position = result.point + result.normal * playerCol.radius * player.transform.localScale; an.SetInteger("Colour", playerAn.GetInteger("Colour")); an.SetBool("Slamming", playerAn.GetBool("Slamming")); an.SetBool("Eating", playerAn.GetBool("Eating")); float interp = (player.transform.position.y - transform.position.y) / (player.max_height - transform.position.y); charRenderer.material.color = new Color(1, 1, 1, Mathf.Lerp(0, 1, interp)); flameRenderer.material.color = new Color(1, 1, 1, Mathf.Lerp(0, 1, interp)); }
private void OnTriggerStay2D(Collider2D collision) { if (collision.tag == "Player") { if (!opened) { int targetCount = cc.Cast(Vector2.zero, nearbyObjs); if (nearbyObjs != null) { for (int i = 0; i < targetCount && i < nearbyObjs.Length; i++) { RaycastHit2D hit = nearbyObjs[i]; PlayerControls tempChk = hit.transform.GetComponent <PlayerControls>(); PlayerControls.pc.canOpen = true; } } } } }
//These methods are used to track the WallJumpStatus of the Player, so we don't apply the force when we want to wall jump. //They are functionally the same as the ones found in WallJump.cs private void OnCollisionEnter2D(Collision2D collision) { //Reset HasHappened //make sure a regular jump ins't possible int casthits = castingCollider.Cast(new Vector2(0, -1), raycastHit2s, 1); int i = 0; while (i < casthits) { rayHit = raycastHit2s[i]; if (collision.gameObject.tag == "Ground" && rayHit.fraction < 0.25) { JumpPossible = true; } i++; } casthits = 0; //Right casthits = castingCollider.Cast(new Vector2(1, 0), raycastHit2s, 1); i = 0; while (i < casthits) { rayHit = raycastHit2s[i]; if (collision.gameObject.tag == "Ground" && rayHit.fraction < 0.25) { WallJumpRightReady = true; } i++; } //Left casthits = castingCollider.Cast(new Vector2(-1, 0), raycastHit2s, 1); i = 0; while (i < casthits) { rayHit = raycastHit2s[i]; if (collision.gameObject.tag == "Ground" && rayHit.fraction < 0.25) { WallJumpLeftReady = true; } i++; } }
private bool openDirection(Vector2 direction) { RaycastHit2D[] rch2ds = new RaycastHit2D[10]; cc2d.Cast(direction, rch2ds, 1f, true); foreach (RaycastHit2D rch2d in rch2ds) { if (rch2d && rch2d.collider.gameObject.tag == "Level") { return(false); } } return(true); }
// Check if a position is clear of other players bool IsPosClear(Vector3 position) { GameObject tempObj = new GameObject("Temp Obj"); tempObj.transform.position = position; CircleCollider2D collider = tempObj.AddComponent <CircleCollider2D>(); collider.isTrigger = true; collider.radius = spawnClearRadius; RaycastHit2D[] results = new RaycastHit2D[1]; bool isPosClear = (collider.Cast(Vector2.zero, results) == 0); Destroy(tempObj); return(isPosClear); }
public bool isBeingTriggered() { int count = cc2D.Cast(Vector2.zero, rh2ds, 0, true); if (count <= 0) { return(false); } for (int i = 0; i < count; i++) { if (!rh2ds[i].collider.isTrigger) { return(true); } } return(false); }
private void FindEnemies() { RaycastHit2D[] hits = null; int count = combatCollider.Cast(new Vector2(0, 0), hits); for (int i = 0; i < count; i++) { Ship ship = hits [i].collider.gameObject.GetComponent <Ship> (); if (ship.faction != cachedRoot.faction) { if (!targets.Contains(ship)) { Debug.Log("ADD " + ship.name); targets.Add(ship); ship.RegisterListener(this); } } } }
public bool CollideCheck() { if (phys.state[1].magnitude == 0f) { return(false); } RaycastHit2D[] hit = new RaycastHit2D[50]; if (mycollider.Cast(phys.state[1], hit, phys.state[1].magnitude * Time.deltaTime, false) > 0) { foreach (RaycastHit2D item in hit) { collide(item); } return(true); } return(false); }
/// <summary> /// This function used to check the passed in direction is valid or not /// </summary> private bool checkDirValid(Vector2 dir) { //stores the game objects that hits by the collider shape casted by the circle collider RaycastHit2D[] hits = new RaycastHit2D[10]; //The distance for the cast float distance = 1; //Cast a collider shape into the scene cirColl.Cast(dir, hits, distance, true); //If the array has 'wall' objects, the dir is not valid and return false foreach (RaycastHit2D hit in hits) { if (hit && hit.collider.gameObject.tag == "wall") { return(false); } } return(true); }
// Check if a position is clear for enemy spawning private bool IsPosClear(Vector3 position) { // Create a temporary object with a circle collider to check an area GameObject tempObj = new GameObject("Temp Obj"); tempObj.transform.position = position; CircleCollider2D collider = tempObj.AddComponent <CircleCollider2D>(); collider.isTrigger = true; collider.radius = spawnClearRadius; // Cast collider at the position RaycastHit2D[] results = new RaycastHit2D[1]; bool isPosClear = (collider.Cast(Vector2.zero, results) == 0); Destroy(tempObj); return(isPosClear); }
//check the direction is able to change to or not private bool checkValidDir(Vector2 direction) { RaycastHit2D[] rch2ds = new RaycastHit2D[10]; //cast collider to see if anything at front of the ghost direction //store an RayCastHit2D object into the array if there are something in the way float dist = 1; cirColli2d.Cast(direction, rch2ds, dist, true); foreach (RaycastHit2D rch2d in rch2ds) { //if has object, and the object's tag is Wall, change direction, otherwise nullpointer if (rch2d && rch2d.collider.gameObject.tag == "Wall") { return(false); } } return(true); }
private void Update() { Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); if (input.sqrMagnitude > 1f) { input.Normalize(); } Vector3 direction = new Vector3(input.x, input.y, 0f); float distance = MovementSpeed * Time.deltaTime; Vector3 motion = direction * distance; int hitCount = collider.Cast(direction, hitBuffer, distance); if (hitCount > 0) { var hit = hitBuffer[0]; motion = direction * hit.distance; } transform.position += motion; }
private void UpdateAllowedMovementDirection() { // if moving left but were blocked, check if we are free now RaycastHit2D[] hits = new RaycastHit2D[1]; //if (m_Rigidbody2D.velocity.y >= 0) { //if (m_ShouldMoveLeft && !m_CanMoveLeft && m_BoxCollider2D.Cast(Vector2.left, hits, .1f) < 1) if (m_ShouldMoveLeft && !m_CanMoveLeft && m_CircleCollider2D.Cast(Vector2.left, hits, .1f) < 1) { m_CanMoveLeft = true; } else if (m_ShouldMoveLeft && !m_CanMoveLeft) { foreach (var hit in hits) { if (hit.transform.tag == "Hazard") { m_CanMoveLeft = true; break; } } } /*} * else { * if (m_ShouldMoveLeft && !m_CanMoveLeft && m_EdgeCollider2D.Cast(Vector2.left, hits, .1f) < 1) * m_CanMoveLeft = true; * else if (m_ShouldMoveLeft && !m_CanMoveLeft) { * foreach (var hit in hits) { * if (hit.transform.tag == "Hazard") { * m_CanMoveLeft = true; * break; * } * } * } * }*/ //if (m_Rigidbody2D.velocity.y >= 0) { //if (m_ShouldMoveRight && !m_CanMoveRight && m_BoxCollider2D.Cast(Vector2.right, hits, .1f) < 1) if (m_ShouldMoveRight && !m_CanMoveRight && m_CircleCollider2D.Cast(Vector2.right, hits, .1f) < 1) { m_CanMoveRight = true; } else if (m_ShouldMoveRight && !m_CanMoveRight) { foreach (var hit in hits) { if (hit.transform.tag == "Hazard") { m_CanMoveRight = true; break; } } } /*} * else { * if (m_ShouldMoveRight && !m_CanMoveRight && m_EdgeCollider2D.Cast(Vector2.right, hits, .1f) < 1) * m_CanMoveRight = true; * else if (m_ShouldMoveRight && !m_CanMoveRight) { * foreach (var hit in hits) { * if (hit.transform.tag == "Hazard") { * m_CanMoveRight = true; * break; * } * } * } * }*/ // if considered in air and falling, check if we are actually landing on ground //if (m_InAir && m_Rigidbody2D.velocity.y <= 0 && m_BoxCollider2D.Cast(Vector2.down, hits, .1f) >= 1) { if (m_InAir && m_Rigidbody2D.velocity.y <= 0 && m_CircleCollider2D.Cast(Vector2.down, hits, .1f) >= 1) { foreach (var hit in hits) { if (hit.collider.gameObject.transform.position.y + hit.collider.bounds.extents.y > transform.position.y - m_CircleCollider2D.bounds.extents.y) { //if (hit.transform.position.y > transform.position.y) continue; } if (hit.transform.tag == "Ground" || hit.transform.tag == "Obstacle") { m_InAir = false; m_Rigidbody2D.velocity = new Vector2(m_Rigidbody2D.velocity.x, 0); //float newDist = hit.point.y - (transform.position.y - m_CircleCollider2D.bounds.extents.y); //if (newDist > 0f) //transform.Translate(Vector2.up * newDist); break; } } } }