private bool attract(Transform objectToAttract, out float distance, bool applyForce, bool hasToChangeFacing, float gravityMultiplyier) { //Only attract the body to the planet if it is close enough. distance = Vector3.Distance(transform.position, objectToAttract.position); GravityBody body = objectToAttract.GetComponent <GravityBody> (); distance = distance - sphereRadius; if (distance <= gravityDistance) { Vector3 targetDir = (objectToAttract.position - transform.position); targetDir = new Vector3(targetDir.x, targetDir.y, 0f).normalized; Vector3 objectUp = objectToAttract.up; if (hasToChangeFacing) { objectToAttract.rotation = Quaternion.FromToRotation(objectUp, targetDir) * objectToAttract.rotation; } float forceToAdd = -Constants.GRAVITY_FORCE_OF_PLANETS * Time.deltaTime; if (applyForce) { objectToAttract.GetComponent <Rigidbody>().AddForce(gravityMultiplyier * targetDir * forceToAdd, ForceMode.VelocityChange); } return(true); } return(false); }
private void Awake() { _gb = gameObject.GetComponent <GravityBody>(); _rb = gameObject.GetComponent <Rigidbody>(); StartMove(); }
public void Attract(GravityBody body) { Vector3 gravityUp; Vector3 localUp; Vector3 localForward; Transform t = body.transform; Rigidbody r = body.rigidbody; // Figure out the body's up vector if (useLocalUpVector){ gravityUp = transform.up; } else { gravityUp = t.position - transform.position; gravityUp.Normalize(); } // Accelerate the body along its up vector r.AddForce(gravityUp * gravity * r.mass); if (body.grounded == 1) { r.drag = 0.8f; } // Force object upright if (r.freezeRotation) { // Orient relative to gravity localUp = t.up; Quaternion q = Quaternion.FromToRotation(localUp, gravityUp); q = q * t.rotation; t.rotation = Quaternion.Slerp(t.rotation, q, 0.1f); localForward = t.forward; } }
void SpawnBattery() { GameObject clone = Instantiate(batteryPrefab, new Vector3(0, 0, 0), Quaternion.identity); clone.transform.localPosition = new Vector3(0, 0, 0); clone.transform.localScale = new Vector3(batteryScaleX, batteryScaleY, batteryScaleZ); clone.transform.localEulerAngles = new Vector3(0, 0, 0); clone.transform.position = transform.position; clone.transform.rotation = Random.rotation; clone.name = "LayedBattery"; BoxCollider boxCollider = clone.AddComponent <BoxCollider>() as BoxCollider; Rigidbody rigidBody = clone.AddComponent <Rigidbody>() as Rigidbody; rigidBody.useGravity = false; rigidBody.velocity = new Vector3(Random.Range(-velocity, velocity), 2, Random.Range(-velocity, velocity)); GravityBody gravityBody = clone.AddComponent <GravityBody>() as GravityBody; Pickable pickable = clone.AddComponent <Pickable>() as Pickable; pickable.countText = countText; BoxCollider triggerCollider = clone.AddComponent <BoxCollider>() as BoxCollider; triggerCollider.isTrigger = true; triggerCollider.size = new Vector3(batteryTriggerScaleX, batteryTriggerScaleY, batteryTriggerScaleZ); // Negate the constraints set by the GravityBody component. gravityBody.fixRotation = false; rigidBody.constraints = RigidbodyConstraints.None; }
public Vector2 GetBossPos(float rad, float checkRad) { if (chara != null) { int iter = 0; while (iter < 1000) { if (chara.closestPlanet != null) { GravityBody planet = chara.closestPlanet.GetComponent <GravityBody>(); if (planet != null) { Vector2 checkVector = new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f)).normalized; if (!Physics2D.OverlapCircle((Vector2)chara.closestPlanet.position + checkVector * chara.closestPlanet.GetComponent <GravityBody>().gravityDistance *rad, checkRad, planet.planetLayer)) { return((Vector2)chara.closestPlanet.position + checkVector * chara.closestPlanet.GetComponent <GravityBody>().gravityDistance *rad); } } else { return(GravityBody.GetSpawnPos(rad, checkRad)); } } else { return(GravityBody.GetSpawnPos(rad, checkRad)); } } } return(Vector2.zero); }
public void SetSceneToConfigPath() { DeleteAllGravityBody(); config = GetJsonNode(filePath); if (config == null) { return; } foreach (JSONNode node in config["bodies"]) { CreateGravityBody(node); } loadPathText.text = this.filePath; GravityBody.Initialize(); Vector3 cameraLookat = JSONNodeToVector3(config["cameraLookat"]); Vector3 cameraPosition = JSONNodeToVector3(config["cameraPosition"]); CameraLookat = cameraLookat; Camera.main.transform.position = cameraPosition; Camera.main.transform.LookAt(cameraLookat); Debug.Log("Reset to load state, " + this.filePath + config.ToString()); TextManager.GetInstance().UpdateCameraText(); }
// Use this for initialization void Start() { playerT = GameObject.FindGameObjectWithTag("Player").GetComponent <Transform> (); planetGravityBody = GameObject.FindGameObjectWithTag("Player").GetComponent <GravityBody> (); dialogue = gameObject.GetComponent <NPCDialogue>(); dialogue.checkConditions(3, 1); dialogue.hideText(); }
private void Start() { rb = GetComponent <Rigidbody>(); gb = new GravityBody(rb, SpinType.axis, mass); StartCoroutine(BulletDecay()); dir = new Orbision(); }
private void Start() { gb = new GravityBody(GetComponent <Rigidbody>(), SpinType.axis); mr = GetComponent <MeshRenderer>(); cldr = GetComponent <Collider>(); StartCoroutine(DelayedSpawn()); }
private void DeleteAllGravityBody() { GravityBody.CollectAllGB(); foreach (GravityBody gb in GravityBody.AllBodies) { DestroyImmediate(gb.gameObject); } }
public void Attract(GravityBody body) { Transform bodyTransform = body.transform; Vector3 direction = (bodyTransform.position - transform.position).normalized; bodyTransform.rotation = Quaternion.FromToRotation(bodyTransform.up, direction) * bodyTransform.rotation; body.rigidbodyRef.AddForce(direction * gravity, ForceMode.Force); }
public override Vector2 ApplyGravityForce(GravityBody planet) { //Get force direction Vector2 forceDirection = transform.position - planet.transform.position; //Calculate acceleration based on attractor's force and distance Vector2 acc = GRAVITYCONSTANT * (forceDirection.normalized * Force) / (Mathf.Max(forceDirection.sqrMagnitude, MIN_DIST_MULTIPLIER) * planet.Weigth); acc *= -1f; //inverse it return acc; }
/** * Applies attractive forces upon and uprights the given GravityBody. */ public void Attract(GravityBody body) { body.Gravitate(gravity); if (body.rigidbody.freezeRotation) { body.UprightPosition(uprightBodiesRate); } }
// -- Start -- void Start() { vehicle = GetComponent <Vehicle>(); transform = gameObject.transform; rb = GetComponent <Rigidbody>(); audioScr = GetComponent <HoverBikeAudio>(); gravBod = GetComponent <GravityBody>(); }
void OnTriggerExit(Collider co) { if (co.GetComponent <Orbital>()) { GravityBody gb = co.GetComponent <GravityBody>(); gb.SetAttractor(null); } }
void Start() { m_mySinker = FindObjectOfType <sinkingPlatform>(); m_myGravBody = FindObjectOfType <GravityBody>(); m_playerStateManager = FindObjectOfType <PlayerStateManagerript>(); m_rb = GetComponent <Rigidbody>(); m_isConnected = false; }
protected virtual void Attract(GravityBody gb) { if (gb != null) { Vector3 r_to_this = this.bodyTransform.position - gb.bodyTransform.position; Vector3 force = r_to_this * G_Runtime * gb.rb.mass * this.rb.mass / Mathf.Pow(r_to_this.magnitude, 3); gb.SetForceTo(force); } }
private void OnTriggerEnter(Collider other) { GravityBody gbOther = other.GetComponent <GravityBody>(); if (this.rb.mass > gbOther.rb.mass) { Destroy(other.gameObject); } }
private void setOrbitalVelocity(GravityBody body) { if (!orbitingMap.ContainsKey(body)) { return; } foreach (GravityBody orbitingBody in orbitingMap[body]) { Vector3 vector = orbitingBody.transform.position - body.transform.position; float forceMultiplier = 1f; if (forceMultMap.ContainsKey(body)) { forceMultiplier = forceMultMap[body]; } float initialDisplacementY = (float)(Math.Sin(orbitingBody.inclination * Math.PI / 180) * vector.magnitude) + body.InitialDisplacementY; orbitingBody.InitialDisplacementY = initialDisplacementY; orbitingBody.transform.Translate(Vector3.up * initialDisplacementY); vector = orbitingBody.transform.position - body.transform.position; Vector3 orbitNormal = Vector3.Cross(new Vector3(0, 1, 0), vector); orbitingBody.circularOrbit = orbitingBody.circularOrbit || orbitingBody.semiMajor == Vector3.zero; if (orbitingBody.circularOrbit) { orbitingBody.semiMajor = vector; } orbitingBody.Perihelion = vector.magnitude; orbitingBody.Apohelion = (orbitingBody.semiMajor.magnitude * 2) - vector.magnitude; float relativeVelocity = (float)Math.Sqrt( (Simulation.G * 2 * forceMultiplier * (orbitingBody.getMass() + body.getMass()) * orbitingBody.Apohelion) / (orbitingBody.Perihelion * (orbitingBody.Perihelion + orbitingBody.Apohelion))); float inverseMassRatio = (1 - (orbitingBody.getMass() / (orbitingBody.getMass() + body.getMass()))); orbitingBody.rigidBody.velocity = (orbitNormal.normalized * (relativeVelocity * inverseMassRatio)) + body.initialVelocity; orbitingBody.initialVelocity = orbitingBody.rigidBody.velocity; body.rigidBody.velocity -= orbitNormal.normalized * (relativeVelocity * (1 - inverseMassRatio)); body.initialVelocity = body.rigidBody.velocity; orbitingBody.transform.Rotate(orbitNormal, orbitingBody.axialTilt); orbitingBody.setRotationAxisVector(Vector3.up); setOrbitalVelocity(orbitingBody); } }
private void HandleSelectedObject(RaycastHit hitInfo) { if (selected.Count >= 2) { return; } GravityBody gb = hitInfo.transform.GetComponent <GravityBody>(); selected.Add(gb); }
private void Start() { rb = GetComponent <Rigidbody>(); gravityBody = GetComponent <GravityBody>(); settings = FindObjectOfType <GameSettings>(); raycastOrigin = raycastPoint.GetComponent <Transform>().localPosition; StartCoroutine("StartRotate"); }
private void Awake() { m_Transform = GetComponent <Transform>(); m_MeshRenderer = m_Transform.Find("Mesh").GetComponent <MeshRenderer>(); m_GravityBody = GetComponent <GravityBody>(); m_AudioSource = GetComponent <AudioSource>(); m_Rigidbody = GetComponent <Rigidbody>(); m_Rigidbody.useGravity = false; m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotation; }
private void FindNumberOfOrbitParents() { GravityBody target = orbitTarget; while (target != null) { numOrbitParents++; target = target.orbitTarget; } }
// Attract another gravitational body public void Attract(GravityBody gb) { // Vector from target body to this body Vector3 r_to_this = this.bodyTransform.position - gb.bodyTransform.position; // Newton's law of gravitation Vector3 force = r_to_this * G * gb.GetMass() * this.GetMass() / Mathf.Pow(r_to_this.magnitude, 3); gb.SetForceTo(force); }
// --- Awake --- void Awake() { gameObj = gameObject; transform = gameObj.transform; rigidbody = transform.GetComponent <Rigidbody>(); cameras = transform.Find("Cameras").gameObject; characterSpot = transform.Find("CharacterSpot"); gravityBody = transform.GetComponent <GravityBody>(); mountPoint = transform.position - transform.Find("HolderCollider").Find("CenterMountPoint").position; }
void SpawnChicken() { GameObject clone = Instantiate(chickenPrefab, Spawn, Quaternion.identity); clone.transform.parent = chickenFolder.transform; clone.transform.position = Spawn; clone.transform.localScale = new Vector3(45f, 45f, 45f); clone.transform.localEulerAngles = new Vector3(0, 0, 0); clone.name = "Chicken"; clone.layer = LayerMask.NameToLayer("Chickens"); SphereCollider sphereCollider = clone.AddComponent <SphereCollider>() as SphereCollider; Rigidbody rigidBody = clone.AddComponent <Rigidbody>() as Rigidbody; rigidBody.useGravity = false; GravityBody gravityBody = clone.AddComponent <GravityBody>() as GravityBody; gravityBody.fixRotation = true; WanderAround wanderAround = clone.AddComponent <WanderAround>() as WanderAround; wanderAround.speed = 2f; wanderAround.rotation = 3f; wanderAround.maxRotation = 10f; GameObject batterySpawner = Instantiate(new GameObject(), new Vector3(0f, 0f, 0f), Quaternion.identity); batterySpawner.transform.parent = clone.transform; batterySpawner.name = "BatterySpawner"; batterySpawner.transform.localPosition = new Vector3(0f, 0f, 0f); batterySpawner.transform.localScale = new Vector3(1f, 1f, 1f); batterySpawner.transform.localEulerAngles = new Vector3(0, 0, 0); BatterySource batterySource = batterySpawner.AddComponent <BatterySource>() as BatterySource; batterySource.countText = countText; batterySource.batteryPrefab = batteryPrefab; batterySource.batteryScaleX = 2f; batterySource.batteryScaleY = 4.5f; batterySource.batteryScaleZ = 2f; batterySource.batteryTriggerScaleX = 0.4f; batterySource.batteryTriggerScaleY = 0.2f; batterySource.batteryTriggerScaleZ = 0.4f; batterySource.speed = 2f; batterySource.velocity = 0.5f; batterySource.layingPercentage = 0.4f; AudioSource audioSource = clone.AddComponent <AudioSource>() as AudioSource; audioSource.spatialBlend = 1f; audioSource.clip = chickenAudioCLip; audioSource.loop = true; audioSource.Play(); }
void Awake() { //Obtain components rb = GetComponent <Rigidbody>(); playerRotation = GetComponent <PlayerRotation>(); raycastDetection = GetComponent <RaycastDetection>(); animator = GetComponent <Animator>(); respawnPos = transform.position; gravityBody = GetComponent <GravityBody>(); gravityBody.enabled = false; }
public override Vector2 ApplyGravityForce(GravityBody planet) { //Apply force toward Attractor Vector2 forceDirectionAtt = transform.position - planet.transform.position; //Get force direction Vector2 accAtt = GRAVITYCONSTANT * (forceDirectionAtt.normalized * Force) / (Mathf.Max(forceDirectionAtt.sqrMagnitude, MIN_DIST_MULTIPLIER) * planet.Weigth); //Calculate acceleration based on attractor's force and distance //Apply force toward planet's current velocity Vector2 forceDirectionVel = planet.Velocity; //Get force direction Vector2 accVel = VELOCITY_FORCE_MULT * accAtt.magnitude * forceDirectionVel.normalized; accVel = Vector2.zero; return accAtt + accVel; }
// Attract another gravitational body public void Attract(GravityBody gb) { if (gb != null) { Vector3 r_to_this = this.bodyTransform.position - gb.bodyTransform.position; // Newton's law of gravitation Vector3 force = r_to_this * G * gb.rb.mass * this.rb.mass / Mathf.Pow(r_to_this.magnitude, 3); gb.SetForceTo(force); } }
void SpawnWave(Wave waveToSpawn) { for (int i = 0; i < enemies.Length; i++) { for (int j = 0; j < waveToSpawn.amounts[i]; j++) { Instantiate(enemies[i], GravityBody.GetSpawnPos(), MathOperations.LookAt2D(transform.position, chara.transform.position, 90)); } } chara.ConfigurePointers(); chara.hp = chara.maxHp; }
/// <summary> /// Atrae hacia su centro el objeto especificado /// </summary> /// <param name="body"></param> protected void Attract(GravityBody body) { Rigidbody2D rb = body.GetComponent <Rigidbody2D>(); // Dirección de la gravedad Vector2 dir = ((Vector2)transform.position - rb.position); float distance = dir.magnitude; Vector2 force = dir.normalized * Gravity(distance); //Simula la gravedad rb.AddForce(force); }
private bool IsBodyParent(GravityBody otherBody) { //check if this body rocket and other body rocket parent Planet otherBodyPlanet = otherBody.GetComponent <Planet>(); if (rocket != null && otherBodyPlanet != null && rocket.ParentPlanet == otherBodyPlanet) { return(true); } return(false); }
public void Attract(GravityBody body) { Vector3 targetDir = (body.transform.position - transform.position).normalized; Vector3 bodyUp = body.transform.up; body.transform.rotation = Quaternion.FromToRotation(bodyUp, targetDir) * body.transform.rotation; if (!settings.isPaused) { if (body.useGrav) { body.getRb().AddForce(targetDir * gravity); } } }
void Awake() { rb = GetComponent <Rigidbody>(); planet = GameObject.FindWithTag("Planet"); air = planet.GetComponent <SphereCollider>(); ui = GameObject.FindObjectOfType <UIScript>(); distanceToPlanet = transform.position.y; body = GetComponent <GravityBody>(); animator = GetComponentInChildren <Animator>(); currentFuel = maxFuel; lightObject.spotAngle = maxLight; lightDecrease = lightDecreaseRate; }
/// <summary> /// Warps gravity body to the target wormhole /// </summary> /// <param name="bodyToWarp">Gravity Body to warp</param> /// <param name="bodyGameObject">GameOBject of gravity body (GO could be parent of gravityBody) </param> private void WarpToTarget(GravityBody bodyToWarp, GameObject bodyGameObject) { Vector2 randomDirection = Random.insideUnitCircle; //Disable collision and teleport bodyToWarp.DisableCollision(2f); ToggleTrailRenderer(bodyGameObject, false); bodyGameObject.transform.position = targetWormhole.warpPosition + (Vector3)randomDirection * 0f; ToggleTrailRenderer(bodyGameObject, true); //Apply Force bodyToWarp.Velocity = randomDirection * EXPULSION_FORCE; bodyToWarp.ResetGravityObjects(); //Spawn Visuals Instantiate(FX_Warp_In, transform.position, Quaternion.identity); Instantiate(FX_Warp_Out, targetWormhole.transform.position, Quaternion.identity); }
void Start() { gravityBody = GetComponent<GravityBody>(); gravityBody.applyForce = false; // populate an array with gameobjects containing a sprite points = new GameObject[25]; for (int i = 0; i < points.Length; i++) { points[i] = new GameObject("dot"); points[i].AddComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("dot"); points[i].GetComponent<SpriteRenderer>().color = new Color(1f, 1f, 1f, 0.25f); points[i].transform.localScale = new Vector3(0.1f, 0.1f, 0f); points[i].transform.parent = transform; points[i].transform.localPosition = Vector3.zero; } }
/* * Public attract class method */ public void Attract(GravityBody body) { // Create local variables Vector3 gravityUp; Transform trans = body.transform; Rigidbody rigid = body.rigidbody; // Set gravity local variable if (isSphere) { gravityUp = trans.position - this.transform.position; gravityUp.Normalize(); } else { gravityUp = transform.up; } // Apply forces to body rigid.AddForce(gravityUp * this.gravity * rigid.mass); // Check value of bodys grounded value if(body.Grounded >= 1) { // Set rigid body drag rigid.drag = 0.1f; } else { // Set rigid body drag rigid.drag = 1.0f; } // Check value of rotation freeze if(rigid.freezeRotation == true) { // Calculate quaternion Quaternion quatern = Quaternion.FromToRotation(trans.up, gravityUp); quatern = quatern * trans.rotation; // Calculate rotation of body trans.rotation = Quaternion.Slerp(trans.rotation, quatern, 0.1f); } }
/// <summary> /// Method called every FixedUpdate by objects affected by this gravity modifier /// </summary> /// <param name="planet">object affected by this modifier</param> /// <returns>force to apply to object</returns> public override Vector2 ApplyGravityForce(GravityBody planet) { //Calculate acceleration based on planet's weigth Vector2 acc = GRAVITYCONSTANT * gravityForceFactor * FORCE; if (!INDEPENDANT_FROM_MASS) acc /= planet.Weigth; return acc; }
void Start() { rb2d = GetComponent<Rigidbody2D>(); gravityBody = GetComponent<GravityBody>(); }
// Use this for initialization void Start() { PlayerGravityBody = this.gameObject.GetComponent<GravityBody>(); }
public void Pull(GravityBody GB) { if(PullStrength == 0){Debug.LogWarning(this.gameObject.name + "Gravity Attractor Pull Strength is equal to zero!");} GB.Velocity += -( (Vector3.Normalize(GB.transform.position - this.transform.position) * PullStrength)/GB.Mass) /Mathf.Pow(Vector3.Distance(this.transform.position,GB.transform.position),GM.FallOffPower); }
/// <summary> /// Method called every FixedUpdate by objects affected by this gravity modifier /// </summary> /// <param name="planet">object affected by this modifier</param> /// <returns>force to apply to object</returns> public abstract Vector2 ApplyGravityForce(GravityBody planet);
void Awake() { rigidbody.freezeRotation = true; rigidbody.useGravity = false; gravBody = gameObject.GetComponent<GravityBody>() as GravityBody; }
// Use this for initialization void Start() { gravBody = gameObject.GetComponent<GravityBody>() as GravityBody; }
void Awake() { rigidbody = GetComponent<Rigidbody2D> (); gravityBody = GetComponent<GravityBody> (); }
// Use this for initialization void Start() { gravityBody = mainBody.GetComponent<GravityBody> (); }