About the use of the physics engine in Unity

Source: Internet
Author: User

The physics engine controls the rigid body, the rigid body above the patch, so it looks like the game is running up state

The physical world calculates the operating state of a rigid body and shows the result of the operation by mapping.

First, the physical engine

The 1:unity 2D physics engine is based on the box2d package;
2: Physics engine simulates physical motion and computation, physics engine helps us to calculate object motion;
3: acceleration of gravity;
4: Collider is the shape of the object + collision physical parameters (physical material);
5: Rigid body controls the movement and force of the object;

Second, step

1. Create a canvas

2. To initialize the canvas, remember to adjust the resolution of the game view to the same resolution set in the canvas 640x960

3. Create an image UI node as a child node of the canvas, called BG, and drag the background image into the node.

4. Create an image of the UI node called the child node of item, color is set to red

5. Add a rigidbody 2D component to the item node and then run it with this component, and you will see the item node fall down

Physical parameter settings for 6.Rigidbody 2D components:

Useautomass/mass: Automatically calculated based on density * area, rigid body mass.
Linear Drag: Linear damping, air resistance, etc.
Angular Drag: Rotational damping, rotational resistance.
Gravity scale: Gravity scaling factor, 1 is the Earth's gravity, 0.6 is the moon's gravity.
is kinematic: static rigid body and dynamic rigid body, static rigid body cannot move, dynamic rigid body can move.
Sleep mode: Rigid body hibernation. Normal: The rigid body is not affected by any external forces, the default is to enter the dormant state, will not participate in the operation, never sleep never sleep, and the default is start awake, wake-up starts.
Collistion Detectionmode: The mode of rigid-body collision detection (continuous detection/discrete detection). Discrete discrete, continues continuous.

Note: A rigid body such as bullets, collistion detectionmode Motion mode to choose continues continuous, so that not because the speed is too fast, the update interval is too short to cause the bullet to penetrate other rigid bodies, the mechanism is to determine whether the movement during the collision of a rigid body, If there is, put the state back to the state before the collision, resulting in a wall effect.

In other cases, the default selection is discrete discrete.

7. Just add a rigidbody 2D component called a rigid body, the rigid body is not any shape. There is no collision between rigid bodies without shapes, and they go straight through, like ghosts. Two objects want to achieve collision effect, must have box Collider components, any one without Collider components are not, will go straight through.

Only collider components, no rigidbody components are not, it will be equivalent to a static object without force, but will and have a collider group has a collision effect produced.

We need to add another component called Box Collider 2D (Rectangular collider) to represent the shape of a rigid body, in fact there are various collider such as Circle Collider Circle Collider and so on.

The green box is the effective area for collision detection. The Size property (radius radius) of the box Collider component is resized and is generally set to the same size as the picture size.

Physical parameter settings for 8.Box Collider 2D:

Material: material, the material that represents the surface of the collision object, the physical material is the difference between the rainy day ground and the cement ground

Is trigger: whether only as a trigger, when this property hooks, then other rigid body collision to it, will only trigger the collision event, no collision effect generated, that is, the system detects a collision, but not on the screen display of the collision process, will go straight through. Can put some through the portal, gold coin is trigger hook,

This way we can achieve the desired effect, that is, there is a collision event, but there is no impact of the picture effect.

Create a physical material

Right--->create--> physics2d Material, physical material has two properties, bounciness elasticity and friction friction.

Once you have created the physical material, you need to drag it to the material property of the corresponding box Collider component, and the bounciness elasticity is 0.5 to see that the object bounces for a while after the collision.

About the use of the physics engine in Unity

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.