public void def(Vector2 from) { globalVec = from; var to = Vector2.zero; var angle = Vector2.Angle(from, to); ball_collider = Physics2D.OverlapBox( new Vector3(this.transform.position.x + from.x, this.transform.position.y + from.y, 0) , colliderSize , angle, layer); if (ball_collider) { Debug.Log("In"); ball_movement = ball_collider.GetComponentInParent <Movement>(); ballState = ball_movement.getState(); var hurtLevel = ball_movement.checkState(); if (ballState == "_third" || ballState == "_second") { pushPlayer(hurtLevel); } if (ballState == "_third") { stunTime = hurtLevel; time = Time.time; stuned = true; } ball_movement.bounce(); } }
//Anpassung der velocity um eine absteigende Rampe zu berücksichtigen void DecentSlope(ref Vector3 velocity) { //Strahlt einen Ray nach unten, um den Winkel der absteigenden Rampe zu erhalten float directionX = Mathf.Sign(velocity.x); Vector2 rayOrigin = (directionX == -1) ? rayCastOrigins.bottomRight : rayCastOrigins.bottomLeft; RaycastHit2D hit = Physics2D.Raycast(rayOrigin, -Vector2.up, Mathf.Infinity, collissionMask); //Wenn absteigende Rampe vorhanden & Bewegung in diese Richtung erfolgt, bewege dich die Rampe herunter if (hit) { float slopeAngle = Vector2.Angle(hit.normal, Vector2.up); if (slopeAngle != 0 && slopeAngle <= maxDecentAngle) { if (Mathf.Sign(hit.normal.x) == directionX) { if (hit.distance - skinwidth <= Mathf.Tan(slopeAngle * Mathf.Deg2Rad) * Mathf.Abs(velocity.x)) { float moveDistance = Mathf.Abs(velocity.x); float descendVelocityY = Mathf.Sin(slopeAngle * Mathf.Deg2Rad) * moveDistance; velocity.x = Mathf.Cos(slopeAngle * Mathf.Deg2Rad) * moveDistance * Mathf.Sign(velocity.x); velocity.y -= descendVelocityY; collissions.slopeAngle = slopeAngle; collissions.descendingSlope = true; collissions.below = true; } } } } }
Vector2 RelativePitchToTarget(Vector2 target, float pitchDegrees, float deadRange = 5) { Vector2 turnForce = Vector2.Perpendicular(rb.velocity) / rb.mass; float angleToTarget = Vector2.Angle(rb.velocity, target - getPosition()); float direction = 1; if (angleToTarget > 180) { angleToTarget = 360 - angleToTarget; direction = -1; } if (pitchDegrees + deadRange < angleToTarget || angleToTarget < pitchDegrees - deadRange) { return(turnForce * direction); } else { return(Vector2.zero); } }
// Update is called once per frame void Update() { var gameOver = FindObjectOfType <GameOver>(); if (my_health == 0 && !gameOver.isShown()) { SpawnManager spawner = FindObjectOfType <SpawnManager>(); if (spawner != null) { spawner.EndGame(); } GameObject[] cows = GameObject.FindGameObjectsWithTag("follower_cow"); foreach (GameObject cow in cows) { Destroy(cow); } Destroy(gameObject); } float rot_dir = 0; float cur_speed = 0; if (Input.GetKey(KeyCode.S) && GameOver.isInputEnabled) { rot_dir = 180; } if (Input.GetKey(KeyCode.A) && GameOver.isInputEnabled) { rot_dir = 90; } if (Input.GetKey(KeyCode.D) && GameOver.isInputEnabled) { rot_dir = 270; } if (stunned_cd < 0.0f) { gameObject.transform.rotation = Quaternion.RotateTowards(gameObject.transform.rotation, Quaternion.Euler(0, 0, rot_dir), 10f); if ((Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D)) && GameOver.isInputEnabled) { cur_speed = speed * Time.deltaTime; transform.Translate(Vector2.up * cur_speed); } Vector2 rot_vec = gameObject.transform.up; foreach (FollowerCow fc in FindObjectsOfType <FollowerCow>()) { float angle = Vector2.Angle(rot_vec, fc.transform.position - gameObject.transform.position); if (angle < 90) { fc.ReceiveData(gameObject.transform.rotation.eulerAngles.z, cur_speed * 0.5f); } else { fc.ReceiveData(gameObject.transform.rotation.eulerAngles.z, cur_speed); } } } else { gameObject.transform.Rotate(new Vector3(0, 0, 10)); foreach (FollowerCow fc in FindObjectsOfType <FollowerCow>()) { fc.ReceiveData(gameObject.transform.rotation.eulerAngles.z, 0); } } // main camera var dist = (transform.position - Camera.main.transform.position).z; var left = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, dist)).x; var right = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, dist)).x; var top = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, dist)).y; var bottom = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, dist)).y; transform.position = new Vector3( Mathf.Clamp(transform.position.x, left, right), Mathf.Clamp(transform.position.y, top + 1, bottom - 1), transform.position.z); stunned_cd -= Time.deltaTime; }
//Vertikalle Bewegungsberechnung void VerticalCollisions(ref Vector3 velocity) { float directionY = Mathf.Sign(velocity.y); float rayLength = Mathf.Abs(velocity.y) + skinwidth; //Rays werden nach oben und unten abhängig von der bewegungsrichtung ausgestrahlt for (int i = 0; i < verticalRayCount; i++) { Vector2 rayOrigin = (directionY == -1) ? rayCastOrigins.bottomLeft:rayCastOrigins.topLeft; rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x); RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collissionMask); Debug.DrawRay(rayOrigin, Vector2.up * directionY, Color.red); //Wenn ein Strahl ein Ground Objekt trifft bewege den Spieler bis zu dem objekt if (hit) { if (hit.distance == 0 && hit.collider.tag != "PassablePlatform") { velocity.y = -directionY * skinwidth; continue; } if (hit.collider.tag == "PassablePlatform") { if (directionY == 1 || hit.distance == 0) { continue; } if (collissions.fallingThorughPlatform) { continue; } if (playerInput.y == -1) { collissions.fallingThorughPlatform = true; Invoke("ResetFallingThroughPlatform", 0.5f); continue; } } velocity.y = (hit.distance - skinwidth) * directionY; rayLength = hit.distance; if (collissions.climbingSlope) { velocity.x = velocity.y / Mathf.Tan(collissions.slopeAngle * Mathf.Deg2Rad) * Mathf.Sign(velocity.x); } collissions.below = directionY == -1; collissions.above = directionY == 1; } } if (collissions.climbingSlope) { float directionX = Mathf.Sign(velocity.x); rayLength = Mathf.Abs(velocity.x) + skinwidth; Vector2 rayOrigin = ((directionX == -1) ? rayCastOrigins.bottomLeft : rayCastOrigins.bottomRight) + Vector2.up * velocity.y; RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collissionMask); if (hit) { float slopeAngle = Vector2.Angle(hit.normal, Vector2.up); if (slopeAngle != collissions.slopeAngle) { velocity.x = (hit.distance - skinwidth) * directionX; collissions.slopeAngle = slopeAngle; } } } }
//Behandelt horiziontale Bewegung void HorizontalCollisions(ref Vector3 velocity) { float directionX = collissions.faceDirection; float rayLength = Mathf.Abs(velocity.x) + skinwidth; if (Mathf.Abs(velocity.x) < skinwidth) { rayLength = 2 * skinwidth; } //Strahlt Rays von dem Character nach links oder rechts aus for (int i = 0; i < horizontalRayCount; i++) { Vector2 rayOrigin = (directionX == -1) ? rayCastOrigins.bottomLeft : rayCastOrigins.bottomRight; rayOrigin += Vector2.up * (horizontalRaySpacing * i); RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.right * directionX, rayLength, collissionMask); Debug.DrawRay(rayOrigin, Vector2.right * directionX, Color.red); if (hit) { if (hit.collider.tag == "lvlBoundry") { isLevelBoundry = true; } else { isLevelBoundry = false; } if (hit.distance == 0 && hit.collider.tag != "PassablePlatform") { for (int e = 0; e < horizontalRayCount; e++) { //print("FaceDirection = " + collissions.faceDirection); Vector2 rayOrigin2 = (directionX == -1) ? rayCastOrigins.bottomRight : rayCastOrigins.bottomLeft; rayOrigin2 += Vector2.up * (horizontalRaySpacing * i); RaycastHit2D hit2 = Physics2D.Raycast(rayOrigin2, Vector2.right * -directionX, rayLength, collissionMask); Debug.DrawRay(rayOrigin2, Vector2.right * -directionX, Color.magenta); if (hit2) { if (hit2.distance == 0) { //Player is Squashed print("Kill Player"); isDead = true; sendDeathAnalytics("Crushed"); isSplashed = true; break; } } } velocity.x = -directionX * skinwidth * 9; continue; } if (hit.collider.tag == "PassablePlatform") { continue; } //Sorgt dafür, dass man durch eine Wand laufen kann, wenn man eigentlich stuck werden würde //TODO: Verhalten hinzufügen dafür, wenn man in einer Wand zu tief drin ist if (hit.distance == 0) { continue; } //Holt den winkel, abhängig von der normalen, auf die man zuläuft float slopeAngle = Vector2.Angle(hit.normal, Vector2.up); if (i == 0 && slopeAngle <= maxClimbAngle) { if (collissions.descendingSlope) { collissions.descendingSlope = false; velocity = collissions.velocityOld; } float distanceToSlopeStart = 0f; if (slopeAngle != collissions.slopeAngleOld) { distanceToSlopeStart = hit.distance - skinwidth; velocity.x -= distanceToSlopeStart * directionX; } ClimbSlope(ref velocity, slopeAngle); velocity.x += distanceToSlopeStart * directionX; } if (!collissions.climbingSlope || slopeAngle > maxClimbAngle) { velocity.x = (hit.distance - skinwidth) * directionX; rayLength = hit.distance; if (collissions.climbingSlope) { velocity.y = Mathf.Tan(collissions.slopeAngle * Mathf.Deg2Rad) * Mathf.Abs(velocity.x); } collissions.left = directionX == -1; collissions.right = directionX == 1; } } else { isLevelBoundry = false; } } }