Unity3d Study Notes Collision Detection

Source: Internet
Author: User

Many people may encounter the following problem when they are new to unity3d: import the unity3d model and add the Rigidbody to the model.

Keep falling.


So how can we solve this problem?


If you have tried to create a cube or something, add the Rigidbody. After running the program, you will obviously find that the cube can be dropped to the ground.


Therefore, if we want our model to do the same, we can bind a cube to the model, adjust the collision volume of the cube, and remove the partition of the cube.

Dyeing. In this way, we can let the model enjoy the cube collision detection without seeing the cube.


1. Import resources: assets -- import package -- terrain assets, and import system terrain maps and other resources.

2. Add model: create a model folder under assets and put your 3D model into the model.


3. Create a terrain: terrain -- create terrain. Create a terrain and click paint

Select a texture icon and adjust tile size X and tile size Y.

As shown in:


4. Drag the model to unity3d scene editing and adjust its position. If you add any direction light, it looks pleasing to the eye. At this time, if you give the Model

Add the Rigidbody. Although it has gravity, it will not collide with terrain.




5. Create a collision body: gameobject -- create other -- cube. Place the cube in the center of the model (other locations are acceptable). You can see the cube

There is a box collider object. Adjust the center and size of the box collider, that is, the collision range (green rectangle), so that the collision range conforms to the entire model.

Type. We recommend that you adjust the orientation of X, Y, and Z axes respectively ..




6. In the hierarchy view, drag the model (I am a tank here) to the cube. Then, click cube, component -- physics -- Rigidbody to give the cube

Add a rigid body. In the cube attributes, remove the mesh Renderer check, that is, do not render the cube.



7. Run the program and you will find that the model has been securely placed on terrain.


Although this example is very simple, everyone has a basic understanding of the collision ideas of the model (using cube, sphere and other gameobjects for collision, the effect is

).

When I learned this, I think that my shoes will think about it. What if it is a weird model?

Of course, you can also use it as a shape such as cube or sphere for collision, but the accuracy may be poor. For example, the tank in the above example is the top

The left side of the gun header is empty.

In this way, we can create multiple cubes for combination to increase the collision accuracy. You can try it on your own.

Try.


Reprinted please indicate the source: http://blog.csdn.net/ml3947


PS: When I was in college, I wrote a blog for a while and communicated with many Java learners. But after work, I haven't written a blog for a long time, but suddenly

At present, every it cool will basically stick to blogging and share with you. I have been working for a year. I am not a good man, but I also hope to become

A cool man. We will continue to write blogs in the future!

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.