void OnTriggerEnter2D(Collider2D target) { if (target.gameObject.CompareTag("Planet")) { currentPlanet = target.gameObject.GetComponent <PlanetScript>(); } }
void Update() { if (gameMaster.currentCapsule == null) { return; } List <PlanetScript> planets = gameMaster.currentCapsule.GetComponent <GravityApplier>().planets; PlanetScript planet = null; if (planets.Count > 0) { planet = planets[0]; } if (planet != null) { counter.text = Math.Round(gameMaster.currentCapsule.GetComponent <GravityApplier>().drag *5, 2, MidpointRounding.AwayFromZero).ToString(); foreach (GameObject visual in visuals) { visual.SetActive(true); } } else { //counter.text = "NONE"; foreach (GameObject visual in visuals) { visual.SetActive(false); } } }
public Vector3 gravity(PlanetScript ps, Vector3 pos) { Vector3 displacement = ps.gameObject.transform.position - pos; float dist = Mathf.Max(5f, displacement.magnitude); return(multiplier * displacement.normalized * ps.strengthOfAttraction / Mathf.Pow(dist, power)); }
private void Awake() { rb = GetComponent <Rigidbody>(); PScript = GameObject.Find("Enviroment/Planet").GetComponent <PlanetScript>(); GMS = GameObject.Find("GameManager").GetComponent <GameManagerScript>(); fixedJoystick = GameObject.Find("Canvas/FixedJoystick").GetComponent <FixedJoystick>(); }
// helper function that lands the player onto a given planet public void LandOnPlanet(GameObject planet) { // make sure that the player hasn't already landed if (isLanded == false && currentPlanet == null) { isLanded = true; canJump = true; // TODO: implement a brief delay when you land where you can't jump // clear coin combo coinCombo = 0; currentPlanet = planet.GetComponent <PlanetScript>(); playerAudio.PlayLandingSound(); // rotates and repositions the player accordingly Vector3 difference = transform.position - currentPlanet.transform.position; float radius = currentPlanet.gameObject.GetComponent <CircleCollider2D>().radius *currentPlanet.transform.lossyScale.x + GetComponent <BoxCollider2D>().size.y *transform.lossyScale.x / 2; Vector3 newDifference = difference.normalized * radius; transform.position = transform.position - difference + newDifference; RotateToPlanet(); GetComponent <Rigidbody2D>().freezeRotation = true; transform.parent = planet.transform; nearbyPlanets.Remove(currentPlanet.gameObject); } else { print("Error: cannot land on " + planet.name); } }
private Vector2 CalculateGravityPull() { Vector2 final = Vector2.zero; float G = 6.67300f * 1f; // should be 10 to the -11th power, but we're keeping planet mass low to compensate float m = 1; // player mass if (activatedPowerup == PowerupScript.PowerupType.lighting) { m = powerupMass; } foreach (GameObject p in nearbyPlanets) { PlanetScript planet = p.GetComponent <PlanetScript>(); if (planet && planet.GetIsDestroyed() == false) { Vector2 r = new Vector2(planet.transform.position.x - transform.position.x, planet.transform.position.y - transform.position.y); Vector2 direction = r.normalized; // raised to power of 2.5 instead of 2 to make gravitation attraction slightly weaker than normal float r_squared = Mathf.Pow(r.magnitude, 3f); // F = G*M*m/r_squared float F = G * planet.mass * m / r_squared; final += F * direction; } } return(final); }
private void generateGameObjectForParticle(int i) { GameObject starObj = starObjectsToGenerate[Random.Range(0, starObjectsToGenerate.Length)]; // GameObject starObj = starObjectsToGenerate[0]; gameObjects[i] = Instantiate(starObj, points[i].position, Quaternion.Euler(Vector3.zero)); DontDestroyOnLoad(gameObjects[i]); planetScript = gameObjects[i].GetComponent <PlanetScript>(); planetScript.radius = pointSizes[i] * 0.15f; planetScript.transform.rotation = Quaternion.Euler(new Vector3(Random.Range(-180, 180), Random.Range(-180, 180), Random.Range(-180, 180))); // COLOR Color.RGBToHSV(planetScript.lightColor, out float h, out float s, out float v); if (s > .2f) { s = .2f; } points[i].startColor = Color.HSVToRGB(h, s, v); //radarPoints[i].startColor = points[i].startColor; // Radar points color isHostile[i] = planetScript.isHostile; if (planetScript.isHostile) { radarPoints[i].startColor = radarParticleColorHostile; // Different color for hostile planets } else { radarPoints[i].startColor = radarParticleColor; } gameObjects[i].SetActive(false); }
// helper function that lands the player onto a given planet public void LandOnPlanet(GameObject planet) { // make sure that the player hasn't already landed if (isLanded == false && currentPlanet == null) { isLanded = true; canJump = true; // TODO: implement a brief delay when you land where you can't jump canAction = false; takingAction = false; currentAntiStuckForceTime = 0f; cumulatedGravityPullForce = Vector2.zero; StartCoroutine(EnableTail(false, 1f)); // clear coin combo currentPlanet = planet.GetComponent <PlanetScript>(); playerAudio.PlayLandingSound(); // rotates and repositions the player accordingly StandOnCurrentPlanet(); RotateToCurrentPlanet(); myRigidBody.freezeRotation = true; transform.parent = planet.transform; nearbyPlanets.Remove(currentPlanet.gameObject); // TODO: clean this up if (currentPlanet.gameObject.tag == "HomePlanet" && currentPlanet.rotationSpeed != homePlanetRotationSpeed) { currentPlanet.rotationSpeed = homePlanetRotationSpeed; } } else { Debug.Log("Error: cannot land on " + planet.name); } }
// helper function that resets the player to initial drifting state private void ResetPlayerStates() { isDead = false; isLanded = false; canJump = false; currentChargeTime = 0f; canAction = false; takingAction = false; currentActionChargeTime = 0f; actionChargesUsable = 0; transform.parent = null; currentPlanet = null; lastPlanet = null; currentAntiStuckForceTime = 0f; leavingSpeed = 0f; chargeBar.SetActive(false); jetpackFlame.SetActive(false); activatedPowerup = PowerupScript.PowerupType.none; powerupCountdown = 0; powerupEffect.DeactivateAllEffects(); transform.position = initialPosition; // TODO: need better method for respawn transform.eulerAngles = initialRotation; nearbyPlanets.Clear(); }
// when colliding with a planet, land on it if drifting, or switch planets if already on a planet void OnCollisionEnter2D(Collision2D other) { if ((other.gameObject.tag == "Planet" || other.gameObject.tag == "HomePlanet") && isDead == false) { if (currentPlanet == null && isLanded == false) { LandOnPlanet(other.gameObject); } else { // make sure that enough time has passed since the last planet transfer or that this is a new planet if (Time.time > lastPlanetTransferTime + 1f || lastPlanet != other.gameObject) { lastPlanet = currentPlanet; currentPlanet = other.gameObject.GetComponent <PlanetScript>(); nearbyPlanets.Remove(currentPlanet.gameObject); // set the player position and rotation accordingly StandOnCurrentPlanet(); RotateToCurrentPlanet(); myRigidBody.freezeRotation = true; transform.parent = currentPlanet.transform; lastPlanetTransferTime = Time.time; } } } }
// Update is called once per frame void Update() { if (frames.Length > 1) { if (currentFrame) { GetComponent <SpriteRenderer>().sprite = frames[0]; } else { GetComponent <SpriteRenderer>().sprite = frames[1]; } } timer += Time.deltaTime; if (timer > frameTimer) { timer = 0f; currentFrame = !currentFrame; } if (frames.Length > 1) { if (allPlanets == null) { allPlanets = FindObjectsOfType <PlanetScript>(); } else { PlanetScript closestPlanet = null; float dist = 9999999f; for (int i = 0; i < allPlanets.Length; i++) { if (allPlanets[i] == null) { continue; } float testDist = (allPlanets[i].transform.position - transform.position).magnitude / allPlanets[i].GetComponent <Rigidbody2D>().mass; if (testDist < dist) { dist = testDist; closestPlanet = allPlanets[i]; } } planet = closestPlanet; } if (planet != null) { Vector2 towardsPlanet = planet.transform.position - transform.position; GetComponent <Rigidbody2D>().velocity = GetComponent <Rigidbody2D>().velocity + towardsPlanet.normalized * Time.deltaTime; } } else { GetComponent <Rigidbody2D>().velocity = GetComponent <Rigidbody2D>().velocity.normalized *6f; } }
void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Planet") { Debug.Log("skok"); attractorPlanet = other.GetComponent <PlanetScript>(); gameObject.transform.parent = attractorPlanet.transform; } }
void Start() { playerLife = 100f; GetComponent <Rigidbody>().useGravity = false; planet = GameObject.FindGameObjectWithTag("Planet"); attractorPlanet = planet.GetComponent <PlanetScript>(); // GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation; playerTransform = transform; }
//Adding Player And Planet; void AddPlayer() { Player = Instantiate(rocket[TransferData.selectedRocket], PlayerStart.position, PlayerStart.rotation); PlayerRb = Player.GetComponent <Rigidbody>(); Playerscript = Player.GetComponent <RocketConteroller>(); Planet = Instantiate(plenet[TransferData.selectedPlanet], PlanetStart.position, PlanetStart.rotation); Planet.transform.parent = PlanetStart; PlanetScript = Planet.GetComponent <PlanetScript>(); PlanetStart.localScale = new Vector3(5.3f, 5.3f, 5.3f); }
public void Teletransport_Out() { canMove = false; gun.canShoot = false; isOnPlanet = false; planet = null; playerRender.color = invisible; armRender.color = invisible; var rotationVector = transform.rotation.eulerAngles; rotationVector.z = 0f; transform.rotation = Quaternion.Euler(rotationVector); }
protected override void UseSpecial() { GameObject go = Instantiate <GameObject>(bombPrefab); Vector3 towardsPlanet = Vector2.zero; PlanetScript planet = GetComponent <CannonScript>().GetPlanet(); if (planet != null) { towardsPlanet = planet.transform.position - transform.position; towardsPlanet = towardsPlanet.normalized; } go.transform.position = transform.position + towardsPlanet * 0.75f; go.transform.rotation = transform.rotation; }
void Update() { closestPlanet = GetClosestPlanet(); if (closestPlanet == null) { return; } //closestPlanetRadius = closestPlanet.GetComponent<PlanetScript>().planetData.radius; //PlanetTestScript planetScript = closestPlanet.GetComponent<PlanetTestScript>(); PlanetScript planetScript = closestPlanet.GetComponent <PlanetScript>(); if (planetScript == null) { return; } //closestPlanetRadius = planetScript.radius; closestPlanetRadius = planetScript.planetData.radius; distanceToClosestPlanet = Vector3.Distance(transform.position, closestPlanet.transform.position); distanceToClosestPlanetSurface = Vector3.Distance(transform.position, closestPlanet.transform.position) - closestPlanetRadius; if (distanceToClosestPlanet > closestPlanetRadius * 1.03f) { if (!inSpace) { inSpace = true; OnSpaceEntered(); if (freezeRotation) { rb.constraints = RigidbodyConstraints.FreezeRotation; } } } else { if (inSpace) { inSpace = false; OnSpaceLeaved(); if (freezeRotation) { rb.constraints = RigidbodyConstraints.FreezeRotation; } } } }
// sends player off a planet and blows it up public void LeavePlanet(float chargeTime) { // make sure there is a currentplanet and the player can jump off it if (isLanded && currentPlanet != null && canJump) { isLanded = false; transform.parent = null; canJump = false; playerAudio.PlayLeavingSound(); Vector2 leavingAngle = new Vector2(transform.position.x - currentPlanet.transform.position.x, transform.position.y - currentPlanet.transform.position.y).normalized; // TODO: check if applyForce is better float leavingSpeed = maxChargeSpeed / 2f; if (chargeTime >= maxChargeTime) { leavingSpeed = maxChargeSpeed; } if (activatedPowerup == PowerupScript.PowerupType.lighting) { leavingSpeed *= powerupSpeed; } GetComponent <Rigidbody2D>().velocity = leavingAngle * leavingSpeed; GetComponent <Rigidbody2D>().freezeRotation = false; // consume powerLevel //powerLevel -= powerLevelUsedOnJump; // kills the other player if they're on this planet and it will explode if (otherPlayer.isDead == false && otherPlayer.currentPlanet != null && otherPlayer.currentPlanet.gameObject == this.currentPlanet.gameObject && currentPlanet.WillExplodeNext()) { otherPlayer.Suicide(); } // if the planet will explode, get points if (currentPlanet.WillExplodeNext()) { AcquirePoints(currentPlanet.GetPointValue(), this.transform.position); } nearbyPlanets.Remove(currentPlanet.gameObject); currentPlanet.SelfDestruct(); currentPlanet = null; } }
void FixedUpdate() { Vector3 playerPosition = new Vector3(playerBody.position.x, playerBody.position.y, 0f); playerBody.MovePosition(playerPosition + transform.TransformDirection(moveDir) * moveSpeed * Time.deltaTime); RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 5f, planetLayer); if (hit) { if (hit.collider.gameObject.tag == "Planet") { planet = hit.collider.gameObject.GetComponent <PlanetScript>(); isOnPlanet = true; } } }
void OnTriggerExit2D(Collider2D other) { if (other.gameObject.tag == "Planet" && other.GetComponent <PlanetScript>().GetPushForce().magnitude > 1f) { PlanetScript planet = other.GetComponent <PlanetScript>(); planet.transform.position = GetWarpedPosition(planet.transform.position); // clears the planet from player's interacted list if if (player.GetLastPlanet() == planet.gameObject) { player.ClearLastPlanet(); } } else if (other.gameObject.tag == "CoinOrb" && other.GetComponent <OrbScript>().GetBlastVelocity().magnitude > 1f) { other.gameObject.transform.position = GetWarpedPosition(other.gameObject.transform.position); } }
// Update is called once per frame protected override void UseSpecial(){ base.UseSpecial(); GameObject go = Instantiate<GameObject>(wildCardPrefab); Vector3 towardsPlanet = Vector2.zero; PlanetScript planet = GetComponent<CannonScript>().GetPlanet(); if (planet != null){ towardsPlanet = planet.transform.position - transform.position; towardsPlanet = towardsPlanet.normalized; } go.transform.position = transform.position + towardsPlanet*CannonScript.spawnDist; go.transform.rotation = transform.rotation; go.transform.localScale = new Vector3(1f, 1f, 1f)*0.1f; go.GetComponent<Rigidbody2D>().velocity = (new Vector3(0f, 0f, 0f)-transform.position).normalized; FindObjectOfType<ScoreTimerScript>().ResetMultiplier(); GameObject snd = Instantiate<GameObject>(soundPrefab); snd.GetComponent<SFXScript>().sfx = drawSound; }
void Update() { List <PlanetScript> planets = gameMaster.currentCapsule.GetComponent <GravityApplier>().planets; PlanetScript planet = null; if (planets.Count > 0) { planet = planets[0]; } if (planet != null) { counter.text = Math.Round(planet.currentDrag, 4, MidpointRounding.AwayFromZero).ToString(); } else { counter.text = "NONE"; } }
// Update is called once per frame void Update() { if (planet == null) { PlanetScript[] allPlanets = FindObjectsOfType <PlanetScript>(); for (int i = 0; i < allPlanets.Length; i++) { if (allPlanets[i] != null && allPlanets[i].match > -1) { planet = allPlanets[i]; break; } } } else { GetComponent <Image>().sprite = gemSprites[planet.match]; } }
// helper function that resets the player to initial drifting state private void ResetPlayerStates() { isDead = false; isLanded = false; canJump = false; transform.parent = null; currentPlanet = null; activatedPowerup = PowerupScript.PowerupType.none; powerupCountdown = 0; powerupEffect.DeactivateAllEffects(); coinCombo = 0; transform.position = initialPosition; transform.eulerAngles = initialRotation; //powerLevel = powerLevelInitial; nearbyPlanets.Clear(); // give a starting velocity to nearest planet. if no planets exist, then just assign random velocity float startingSpeed = 5f; GameObject nearestPlanet = null; float nearestDistance = 9999f; foreach (GameObject planet in GameObject.FindGameObjectsWithTag("Planet")) { float newDistance = Vector3.Distance(planet.transform.position, transform.position); if (newDistance < nearestDistance) { nearestDistance = newDistance; nearestPlanet = planet; } } if (nearestPlanet != null) { Vector2 startingDirection = (nearestPlanet.transform.position - transform.position).normalized; GetComponent <Rigidbody2D>().velocity = startingDirection * startingSpeed; } else { float angle = Random.Range(0, 360) * Mathf.Deg2Rad; GetComponent <Rigidbody2D>().velocity = new Vector2(Mathf.Cos(angle) * startingSpeed, Mathf.Sin(angle) * startingSpeed); } }
void OnCollisionEnter2D(Collision2D other) { // if this is homeplanet and it collided with a normal planet, game over if (this.tag == "HomePlanet" && other.gameObject.tag == "Planet") { GameObject.FindGameObjectWithTag("GameManager").GetComponent <GameManager>().StopGameRunning(); } // if this is a planet-planet collision, the current planet is being moved by pushForce and the two planet havent just interacted with each other else if (this.tag == "Planet" && other.gameObject.tag == "Planet" && pushForce != Vector2.zero && other.gameObject != lastInteractedObj) { PlanetScript otherPlanet = other.gameObject.GetComponent <PlanetScript>(); SetLastInteractedObj(otherPlanet.gameObject); otherPlanet.SetLastInteractedObj(this.gameObject); Vector3 damageDirection = (other.transform.position - this.transform.position).normalized; this.TakeDamage(damageDirection, false); collidedAfterPush = true; otherPlanet.TakeDamage(-damageDirection, false); } }
// ---- METHODS ---- #region // Generate Planet public GameObject GeneratePlanet() { planet = new GameObject(name); planet.AddComponent <MeshFilter>(); planet.AddComponent <MeshRenderer>(); planet.AddComponent <MeshCollider>(); PlanetScript ps = planet.AddComponent <PlanetScript>(); // Add planet script PlanetScript planetScript = planet.GetComponent <PlanetScript>(); //planetScript.planetData = this; // <--------------------<----------------------<---------------------<------------------<-------- Jos käytät tätä scriptiä niin muista tää ------<------------------< if (hasAtmosphere) { atmosphere = AtmosphereGenerator.GenerateAtmosphere(planet.transform, planetSize / 2f + 15f, 0.25f); } return(planet); }
// ---- METHODS ---- #region // Generate Planet public GameObject GeneratePlanet(Vector3 position) { planet = new GameObject(name); planet.tag = "Planet"; planet.AddComponent <MeshFilter>(); planet.AddComponent <MeshRenderer>(); PlanetScript ps = planet.AddComponent <PlanetScript>(); ps.planetData = this; GravityAttractor ga = planet.AddComponent <GravityAttractor>(); if (hasAtmosphere) { atmosphere = AtmosphereGenerator.GenerateAtmosphere(planet.transform, planetSize / 2f, 0.1f); //atmosphere = AtmosphereGenerator.GenerateAtmosphere(planet.transform, planetSize / 2f + 12f, 0.1f); } planet.transform.position = position; return(planet); }
// Update is called once per frame void Update() { if (allPlanets == null) { allPlanets = FindObjectsOfType <PlanetScript>(); } else { PlanetScript closestPlanet = null; float dist = 9999999f; for (int i = 0; i < allPlanets.Length; i++) { if (allPlanets[i] == null || allPlanets[i].gameObject.Equals(this.gameObject)) { continue; } float testDist = (allPlanets[i].transform.position - transform.position).magnitude / allPlanets[i].GetComponent <Rigidbody2D>().mass; if (testDist < dist) { dist = testDist; closestPlanet = allPlanets[i]; } } planet = closestPlanet; } if (planet != null) { Vector2 towardsPlanet = planet.transform.position - transform.position; float gravDir = 1f; if (planet.antigravity && towardsPlanet.magnitude < planet.GetComponent <Rigidbody2D>().mass) { gravDir = -1f; } GetComponent <Rigidbody2D>().velocity = GetComponent <Rigidbody2D>().velocity + towardsPlanet.normalized * 0.5f * Time.deltaTime * planet.GetComponent <Rigidbody2D>().mass *gravDir; } }
// sends player off a planet and blows it up public void LeavePlanet(float chargeTime) { // make sure there is a currentplanet and the player can jump off it if (isLanded && currentPlanet != null && canJump) { isLanded = false; transform.parent = null; canJump = false; StartCoroutine(EnableTail(true, 0f)); playerAudio.PlayLeavingSound(); float chargePercentage = Mathf.Clamp(chargeTime / maxChargeTime, 0f, 1f); leavingSpeed = maxChargeSpeed * (2f + chargePercentage) / 3f; Vector2 leavingDirection = (Vector2)(transform.position - currentPlanet.transform.position).normalized; //if (activatedPowerup == PowerupScript.PowerupType.lighting){ // leavingSpeed *= powerupSpeed; //} myRigidBody.velocity = leavingDirection * leavingSpeed; myRigidBody.freezeRotation = false; lastPlanet = currentPlanet; // crack/destroy the planet if it's not the home planet if (currentPlanet.tag != "HomePlanet") { // if the planet will not explode, push it if (!currentPlanet.WillExplodeNext()) { float pushPower = planetPushPower * (1f + chargePercentage * 1f); currentPlanet.PushPlanet(-leavingDirection, pushPower); } currentPlanet.TakeDamage(leavingDirection, true); } currentPlanet = null; } }
// Update is called once per frame void Update() { if (/*Input.GetKey (KeyCode.Space) ||*/ Input.touches.Length > 0) { pos = !pos; } if (pos) { foreach (Transform planet in planets.transform) { PlanetScript ps = planet.GetComponent <PlanetScript>(); rigidbody.AddForce(ps.mass / (planet.position - transform.position).magnitude * (planet.position - transform.position).normalized * Time.deltaTime); } } else { foreach (Transform planet in planets.transform) { PlanetScript ps = planet.GetComponent <PlanetScript>(); rigidbody.AddForce(ps.mass / (planet.position - transform.position).magnitude * -1 * (planet.position - transform.position).normalized * Time.deltaTime); } } }
// Use this for initialization void Start() { style.font = skin.font; style.alignment = TextAnchor.UpperLeft; //x = 14f; //y = -8f; user = GameObject.FindWithTag("User"); planet = GetComponent<PlanetScript>(); //PlanetScript tmp = GetComponent<PlanestScript>(); shipsBT = planet.shipsB.Count; shipsRT = planet.shipsR.Count; shipsNT = planet.shipsN.Count; //refresh = 0.2f; //tmp = 0; vecR = new Vector3(gameObject.transform.position.x, -18, gameObject.transform.position.z); textR = Resources.Load("TextSelectRed")as GameObject; textR.transform.position = vecR; textR = (GameObject) Instantiate(textR); textR.transform.RotateAround(textR.transform.position,Vector3.up, 90); boolR = true; boolRT = false; textB = Resources.Load("TextSelectBlue")as GameObject; textB.transform.position = vecR; textB = (GameObject) Instantiate(textB); textB.transform.RotateAround(textB.transform.position,Vector3.up, -90); boolB = true; boolBT = false; boolBT2 = false; textN = Resources.Load("TextSelectNeutre")as GameObject; textN.transform.position = vecR; textN = (GameObject) Instantiate(textN); boolN = true; boolNT = false; textVs = Resources.Load("TextSelectNeutre")as GameObject; textVs.transform.position = vecR; textVs = (GameObject) Instantiate(textVs); textVs.active = false; boolVs = false; if(shipsRT >0){ textB.active = false; textN.active = false; ((TextMesh)textR.GetComponent<TextMesh>()).text = ""+shipsRT; } if(shipsBT >0){ textR.active = false; textN.active = false; ((TextMesh)textB.GetComponent<TextMesh>()).text = ""+shipsBT; } if(shipsNT >0){ textR.active = false; textB.active = false; ((TextMesh)textN.GetComponent<TextMesh>()).text = ""+shipsNT; } ((TextMesh)textVs.GetComponent<TextMesh>()).text = "/"; }
Vector3 gravity(PlanetScript ps) { Vector3 displacement = ps.gameObject.transform.position - transform.position; return displacement.normalized * ps.strengthOfAttraction / Mathf.Pow(displacement.magnitude, 2); }
public Vector3 gravity(PlanetScript ps, Vector3 pos) { Vector3 displacement = ps.gameObject.transform.position - pos; float dist = Mathf.Max(5f,displacement.magnitude); return multiplier * displacement.normalized * ps.strengthOfAttraction / Mathf.Pow(dist, power); }