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); }
/// <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); }
public void Attract(GravityBody body) { Transform bodyTrans = body.transform; Rigidbody rbBody = body.GetComponent <Rigidbody>(); Vector3 gravityUp = (bodyTrans.position - transform.position).normalized; //Apply the gravity rbBody.AddForce(gravityUp * forceGravity * rbBody.mass); rbBody.drag = body.grounded == 0 ? 0.1f : 1.0f; //For the player controller if (rbBody.freezeRotation) { Quaternion q = Quaternion.FromToRotation(bodyTrans.up, gravityUp) * bodyTrans.rotation; bodyTrans.rotation = Quaternion.Slerp(bodyTrans.rotation, q, 0.1f); } }
public static GameObject CreateGravityBody(string name, Vector3 position, float mass, float scale, Color color, Vector3 initVelocity, bool trail = false) { GameObject gObj = GameObject.CreatePrimitive(PrimitiveType.Sphere); Rigidbody rb = gObj.AddComponent <Rigidbody>(); GravityBody gb = gObj.AddComponent <GravityBody>(); Renderer renderer = gb.GetComponent <Renderer>(); MaterialPropertyBlock props = new MaterialPropertyBlock(); if (trail) { // Trail gameobject as child of GB so they scale together GameObject trailGObj = new GameObject(); trailGObj.name = "Trail"; trailGObj.transform.parent = gObj.transform; // Create component under trail gameobject TrailRenderer tr = trailGObj.AddComponent <TrailRenderer>(); AnimationCurve curve = new AnimationCurve(); curve.AddKey(0.0f, 1.0f); curve.AddKey(0.35f, 0.35f); curve.AddKey(1.0f, 0.0f); tr.widthCurve = curve; tr.widthMultiplier = scale * 0.5f; tr.material = new Material(Shader.Find("Sprites/Default")); tr.time = 30f; } gObj.transform.name = name; gObj.transform.position = position; gObj.transform.localScale = scale * Vector3.one; gb.mass = mass; gb.initVelocity = initVelocity; renderer.material.shader = Shader.Find("Custom/DefaultShader"); props.SetColor("_Color", color); renderer.SetPropertyBlock(props); return(gObj); }