About item pickup in Unity (Topic VI)

Source: Internet
Author: User

The principle is to make a trigger, the trigger is when we have a collision occurs, will only detect collisions, and there will be no change in the physical state of the process of movement.

The trigger is ideal for item pickup because it does not alter any physical properties of the original moving object, but still detects collisions and responds to physical events.

Item Pickup Instance

1. Create a Unity project and file directory to save the scene

2. Import the coin model resource Rc_fx_obj_04_mod. FBX and Obj_04_tex.png, set material ball shader to lagacy Shaders---->diffuse, color set to 255,255,255,255

3. Create a planar plane (100 times times the elongated z-axis) and a capsule body capsule, create an empty node prop, place the gold coin model under the prop node as a child node, and position each node as shown

4. Add a rectangular collider component to the Coin node prop box Collider, click Edit Collider, manually adjust the area of the collider, then tick the is trigger of the collider component as a trigger, Note that one of the two objects must be a rigid body before collision detection occurs, so add rigidbody components to the capsule body capsule, do not use gravity

5. Edit the layer in the upper right corner, add two layers, respectively, player and prop, set the capsule body capsule as the player layer, and the node prop as prop layer

6. Open the physics engine settings edit---->project Settings---->physics, set the collision matrix of the layer, only the player layer and prop layer will have collision detection

7. Scripting, generally based on the results of the collision, such as the player hit gold coins, gold value added to the Player node, so players need to hang a script to deal with this collision detection event, and gold coins hit the player, delete themselves, so gold also need to mount a script to handle collision detection events

8. Create two scripts prop and Recv_prop,prop mounted under the prop node, Recv_prop mounted under the capsule body capsule

Open Prop.cs

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine; Public classProp:monobehaviour { Public intProp_type;//types of props, different props can be set different types//Use this for initialization    voidStart () {}//Update is called once per frame    voidUpdate () {}voidOntriggerenter (Collider c)//The C point here is the node that hit me, not myself .{Debug.Log ("prop says:"+ C.gameobject.name +C.gameobject.layer); if(C.gameobject.layer = =8)//is the player layer of the node hit me, you can write some of the code to play the effect{Gameobject.destroy ( This. Gameobject); }    }}

Open Recv_prop.cs

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine; Public classRecv_prop:monobehaviour {//Use this for initialization    voidStart () {}//Update is called once per frame    voidUpdate () {}voidOntriggerenter (Collider c)//The C point here is the node that hit me, not myself .{Debug.Log ("players say:"+ C.gameobject.name +C.gameobject.layer); if(C.gameobject.layer = =9)//It's a node of the prop layer that hit me.{prop P= C.getcomponent<prop> ();//get the prop component of the node that crashed me            Switch(P.prop_type)//Judging Item Types            {                 Case 1: Debug.Log ("you picked up the gold! Score plus 1! ");//Different props to do different processing                     Break; }        }    }}

9. Create a script to control the capsule body capsule the positive direction of the z-axis, mount a script, move all nodes capsule

Open Move.cs

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine; Public classMove:monobehaviour {Private floatSpeed =5.0f;//speed of movement//Use this for initialization    voidStart () {}//Update is called once per frame    voidUpdate () {floats = This. Speed * TIME.DELTATIME;//distance traveled per second, unit meter         This. Transform. Translate (0,0, s);//Moving    }}

10. Running Results

Summarize
(1) Do props, set the device collider to trigger mode, only check the car collision, do not change the movement
(2) Players also add collider, players also have a rigid body, otherwise there is no way to trigger the collision;
(3) Players and props, grouping, do a good job of collision matrix settings;
(4) Write the response function of the collision, Ontriggerenter (Collider c), Ontriggerexit, Ontriggerstay; Judging the current touch is that type of object, Gameobjct.layer;
(5) Each prop, you can hang the same script, and then, inside with the type of props;
(6) Players pick up props, get the type of props, and deal with our game logic;
(7) The props, made of prefabricated body, combined with the map, or the generation of location, in the designated position to put props;

About item pickup in Unity (Topic VI)

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.