void Start() { SetupMoveBoundaries(); laserDetector = FindObjectOfType <LaserDetector>(); alienController = FindObjectOfType <AlienController>(); levelController = FindObjectOfType <LevelController>(); lagBehindOffset = initialLagBehindOffset; }
// Use fixed update as we're working with raycasts void FixedUpdate() { //set ray orgin to gameeobject pos and set the direction to the forward vector ray = new Ray(transform.position, transform.forward); RaycastHit hit; if (Physics.Raycast(ray, out hit, MaxRange)) { //check if last hit gameobject has changed LaserDetector laserDetector = hit.transform.GetComponent <LaserDetector>(); //check if object hit is a laser detector if (laserDetector != null && lastHitObject != null && lastHitObject == hit.transform.gameObject) { //object is a laser detector //laserDetector.LaserExit(this); } //set the line rendered point to ray hit lineRenderer.SetPosition(1, transform.InverseTransformPoint(hit.point)); //set postion of laser hit particles and enable them LaserHitParticles.transform.position = hit.point; LaserHitParticles.transform.rotation = Quaternion.LookRotation(hit.normal); LaserHitParticles.SetActive(true); //Trigger laser detector enter if last object is differnt to the current if (laserDetector != null) { laserDetector.LaserHit(this); } //check if object hit is reflective if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Reflective")) { if (reflectedLaser != null && lastHitRotation != hit.transform.rotation) // moved object reset laser { reflectedLaser.transform.position = hit.point; reflectedLaser.transform.rotation = Quaternion.LookRotation(hit.normal); } //reflect Laser if (reflectedLaser == null && GetLaserParentCount() < MaxReflections) { reflectedLaser = Instantiate(gameObject).GetComponent <Laser>(); reflectedLaser.transform.position = hit.point; reflectedLaser.transform.rotation = Quaternion.LookRotation(hit.normal); reflectedLaser.parentLaser = this; } } else if (reflectedLaser != null) { DestroyLaser(reflectedLaser); //delete reflected laser reflectedLaser = null; } lastHitRotation = hit.transform.rotation; lastHitObject = hit.transform.gameObject; } }
public void ShootLaser() { string laserString = ""; var currentPosition = new Vector2Int((int)Math.Round(transform.position.x), (int)Math.Round(transform.position.y)); var direction = _laserDirection; var blockStatus = statusAtPosition(currentPosition, direction); _foundDetector = false; while (blockStatus != BlockStatus.Blocked) { if (blockStatus == BlockStatus.Free) { currentPosition += direction; laserString += DirectionToCharacter(direction); } else if (blockStatus == BlockStatus.Mirror) { if (direction.Equals(_lastMirror.mainInDirection)) { direction = direction.Rotate90Clockwise(); } else if (direction.Equals(_lastMirror.mainInDirection.Rotate90CounterClockwise())) { direction = direction.Rotate90CounterClockwise(); } else { break; } currentPosition += direction; laserString += DirectionToCharacter(direction); } blockStatus = statusAtPosition(currentPosition, direction); } if (!_foundDetector) { if (_lastDetector != null) { _lastDetector.Depower(); _lastDetector = null; } } RebuildWithDirections(laserString); }
private BlockStatus statusAtPosition(Vector2Int position, Vector2Int direction) { var results = Physics2D.OverlapBoxAll(position, new Vector2(0.95f, 0.95f), 0.0f); var blocked = false; foreach (var result in results) { if (result.gameObject.HasComponent(out Mirror mirror)) { _lastMirror = mirror; return(BlockStatus.Mirror); } if (result.gameObject.HasComponent(out LaserDetector detector)) { _foundDetector = true; if (detector.DetectionDirection.Equals(-direction)) { if (detector != _lastDetector) { if (_lastDetector != null) { _lastDetector.Depower(); } _lastDetector = detector; detector.Power(); } } return(BlockStatus.Free); } if (!result.isTrigger && result.gameObject != gameObject && !result.gameObject.HasComponent <LaserWall>()) { blocked = true; } } return(blocked ? BlockStatus.Blocked : BlockStatus.Free); }
private void Start() { laserDetector = FindObjectOfType <LaserDetector>(); spriteRenderer = GetComponent <SpriteRenderer>(); health = maxShots; }
private void Start() { laserDetector = FindObjectOfType <LaserDetector>(); }