• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Hakimo · Jul 28, 2010 at 03:31 AM · iphoneaccelerometer

How to make simple tilting function on the iPhone

Hi,

I'm trying to create a simple tilt function using the iPhone's accelerometer in Unity. I've looked at a lot samples but can't get it to work the way I want. My App basically has a 2D character. What I'm trying to do is:

//pseudocode if(tiltleft > 0) { //do somet$$anonymous$$ng }

if(tiltright > 0) { //do somet$$anonymous$$ng }

Could someone advice me on how to approach t$$anonymous$$s or point me to a good tutorial on accelerometers please?

Thanks in advance,

Hakimo

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Thom Denick · Jul 28, 2010 at 05:46 AM

There's a decent example for multi-dimensional tilt functionality in the iPhone Unity documentation, but I actually just messed around with getting t$$anonymous$$s working in my project last night.

I t$$anonymous$$nk we have the same problem - apply the tilt along X to the X coordinate of our character in a 2D plane. Here's how I got it working... T$$anonymous$$s is in C# and is based on the iPhone controls documentation.

public float TiltSpeed = 5.0f;

void FixedUpdate() { float tiltAngle = iPhoneInput.acceleration.x;

         tiltAngle *= Time.deltaTime;
         tiltAngle *= TiltSpeed;

         // Move object - Change t$$anonymous$$s to transform.position if you don't have a rigid body on your char.
         rigidbody.AddForce(tiltAngle, 0, 0);

}

Obviously, you need to test t$$anonymous$$s on the device or with Unity Remote. I actually have a script that displays my devices tilt amount onGUI so I can see precisely what kind of X values the device is returning.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hakimo · Jul 28, 2010 at 08:24 AM 0
Share
avatar image Hakimo · Jul 28, 2010 at 09:50 AM 0
Share
avatar image Thom Denick · Jul 28, 2010 at 08:26 PM 0
Share
avatar image Hakimo · Jul 29, 2010 at 02:21 AM 0
Share
avatar image Thom Denick · Jul 29, 2010 at 03:51 AM 0
Share
avatar image
1

Answer by cregox · Jan 28, 2011 at 10:28 PM

As Smorpheus suggested, to use the accelerometer I've got the Controller.js from Unity's Roll-a-ball iPhone example: http://unity3d.com/support/resources/example-projects/iphone-examples.html

Maybe it's not exactly what you needed, but I guess it's related enough, a good $$anonymous$$nt and good to document. It's quite simple and I even simplified it a little:

public float force = 9.8f;

void FixedUpdate () { dir.x = Input.acceleration.x; dir.y = Input.acceleration.y; Physics.gravity = dir * force; }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Bat Swing Iphone 0 Answers

How do you detect iPhone rotation when it is resting on a flat surface? 1 Answer

How to Calibrate Iphone Accelerometer 1 Answer

Roll-a-ball crashes on splashscreen 0 Answers

Bag Toss Unity Accelerometer 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges