Similar to the gravity of an angry bird
Physics in Unity:
There is a real physical behavior, and your game objects must be affected by collision, gravity, and various other forces. Simulating this physical function is one of the main functions provided by Unity's physical engine (although not used by many large projects ).
Gravity:
Gravity brings together the power of all things. Material includes any tangible thing. Gravity is a linear proportional relationship. As a result, there are a lot of problems, and gravity produces centripetal force on the things around the flight. Quality is how we measure the quantity of things. The larger the case is, the higher the quality, the more gravity it produces. When we walk on the surface of the Earth. However, because the earth is much larger than us, and the gravity of the earth can cause us to fall on the ground, in addition to the quality used, gravity also depends on how far your things are. That's why we have an influence on the Earth's gravity in your game, not on the Earth's surface, but not on the sun's surface.
Demonstration Project:
Open Unity and go to File> New Project. Name the project and select 2D. Create a Scene with the Main Camera, the Sprite of the planet, the Sprite of the bird and the canvas containing the text, as shown below:
Images can be downloaded from the Internet and configured as follows:
Go to GameObject> UI> Text. You can enter your content. Here, I use the text in the game view as "Press Space ". Now, write a C # script for the bird that rotates near the planet, as shown in, and apply it to your bird.
Bird. cs:
using UnityEngine;using System.Collections;public class Bird : MonoBehaviour{ public Transform planet;private float forceAmountForRotation = 10;private Vector3 directionOfPlanetFromBird; private bool allowForce; void Start() { directionOfPlanetFromBird = Vector3.zero; }void Update (){ allowForce = false; if (Input.GetKey(KeyCode.Space)) allowForce = true; directionOfPlanetFromBird = transform.position - planet.position; transform.right = Vector3.Cross(directionOfPlanetFromBird, Vector3.forward);}void FixedUpdate (){if (allowForce) rigidbody2D.AddForce (transform.right * forceAmountForRotation);}}
Add the gravity that can pull the bird to the center of the planet, write a C # script as follows and apply it to your planet.
using UnityEngine;using System.Collections;public class Planet : MonoBehaviour{public Transform bird;private float gravitationalForce = 5; private Vector3 directionOfBirdFromPlanet;void Start (){directionOfBirdFromPlanet = Vector3.zero;}void FixedUpdate (){directionOfBirdFromPlanet = (transform.position-bird.position).normalized;bird.rigidbody2D.AddForce (directionOfBirdFromPlanet*gravitationalForce);}}
In this way, you can see the effect when running the Play scenario!