Unity Learning-Collision Detection (vii)

Source: Internet
Author: User

Unity Script Description

Unity Support Language:
C # (mainstream), Javascript,boo
To create a script:
"Assets-create-c# Script"

Learning Cases

Decorate the scene:
Add a plane,3 cube, a directional light
Set startup item (Preferences)
Import Unityvs, easy to use vs development unity
"Assets-import package-visual Studio Tools"
Write a script that lets the block move (move)
"Assets-create-c# Script"
Modify script with VS Open
"Edit-preferences-external tools-Select VS2013"

Writing scripts
Double-click the Move script, then open, write the script
Writing blocks moving Footsteps (Move.cs)
//Note whether the name of the move is the same as the name of your new script Public classmove:monobehaviour{//like a component-oriented interface, if it is public, you can see a property called Go in the component    PrivateGameobject go; //Initialization Program    voidStart () {//find the Game object named Cube4Go = Gameobject.find ("Cube4"); //set the color of the game object in the render componentGo.renderer.material.color =color.red; }    //The game runs one frame at a time, and each frame calls the update () method    voidUpdate () {//Check the keyboard at all times in each frame        if(Input.getkey (keycode.a)) {//Time.deltatime is the unit of the frame, here is 5 units//determine the upper and lower left and right of X, y by viewing the coordinate system//space.self Self-coordinate systemGo.transform.Translate (0,0,5*Time.deltatime, space.self); }        if(Input.getkey (KEYCODE.D)) {go.transform.Translate (0,0, -5*Time.deltatime, space.self); }        if(Input.getkey (KEYCODE.S)) {go.transform.Translate (-5*time.deltatime,0,0, space.self); }        if(Input.getkey (KEYCODE.W)) {go.transform.Translate (5*time.deltatime,0,0, space.self); }    }}
Pull the script into the camera and see in the Inspector

At this time, run the game, can be moved up and down through the w,a,s,d, but this game object bump together back overlap, we want the effect is can collide.
Set up a rigid body (rigidbody)
"Select Cube in Hierarchy, component-physics-rigidbody" Four cubes in the toolbar to set
Writing Collision Detection Footsteps (PZJC.cs)

The code is as follows
 Public classPzjc:monobehaviour {//Use this for initialization    voidStart () {}//Update is called once per frame    voidUpdate () {}/// <summary>    ///This method will be executed every time the game object collides/// </summary>    /// <param name= "CO" >can get a hit game object</param>    voidOncollisionenter (Collision co) {Co.gameObject.renderer.material.color=Color.Blue; }}
Because of the Cube4 of the operation, then pull the script to Cube4

Now run the game, action
Because CUBE4 first collided with plane, all plane will change color.

Unity Learning-Collision Detection (vii)

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.