Unity3d Practice Series 09, physics Engine and collision detection

Source: Internet
Author: User
Tags instance method

In Unity3d, an object usually consists of a collider and a rigidbody. Collider is a collision body, an object is a collider, before collision detection can be carried out. When the "is Trigger" attribute item in the collider component is checked, the object becomes a collision detector.

Objects have rigidbody components that have some physical properties, such as weight, resistance, rotational resistance, and so on. The "Use Gravity" item indicates whether there is a natural gravity. The "is Kinematic" entry indicates whether it is driven by the physical engine, and the tick indicates that it is not driven by the physical engine. The "Interpolate" entry represents the calculation difference. The "Collision Detection" entry represents the frequency of collision detection, "discrete" means discontinuous detection, and "continuous" indicates continuous detection.


Using scripts to control components

The physics engine can be controlled by a script, and now uses a script to control some physical properties.

A cube already has "box Collider", there is no "rigidbody" component, for which a script named "Physiccontroller" is attached.

Using Unityengine;
Using System.Collections;
[Requirecomponent (typeof (Rigidbody))]
public class Physiccontroller:monobehaviour
{
    Public Rigidbody RB;
    Use this for initialization
    void Start ()
    {
        RB = Gameobject.getcomponent<rigidbody>();
    }
    
    Update is called once per frame
    void Update () {
    
    }
}

At this point, a "rigidbody" component is automatically added to the cube.

Now, adding a force to the object can be achieved by rigidbody the Addforce instance method. Modify the "Physiccontroller" script as follows:

Using Unityengine;
Using System.Collections;
[Requirecomponent (typeof (Rigidbody))]
public class Physiccontroller:monobehaviour
{
    Public Rigidbody RB;
    Use this for initialization
    void Start ()
    {
        RB = Gameobject.getcomponent<rigidbody>();
        Rb. Addforce (0,0,1,forcemode.impulse);
    }
    
    Update is called once per frame
    void Update () {
    
    }
}

Remove the "Use Gravity" of the current "Rigidbody".

Save the running game, the cube in "Scene" moves in the z direction with some degree of force.

As we can see, a Forcemode enumeration is used in the "Physiccontroller" script, and the options for this enumeration include: acceleration, Force, Impulse,velocitychange. "A" for "acceleration", "F" for "Force", "I" for "Impulse", "V" for "Velocitychange", "M" for the object, and "T" for the event, the relationship between 2:

F = M * A
I = F * T = M * V

When the item selection for Forcemode is "acceleration", it is generally placed in the Fixedupdate method. Modify the "Physiccontroller" script as follows:

Using Unityengine;
Using System.Collections;
[Requirecomponent (typeof (Rigidbody))]
public class Physiccontroller:monobehaviour
{
    Public Rigidbody RB;
    Use this for initialization
    void Start ()
    {
        RB = Gameobject.getcomponent<rigidbody>();
        Rb. Addforce (0,0,1,forcemode.impulse);
    }
    
    Update is called once per frame
    void Update () {
    
    }
    void Fixedupdate ()
    {
        Rb. Addforce (0, 0, 1, forcemode.acceleration);
    }
}

Save the running game, you can see that the cube is shifted in the z direction in the way of acceleration, and the translation speed is getting faster.

Collision detection relationships between objects

The types of collisions include the following 6:

Static Collider Collider
Rigidbody Collider Rigid Body Collider
Kinematic rigidbody Collider kinematic Collider
Static Trigger Collider crash trigger
Rigidbody Trigger Collider rigid Body Trigger Collider
Kinematic rigidbody Trigger Collider kinematic rigid Body Trigger Collider

The collision relationship of these 6 types of collider is as follows, tick indicates that collisions can occur.

Visible

Static collider collides only with rigid body collider
A party to a collision must have a rigidbody, and a party that is colliding can have no rigidbody.
The Ontrigger method is called when the object is ticked on the trigger.

The following tick lists the circumstances in which the trigger occurred:

Ontrigger method and Oncollision method

Ontriggerenter () Called when Collider enters trigger
Ontriggerexit () Called when Collider stops triggering trigger
Ontriggerstaty () will be called at every frame when collider is de-trigger

Oncollisonenter () Called when collider/rigidbody triggers another rigidbody/collider
Oncollisionexit () Called when Collider/rigidbody stops triggering another rigidbody/collider
Oncollisionstay () When collider/rigidbody triggers another rigidbody/collider, it is called at each frame

Unity3d Practice Series 09, physics Engine and collision detection

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.