public void CalculatePosition() { startPoint = transform.position + new Vector3(0f, transform.localScale.y / 2, 0f); endPoint = ((target.position) - startPoint); length = (startPoint - target.position).magnitude; Debug.DrawRay(startPoint, endPoint, Color.black); if (isAttached) { //velocity = (Vector3.Lerp (transform.position, (target.position), Time.fixedDeltaTime * followSpeed) - transform.position); //if (isAttaching) velocity = (target.position - transform.position) * Time.fixedDeltaTime * followSpeed / 2; /*else*/ velocity = (target.position - transform.position) * Time.fixedDeltaTime * followSpeed; spriteRenderer.flipX = owner.faceDir < 0; if (Physics2D.Raycast(startPoint - new Vector3(0f, transform.localScale.y / 2, 0f), endPoint, length, solidMask) && !isAttaching) { isAttached = false; } if (Mathf.Abs(target.position.x - transform.position.x) <= velocity.x) { velocity.x = 0; } if (Mathf.Abs(target.position.y - transform.position.y) <= velocity.y) { velocity.y = 0; } Vector3 rightCheck = startPoint + new Vector3(col.size.x / 2 * 1.25f, 0, 0); Vector3 rightEnd = ((target.position) - rightCheck); float rightLength = rightEnd.magnitude; if (Physics2D.Raycast(rightCheck + Vector3.up * 1.6f / 4, rightEnd, rightLength, solidMask) || Physics2D.Raycast(rightCheck - Vector3.up * 1.6f / 2, rightEnd, rightLength, solidMask)) { velocity += new Vector2(-2, -1) * Time.fixedDeltaTime * followSpeed / 2; } Vector3 leftCheck = startPoint + new Vector3(-col.size.x / 2 * 1.25f, 0, 0); Vector3 leftEnd = ((target.position) - leftCheck); float leftLength = leftEnd.magnitude; if (Physics2D.Raycast(leftCheck + Vector3.up * 1.6f / 4, leftEnd, leftLength, solidMask) || Physics2D.Raycast(leftCheck - Vector3.up * 1.6f / 2, leftEnd, leftLength, solidMask)) { velocity += new Vector2(2, -1) * Time.fixedDeltaTime * followSpeed / 2; } Debug.DrawRay(rightCheck, rightEnd, Color.blue); Debug.DrawRay(leftCheck, leftEnd, Color.blue); } else { if (controller.collisions.shouldReflectY) { velocity.y = -velocity.y * bounceDampening; } if (controller.collisions.shouldReflect) { // if (controller.collisions.isHittingPlayer && currentHit < numberOfHits) { // currentHit++; // // } else { // // } if (controller.collisions.hitHat) { velocity.x = -velocity.x * bounceDampening; } else if (!collisionSkip) { velocity.x = -velocity.x * bounceDampening; } else { collisionSkip = false; } currentHit = 1000; numberOfHits = 999; transform.rotation = Quaternion.identity; if (Mathf.Abs(velocity.x) > 0.001f) { PlayCollisionParticles(velocity); } } if (isThrown) { // [NEW] PlayHatFire(velocity); UpdateTrailAndFire(velocity); throwTimer += Time.deltaTime; if (ThrowIsOver()) { EndThrow(); ActivateOnDangerFinishAbility(); PlayThrowTimeDoneParticles(velocity); } } if (Mathf.Abs(velocity.x) > dangerousFriction * Time.deltaTime) { velocity.x -= Mathf.Sign(velocity.x) * ((controller.collisions.below) ? ((splitFriction && isBeingThrown) ? dangerousFriction : normalFriction) : ((splitDrag && isBeingThrown) ? dangerousDrag : normalDrag)) * Time.fixedDeltaTime; if (controller.collisions.below && Mathf.Abs(velocity.x) > 0.2f) { VFXManager.instance.EmitAtPosition("Collision_Stars", 2, transform.position + Vector3.right * 0.5f * controller.collisions.faceDir, true); } } else { velocity.x = 0.0f; } velocity.y -= (gravity * Time.fixedDeltaTime) + ((transform.position.y > throwY + throwHeight && velocity.y > -5) ? (gravity * ((Mathf.Abs(transform.position.y - (throwY + throwHeight))) * GRAVITY_HEIGHT_MODIFIER) * Time.fixedDeltaTime) : 0); if (canComeBack && !owner.isStunned && GetDistanceToOwner() <= maxDistance && !isHitFrozen) { if ((Physics2D.Raycast(startPoint, endPoint, length, solidMask))) { isAttached = false; } else { if (owner.canPickUpHat && !owner.isStunned) { GrabHatBack(); //GetComponent<HatPersonality>().StopDancing(); } } } if (controller.collisions.hitHat) { StopCoroutine("HitFlash"); StartCoroutine("HitFlash"); controller.collisions.hitHat = false; } } if (isAttaching) { if (Time.time > endAttachingTime) { isAttaching = false; } } //max speed check if (Mathf.Abs(velocity.x) > MAX_SPEED_X) { velocity.x = MAX_SPEED_X * Mathf.Sign(velocity.x); } if (Mathf.Abs(velocity.y) > MAX_SPEED_Y) { velocity.y = MAX_SPEED_Y * Mathf.Sign(velocity.y); } //move controller.Move(velocity); if (controller.collisions.below) { velocity.y = 0f; } if (controller.collisions.above) { velocity.y = -0.01f; } Squash(velocity, lastVelocity); if ((int)(velocity.x * 100) != (int)(lastVelocity.x * 100)) { lastVelocity.x = velocity.x; } if ((int)(velocity.y * 100) != (int)(lastVelocity.y * 100)) { lastVelocity.y = velocity.y; } }