Similar to the gravity of an angry bird

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.