Realization of screen vibration effect of unity3d game development

Source: Internet
Author: User

Dear friends, everyone, welcome to pay attention to my blog, I am Qin Yuanpei, my blog address is http://qinyuanpei.com. Today, let's talk about how to achieve the screen shake effect in Unity3d. Screen shake effect in some game scenes, to enhance the real sense of the game is of great help. For example, in the "Fruit Ninja" this game, when the player hit the bomb when the screen will have a strong vibration effect, while the mobile phone itself to vibrate, so that greatly enhance the player in the moment of the bomb hit the game experience.

Again such as "Swordigo" this game is Bo Master has been more like a mobile phone game, in this game players will play a young swordsman, across the barrier, experience all kinds of hardships, collect sword hilt, sword blade, sword Heart finally defeated the devil. This is a complete RPG game, in the game players will be in different styles of the maze to explore, and then eliminate all kinds of enemies, is a very good mobile game, if you are interested, you can download it to play a play Oh! In this game there is a fire attribute of the enemy, it will throw a fireball to the player, when the fireball touches the ground, the entire ground will be violent vibration, this is also to reflect a real game experience.

We know that the size of the space in the 3D world can be infinitely extended, and the space that our sight can reach is relatively limited. We often say we want to broaden our horizons, because broadening our horizons allows us to see more content. So what does this say in the game world? We know that in the 3D world the camera plays the same role as the human eye, so how much we can see in the 3D world depends entirely on the camera. Well, with this in mind, we can roughly find two ways to implement screen shaking:

Vibration camera in case of fixed scene

The first class of methods is simpler to implement, and this approach is easier to accept. Because we usually in the process of photographing or filming will inevitably appear because the photographer's body or hand jitter caused by the picture vibration effect, so now the camera equipment such as DV or digital camera or even a smart phone, there will be image anti-jitter function. This is the first class to implement the screen vibration method in the life of the real example, so based on this idea, to achieve the screen vibration as long as the camera to shake.

Vibration scene with camera fixed

The second method is a simulation of the real world, the main simulation is the Earth's earthquake disaster. This approach is mainly used in 3D games, by making the virtual ground in the game vibrate, to achieve a picture of the effect of vibration. However, because this approach may involve other objects in the scene and the limitations of the main use of 3D games, the first class of methods in 2D and 3D games are used primarily to achieve screen shake effects.

Project examples

Here is the game of "Fruit Ninja" under the Unity3d engine, for example, this is a project that bloggers have been studying recently. In this game mid-range players hit the bomb when there is a screen shake effect, so here we take the implementation of this function as an example to explain today's content it!

First, the first method is to adjust the camera's rect viewport range to achieve screen shake, which is the method that bloggers use in this game.

    ///The   main function in this script is the vibration effect of the camera      ///Date: April 8, 2015    //   Whether shaking the camera     Public BOOLIsshake=false;//   <summary>     //   Vibration Interval    //   </summary>      Public floatShaketime=1.0F//   <summary>     ///   Vibration Increment    //   </summary>      Public floatShakedelta=0.1F//   <summary>     ///   record current frame time    //   </summary>     Private floatFrametime=0.0F//   <summary>     //   FPS    //   </summary>     Private floatfps=20.0F//   <summary>     ///   from the start vibration timing    //   </summary>     Private floatTime=0.0F//   <summary>     ///   camera Shake method    //   </summary>     Private void Shakecamera()    {if(Isshake) {time+=time.deltatime;if(shaketime>0) {shaketime-=time.deltatime;if(shaketime<=0){//Reset cameratransform.camera.rect=NewRect (0.0F0.0F1.0F1.0f);//Reset Vibration intervalShaketime=1.0F }Else{//In order to make the camera shake smoothly and evenly design a frame timeFrametime+=time.deltatime;//Only vibrate when frame time is greater than FPS                    if(frametime>=1/fps) {//Reset Frame TimeFrametime=0;//Randomly adjust the camera rectangleTransform.camera.rect =NewRect (0.1F * (-shakedelta +2.0F * Random.value),0.1F * (-shakedelta +2.0F * Random.value)                            ,1.0F1.0f); }                }            }        }

Of course, we can take a simple and crude approach, so that the position of the camera is constantly changing, so we can adopt the second method is:

     Public BOOLIsshake=false;//define the amplitude of the camera    Private floatShakedelta=0.75F//Vibration interval     Public floatShaketime=1.0FvoidUpdate () {if(Isshake) {if(shaketime>0) {shaketime-=time.deltatime;if(shaketime<=0) {Shaketime=1.0F transform.position=NewVector3 (0,0, transform.position.z); }Else{transform.position=NewVector3 (-shakedelta+1* Random.value,-shakedelta+1* Random.value, transform.position.z); }            }        }    }}

However, from the actual effect of comparison, the first way to achieve better than the second, perhaps Bo master himself write bad bar, haha. OK, now let's take a look at the actual effect!

If you have better methods or ideas, welcome to the article below the message, today's content is this, I hope you like!

Realization of screen vibration effect of unity3d game development

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.