Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Shake function of Unity3D Mobile Terminal

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to realize the shaking function of Unity3D mobile terminal". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to realize shaking function of Unity3D mobile terminal".

The shaking function of mobile phone is a very common requirement in usual project development, which can be easily realized by using the gravity sensor of Unity.

Unity simplifies the development of gravity sensing by accessing the Input.acceleration property to retrieve the value of the accelerometer. First of all, let's look at the direction of the gravity sensor. The value of weight in Unity3D ranges from-1.0 to + 1.0.

X axis: the home button rotates 90 degrees to the right in the face of the lower phone, and the gravity component rotates 90 degrees to the left.

Y axis: the home button on the top phone is facing away from you, and the gravity component is + 1.00.The gravity component of the lower phone facing you is-1.0mm.

Z axis: the gravity component of the phone facing the ground is + 1.0. The gravity component of the phone facing the sky is-1.0.

Gravity components can be obtained in unity by using the Input.acceleration 's xQuery yrez property:

Gravity component of Input.acceleration.x; gravity induction X axis

Gravity component of Input.acceleration.y; gravity induction Y axis

Gravity component of Input.acceleration.z; gravity induction Z axis

The shake function code is as follows:

Using UnityEngine;using System.Collections; public class CellphoneVibrate: MonoBehaviour {/ / record the Y value of the last gravity induction private float old_y = 0; / record the Y value of the current gravity induction private float new_y; / / the current mobile phone shaking distance private float currentDistance = 0; / / the effective distance of the mobile phone shaking public float distance; void Update () {new_y = Input.acceleration.y; currentDistance = new_y-old_y; old_y = new_y If (currentDistance > distance) {/ / realize the shaking and shaking effect of mobile phone Handheld.Vibrate ();}

The above is all the contents of the article "how to shake the Unity3D mobile terminal". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report