Mathematics and Physics in Unity game development 4 (adding gravity to object motion), unity Game Development

Source: Internet
Author: User

Mathematics and Physics in Unity game development 4 (adding gravity to object motion), unity Game Development

Adding gravity to object motion

Note:

  • Use the series superposition algorithm y + = vy
  • Acceleration on location vy + = GR
  • The acceleration GR on the speed is the gravity acceleration. The unit of acceleration used in the computer is the special dot/F ^ 2 (pixel/square frame), instead of the 9.8 Mbit/s ^ 2
    (Meters/square seconds)
  • Differentiation is an operation that is used to evaluate the differential coefficient. A differential coefficient refers to the change rate of a function about a specific value. This concept is often used in game development to evaluate a function change.
Using UnityEngine; using System. collections; // Add the gravity public class ParabolicMotionTest: MonoBehaviour in the object motion {// float posX = 0 in the X position of the object; // float posY in the Y position of the object = 0; // float speedX = 0 for the object in the x direction; // float speedY =-8 for the object in the y direction; // The upper right pixel of the screen is in the coordinate Vector3 ScreenRightTopPos Of The World Space; // the lower left pixel of the screen is in the coordinate Vector3 ScreenLeftBottomPos Of The World Space; // The half width float boxHalfWidth of the box; // run time float runTime; // gravity acceleration const float GR = 0.5f; // The Birth position of the object Vector2 bornPos; void Start () {// convert the pixels on the right of the screen to the coordinates of the World Space ScreenRightTopPos = Camera. main. screenToWorldPoint (new Vector3 (Screen. width, Screen. height, 0); // converts the pixels in the lower right of the screen to the coordinates of the World Space ScreenLeftBottomPos = Camera. main. screenToWorldPoint (new Vector3 (0, 0, 0); // The half width of the box, because the box is a square boxHalfWidth = transform. localScale. x * 0.5f; // the upper left corner of the birth location. bornPos = new Vector2 (ScreenLeftBottomPos. x + boxHalfWidth, ScreenRightTopPos. y-boxHalfWidth); // the initial position. transform is located in the upper left corner of the screen. localPosition = new Vector3 (bornPos. x, bornPos. y, 0);} void Update () {// detection. if the object goes out of the interface, send it to the bornPos point if (posX-boxHalfWidth> = ScreenRightTopPos. x | posX + boxHalfWidth <= ScreenLeftBottomPos. x | posY-boxHalfWidth> = ScreenRightTopPos. y | posY + boxHalfWidth <= ScreenLeftBottomPos. y) {// The runTime is set to 0 runTime = 0f;} // The displacement to the value posX = speedX * runTime + bornPos in each frame time. x; // posY = (0.5f * GR * runTime) + (speedY * runTime) + bornPos. y; // set the transform position of the frame. localPosition = new Vector3 (posX, posY, 0); // Time change value: runTime + = Time. deltaTime ;}}

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.