void Awake() { _planet = GameObject.FindObjectOfType <PlanetAttractor>(); _rbody = GetComponent <Rigidbody>(); _rbody.useGravity = false; _rbody.constraints = RigidbodyConstraints.FreezeRotation; }
protected virtual void FixedUpdate() { //Jeżeli program jest w trybie symulacyjnym to planety oddziałują na siebie if (Game.State == GameState.Simulation) { PlanetAttractor.AttractPlanets(this); } }
void OnTriggerExit2D(Collider2D other) { if (other.CompareTag("Planet") && other.GetComponent <PlanetAttractor> () == planet) { gameObject.transform.parent = null; planet = null; } }
void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Planet")) { gameObject.transform.parent = other.gameObject.transform; planet = other.gameObject.GetComponent <PlanetAttractor> (); } }
//Inicializálása használatos //Akkor fut le,amikor az a GameObject létrejön, amihez csatoltuk ezt a scriptet //A GameObject akkor jön létre, ha belépünk arra a Scene-re, amihez tartozik vagy más mondon hozzuk létre a GameObjectet(Instantiate()) void Awake() { //Az aktív Scene-en megkeressük a PlanetAttractor típusú objektumot és elmentjük a refenciáját _planet = FindObjectOfType <PlanetAttractor>(); //A GameObject-en lévő Rigidbody component referenciájának lementése _rbody = GetComponent <Rigidbody>(); //Mivel saját gravitációs erőt szimulálunk, ezért ne használja a Unity saját gravitációs rendszerét _rbody.useGravity = false; //A saját rendszerünk elforgatja megfelelően a vonzott testeket _rbody.constraints = RigidbodyConstraints.FreezeRotation; }
void Attract(PlanetAttractor objToAttract) { Rigidbody rbToAttract = objToAttract.rb; Vector3 direction = rb.position - rbToAttract.position; float distance = direction.magnitude; float forceMagnitude = gravity * (rb.mass * rbToAttract.mass) / Mathf.Pow(distance, 2); Vector3 force = direction.normalized * forceMagnitude; rbToAttract.AddForce(force); }
private void OnTriggerEnter2D(Collider2D hit) { if (hit.CompareTag("Planet")) { planeta = hit.GetComponent <PlanetAttractor>(); } if (hit.GetComponent <Limites>()) { Destroy(hit.gameObject); } }
private void OnCollisionEnter(Collision collision) { if (sediment < sedimentCap) { float chance = Random.Range(0, 100); if (sedimentRemoveChance < chance) { PlanetAttractor attractor = FindObjectOfType<PlanetAttractor>(); Vector3 planetPosition = attractor.gameObject.transform.position; float planetScale = attractor.gameObject.transform.localScale.x; if (!CheckIfInOcean(planetPosition, planetScale) && !(this.GetComponent<Rigidbody>().velocity.magnitude < 0.01f && timeAlive > 1)) { m = planet.GetComponent<MeshFilter>().mesh; mc = planet.GetComponent<MeshCollider>(); Vector3 raindropPositon = transform.position; verts = m.vertices; int vertIndex = 0; float shortestDistance = float.MaxValue; for (int i = 0; i < verts.Length; i++) { float distance = Mathf.Sqrt(Mathf.Pow(verts[i].x - raindropPositon.x, 2) + Mathf.Pow(verts[i].y - raindropPositon.y, 2) + Mathf.Pow(verts[i].z - raindropPositon.z, 2)); if (shortestDistance > distance) { shortestDistance = distance; vertIndex = i; } } Vector3 newVert = verts[vertIndex] * sedimentDissolution; newSediment = verts[vertIndex] - newVert; sediment += Mathf.Sqrt(Mathf.Pow(verts[vertIndex].x - newVert.x, 2) + Mathf.Pow(verts[vertIndex].y - newVert.y, 2) + Mathf.Pow(verts[vertIndex].z - newVert.z, 2)); //Debug.Log(sediment); //Debug.Log("--------------"); verts[vertIndex] = newVert; m.vertices = verts; mc.sharedMesh = m; m.RecalculateBounds(); planetScript.updateMesh(m); } } } }
void FixedUpdate() { PlanetAttractor attractor = FindObjectOfType<PlanetAttractor>(); Vector3 planetPosition = attractor.gameObject.transform.position; float planetScale = attractor.gameObject.transform.localScale.x; if (CheckIfInOcean(planetPosition, planetScale) || this.GetComponent<Rigidbody>().velocity.magnitude < 0.01f && timeAlive > 1) { m = planet.GetComponent<MeshFilter>().mesh; mc = planet.GetComponent<MeshCollider>(); Vector3 raindropPositon = transform.position; verts = m.vertices; int vertIndex = 0; float shortestDistance = float.MaxValue; for (int i = 0; i < verts.Length; i++) { float distance = Mathf.Sqrt(Mathf.Pow(verts[i].x - raindropPositon.x, 2) + Mathf.Pow(verts[i].y - raindropPositon.y, 2) + Mathf.Pow(verts[i].z - raindropPositon.z, 2)); if (shortestDistance > distance) { shortestDistance = distance; vertIndex = i; } } //Debug.Log(verts[vertIndex]); Vector3 newVert = verts[vertIndex] + newSediment; //Debug.Log(newVert); //Debug.Log(sediment); //Debug.Log("--------------"); //Debug.Break(); verts[vertIndex] = newVert; m.vertices = verts; mc.sharedMesh = m; m.RecalculateBounds(); planetScript.updateMesh(m); Destroy(this.gameObject); } else { timeAlive += 0.2f; } Attract(attractor); }
private void OnEnable() { planeta = null; }