Gravity sensor control (Unity iPhone)

Source: Internet
Author: User
Solution 1: Speed
 
Public var simulateaccelerometer: Boolean = false; var speed= 10.0; function Update () {var dir: vector3 = vector3.zero; If (simulateaccelerometer) {dir. X = input. getaxis ("horizontal"); Dir. y = input. getaxis ("vertical");} else {dir. X = input. acceleration. x; Dir. y = input. acceleration. y; // clamp acceleration vector to unit sphereif (dir. sqrmagntasks> 1) dir. normalize (); // make it move 10 meters per second instead of 10 meters per frame ...} dir * = time. deltatime; // move objecttransform. translate (dir * speed );}

You can also change the speed into a force.

 
Solution 2: Force
Public var force: Float = 1.0; Public var simulateaccelerometer: Boolean = false; function fixedupdate () {var dir: vector3 = vector3.zero; If (simulateaccelerometer) {// using joystick input instead of iPhone accelerometerdir. X = input. getaxis ("horizontal"); Dir. y = input. getaxis ("vertical ");} else {// we assume that device is held parallel to the ground // and Home button is in the right hand // remap device acceleration axis to game coordinates // 1) XY plane of the device is mapped onto xz plane // 2) rotated 90 degrees around y axisdir. X = input. acceleration. y; Dir. y = input. acceleration. x; // clamp acceleration vector to unit sphereif (dir. sqrmagntasks> 1) dir. normalize ();} Rigidbody. addforce (dir * force );}

I personally feel that solution 1 is flexible and responsive. Solution 2 has inertia and obvious buffering.

Related Article

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.