using UnityEngine; using System.Collections; using Random = UnityEngine.Random; using System.Collections.Generic; public class World : MonoBehaviour { [Header(">>>>>This is a script designed to create gravity fields, it can also be used as an area based normal gravity point and will orient it's targets towards the direction of the gravity")] [Header("Negative gravity atracts towards the gravity point, positive gravity pulls away from the center of gravity")] public float gravity = -10f; //public float Gravity_Radius = 1000f; [Header("it can also be used as an area based normal gravity point by toggling this variable")] public bool planetary_Attraction = false; [Header("You can also create an anomalous gravity field a bit by enabling this option")] public bool AnomalousAtraction = false; public void Attract_Global(Transform body){ Vector3 targetDir = (body.position-transform.position).normalized; Vector3 bodyUp = body.up; float XDir =0f; float YDir =0f; float ZDir =0f; if(body.gameObject.GetComponent()){ if(AnomalousAtraction){ if(Random.Range(0,1)==1){ XDir = targetDir.x; } else{ XDir = -targetDir.x; } if(Random.Range(0,1)==1){ YDir = targetDir.y; } else{ YDir = -targetDir.y; } if(Random.Range(0,1)==1){ ZDir = targetDir.z; } else{ ZDir = -targetDir.z; } Vector3 finalCalculation = new Vector3(XDir,YDir,ZDir); body.GetComponent().AddForce(finalCalculation*gravity); } else{ body.GetComponent().AddForce(targetDir*gravity); } body.rotation = Quaternion.FromToRotation(bodyUp, targetDir)* body.rotation; } } public void Simple_Gravity(Transform body){ Vector3 targetDir = this.transform.up; Vector3 bodyUp = body.up; //Vector3 targetDir = (Vector3.up); //Vector3 bodyUp = this.transform.up; if(body.gameObject.GetComponent()){ body.rotation = Quaternion.FromToRotation(bodyUp, targetDir)* body.rotation; } body.GetComponent().AddForce(targetDir*gravity); } /*private void Global_Atraction(){ Collider[] hitColliders = Physics.OverlapSphere(this.transform.position, Gravity_Radius); if(hitColliders!=null){ int c=0; do{ if(hitColliders[c].gameObject.GetComponent() || hitColliders[c].gameObject.GetComponent() || hitColliders[c].gameObject.GetComponent()){// if(hitColliders[c].gameObject.GetComponent()){ if(planetary_Attraction){Attract_Global(hitColliders[c].transform);} else{Simple_Gravity(hitColliders[c].transform);} } } c++;}while(c()){ if(planetary_Attraction){Attract_Global(other.transform);} else{Simple_Gravity(other.transform);} } } }