private void CheckForHits() { //Simply checks the distance between the ball before and after moving in this same update cycle. //If the ball hits something, the first thing hit is checked, then the next, etc. If it hits something, //it returns, not hitting any other targets. if (fireState != FireState.Live) { return; } Vector2 currentPos = new Vector2(transform.position.x, transform.position.y); //RaycastHit2D[] hits = Physics2D.RaycastAll(previousPos, (currentPos - previousPos).normalized, (currentPos - previousPos).magnitude); SpriteRenderer tempRend = GetComponentInChildren <SpriteRenderer>(); RaycastHit2D[] hits = Physics2D.CapsuleCastAll(previousPos, new Vector2(tempRend.size.x / 2, tempRend.size.y / 2), CapsuleDirection2D.Horizontal, 0, velocity, (currentPos - previousPos).magnitude, PhysicsLayers.LayerMask((int)Layer.AttackOrb)); for (int i = 0; i < hits.Length; i++) { HitTrigger(hits[i].collider); } }
private void Start() { boidsLayerMask = PhysicsLayers.GetLayerMask(new[] { PhysicsLayers.Layers.Boids }); _boids = FindObjectsOfType <BoidAgent>(); }
private void Update() { //read user axis input var inputX = Input.GetAxisRaw(InputStrings.Axis.Horizontal) * moveSpeed * Time.deltaTime; var inputZ = Input.GetAxisRaw(InputStrings.Axis.Vertical) * moveSpeed * Time.deltaTime; //set move metadata booleans if (inputX == 0 && inputZ == 0) { _isMoving = false; } else { _isMoving = true; } _isGrounded = Physics.CheckSphere(groundChecker.position, groundCheckDistance, LayerMask.GetMask(PhysicsLayers.GetLayerName(Ground))); //apply movement logic if (_isMoving && _isGrounded) { _moveVector = transform.TransformDirection(new Vector3(inputX, 0, inputZ)); //"TranformDirection" to convert from localspace to worldspace (used by AddForce) _rigidbody.drag = groundedDrag; } else if (_isGrounded) { _rigidbody.velocity = new Vector3(0, 0, 0); _moveVector = Vector3.zero; } else { _moveVector = Vector3.zero; _rigidbody.drag = inAirDrag; } //update rotations on player & camera if (isPlanetPresent) { var vectorTowardsPlanet = (planet.transform.position - transform.position).normalized; RealignPlayerRotation(-vectorTowardsPlanet); RealignCameraRotation(-vectorTowardsPlanet); } else { RealignPlayerRotation(Vector3.up); } //update camera position MoveCameraWithPlayer(); }