1. Scenario and layout, as shown below:
2. Write a script to control vibration
/*** * Title: "SimpleUIFrame" UI Frame Project * Topic: about the vibration of the scene or object (the principle is to make the camera vibrate) * Description: * Function: Realize the overall vibration of the scene * Date:2017 * Version: Version 0.1 * Author:Coffee * Modify Recoder: */ using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; namespace SimpleUIFrame { public class MyScreenShake : MonoBehaviour { public Transform MainCamera; //Main camera (representing the whole scene vibration) public Transform NeedGameObject; //Object that needs to vibrate //The object moves or returns the identifier of the specified position private bool IsMove = false; private void Update() { //The whole scene vibrates if (Input.GetKeyDown(KeyCode.A)) { MyShake(MainCamera); } //Single object vibrates if (Input.GetKeyDown(KeyCode.B)) { MyShake(NeedGameObject); } } public void MyShake( Transform tf) { if (tf!=null) { //control vibration //Tweener tweener = tf.DOShakePosition(5); //Tweener tweener = tf.DOShakePosition(1,1); ////Indicates vibration in the X-axis direction //Tweener tweener = tf.DOShakePosition(11, new Vector3(1, 0, 0), 20); ////Indicates vibration in the Y-axis direction Tweener tweener = tf.DOShakePosition(11, new Vector3(0, 1, 0), 20); } } }//class_end }
3. Add the script to the script manager (create a new empty object in the scene and rename it to _ScriptMgr), and then assign the corresponding object to the script
4. Run the scene, then turn on uppercase, press the keyboard "A", the whole scene will vibrate, press the keyboard "B" to specify the object that needs to vibrate (here is a small ball) to vibrate