Unity3d Novice Teaching, let you 12 hours, from getting started to mastering! (iii) [turn]

Source: Internet
Author: User

Copyright NOTICE: This article is Aries original article, reprint please indicate source. If you have any deficiencies, please feel free to comment or advise, contact QQ531193915

In this case, I'll teach you how to write the code for Collision detection, and then teach you how to use the first Unity plugin I've introduced to everyone.
Well, no nonsense to say, I first introduce how to download and install the plugin.

VS for Unity Download link

Go to this page, press the compiler version to download, I use 2010, so to download this.

I don't have to teach you how to install it, I'll start by looking at how I'm importing unity vs.

After clicking Import we will find that nothing has happened, but next we click on the refreshed shortcut:ctrl+f (F:flush) will see more than one thing above.
When you click Open , the VS is automatically opened.

OK, plug-in installation is complete, then teach you how to do collision detection.

A collision requires two conditions , one with a rigid body and one with a collider.

We've already added a collider to cube, and we've chosen the trigger for IS trigger, so we're going to start writing the code for Collision detection.

Create a new C # script named playercollision under the Scripts folder.

Before I write the code, I would like to introduce a lot of collision detection:
Ontriggerenter (Collider other) when entering the trigger
Ontriggerexit (Collider Other) when exiting a trigger
Ontriggerstay (Collider other) when stay trigger
Oncollisionenter (Collision collisioninfo) when entering the collider
Oncollisionexit (Collision collisioninfo) when exiting the collider
Oncollisionstay (Collision collisioninfo) as a stay collider

All of these six methods are Monobehaviour , because our scripts are inherited Monobehaviour classes. So our script can cover these six methods.

Because we checked the is Trigger, we covered the Ontriggerenter (Collider Other) method:

//当进入碰撞器的方法     public void OnTriggerEnter(Collider other)     {         //如果碰撞的GameObject不是Floor if(!other.gameObject.name.Equals("Floor")) { print("报告主人:触发器成功触发!"); } }

Then, give the code to the player and run the game.

, the collision did happen, but the player went through the cube.
This is not a good phenomenon!!!
So, let's move on to another method to remove the is trigger of the collider.
Then change the code to:

public void OnCollisionEnter(Collision other)    {        //如果碰撞的GameObject不是Floor        if (!other.gameObject.name.Equals("Floor")) { print("报告主人:触发器成功触发!"); } }

Well, then, someone might ask me, what's the use of the plug-in I imported at first?

Now I would like to introduce you, I am most happy to change this plugin one of the most favorite features!

When we edit the code in VS, we press the shortcut key ctrl+shift+q will pop up a box.

This includes all the classes we inherited from monobehaviour .

We enter on as a demonstration, and he will automatically index us all about the on method:

Is it very useful?

So now that our collision detection can be achieved, how should we implement the cube to stop the motion when we fall?

Casually ask a person who does not understand the programming will say, let him not move not move.

Yes, we will not be able to move the code of the Cube movement, so how can we cancel the cube motion code when the game is running ?

We move the leftcube to the floor front. As long as we write this in the event of a collision:

public Span class= "Hljs-keyword" >void oncollisionenter (Collision other) {//defines a string string collisionname; //if the collision of Gameobject is not floor if (!other.gameobject.name.equals (//the name of the collision body exists in the string collisionname = Other.gameObject.name; //if the collision body name is Leftcube if (collisionname.equals (" Leftcube ") {//gets leftcube script on the body and cuts off execution of this code. Other.gameobject.getcomponent<leftcubemove> (). Enabled = FALSE;} }

Then we run the game and make the player collide with the Leftcube.:

As you can see, in the moment of the collision, the cube does not move, and the script on the right leftcubemove is canceled.

So can we stop his movement by falling on the cube?

And not!

We can't always judge the name of the collider in every collision and then cancel the script.

Remember in the second saying I said:(never write two?) A separate code to the left, a separate code to the right? )

Now, let me solve the problem.

Create a new C # script that lives as Cubemove immediately. The code is as follows:

PublicClass Cubemove:monobehaviour {Defining the initial speed of a cubePublicFloat speed =1f;void Start () {}void Update () {If the script is in the same name as Leftcube (Clone) or Leftcubeif (This.gameObject.name.Equals ("Leftcube (Clone)") | |This.gameObject.name.Equals ("Leftcube")) {Execute the Move method MoveLeft (); }if (This.gameObject.name.Equals ("Rightcube (Clone)") | |This.gameObject.name.Equals ("Rightcube")) {MoveRight ();}if (This.gameObject.name.Equals ("Staticcube (Clone)") | | this.gameObject.name.Equals ("Staticcube")) {movestop ();}} //define Move method void MoveLeft () { //Make cube move faster speed + = 0.1f; //Mobile this.transform.Translate (vector3.left * speed * time.deltatime);} void MoveRight () {speed + = 0.1f; this.transform.Translate (vector3.right * speed * time.deltatime);} void Movestop () { this.transform.Translate (0,0,0);}}       

In this way, all of our available cubes can have a common script.

We remove all the cube-controlled moves, and then cubemove the script to all the cubes, and don't forget to give staticcube.

Then we modify the code of the Playercollision script:

    //当进入碰撞器的方法    public void OnCollisionEnter(Collision other)    {        //如果碰撞的GameObject不是Floor if (!other.gameObject.name.Equals("Floor")) { //取消碰撞体上的CubeMove方法 other.gameObject.GetComponent<CubeMove>().enabled = false; } }

That way, we can simplify the code and be easy to manage, and anyone can read your code!

Thank you for your attention, then this is the end of the story, and next I'll show you how to get the next cube when the player falls on the cube.

Unity3d Novice Teaching, let you 12 hours, from getting started to mastering! A

Unity3d Novice Teaching, let you 12 hours, from getting started to mastering! Two

Unity3d Novice Teaching, let you 12 hours, from getting started to mastering! (iii) [turn]

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.