Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 something }

if(tiltright > 0) { //do something }

Could someone advice me on how to approach this 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 this working in my project last night.

I think 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... This 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 this to transform.position if you don't have a rigid body on your char.
         rigidbody.AddForce(tiltAngle, 0, 0);

}

Obviously, you need to test this 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

Wow, that looks easier than what I have tried. I even got to see the values as well. Although I'm a bit stuck at the moment with the if conditions. I'm using print(transform.position.x) to get the values. I tried to use: if(transform.position.x == -0.1) { //do something } Unfortunately nothing works. What value should the conditions be? I even used tiltAngle but nothing happens.

Thanks again,

avatar image Hakimo · Jul 28, 2010 at 09:50 AM 0
Share

It's alright, manage to do something. I didn't check the value properly for it to make any effect. Thanks again. Also, where did you learn about accelerators? The one example I followed earlier made so many variables that I didn't quite understand what it was doing :P

avatar image Thom Denick · Jul 28, 2010 at 08:26 PM 0
Share

This is all modified from the Help documentation in Unity under the iPhoneInput script reference. I do think the example could have been simpler, but I guess they wanted to show that generally for a ball rolling around you would want to be polling at least two coordinates.

avatar image Hakimo · Jul 29, 2010 at 02:21 AM 0
Share

Thanks again. Just wanted to understand more about the coding; How does the float TiltSpeed effect the tilt? I can't seem to see any dramatic changes. Also, when you assigned TiltSpeed to tiltAngle, does it override the Time.deltaTime? I didn't know you can assign 2 values to a variable. Thanks again for the help :)

avatar image Thom Denick · Jul 29, 2010 at 03:51 AM 0
Share

Hi Hakimo,

Float is a type of number that can hold decimals. Since the Input is a decimal number, it's perfect for holding the X Acceleration.

In the next two I'm multiplying the TiltAngle. $$anonymous$$ultiplying movement deltaTime is a way of ensuring that you move based on the time and not by Frames (which can vary by FPS). Second, I multiply by our tilt speed which is the variable that affects how much our character moves based on the tilt.

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 hint 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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Device Orientation Issue 1 Answer

moving an object with accelerometer using unity iphone. 3 Answers

External IMU data to control iOS game via BLE 0 Answers

accelerometer script but used while device in landscape mode? 1 Answer

Accelerometer question 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