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
2
Question by teemujin · May 21, 2010 at 03:26 AM · iphoneaccelerometerlandscape

accelerometer script but used while device in landscape mode?

I have an accelerometer script that works great in portrait mode.

Tilt forward = move forwards Tilt back = move back Tilt left = move left Tilt right = move right

all good!

Now I changed things up a bit by forcing player into Landscape mode... I now have the device horizontal with button on right side. This is the mode I want to develop the game for...but the accelerometer script is now confusing me Im not sure how to modify it.

Now I get this from this slightly modified script:

Tilt forwards = move forwards ( ok! ) Tilt back = move backwards ( great! ) Tilt right = goes up?? Tilt left = goes down??

Im not really looking for Y acceleration...I think Im looking for Z rotation?? how do I find this?

How do I get this to:

Tilt right = goes right. Tilt left = goes left.

using UnityEngine; using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

// Use this for initialization void Start () {

}

// Update is called once per frame void Update () {

   if ( Application.platform == RuntimePlatform.IPhonePlayer ) 
   { 

      float z = iPhoneInput.acceleration.z * -1; 
      transform.Translate(0,0,z*8); 

      float y = iPhoneInput.acceleration.y;  // should i get some rotation?
      transform.Translate(0,y*8,0);

   } 
   else 
   { 

      float z = Input.GetAxis("Vertical"); 
      float x = Input.GetAxis("Horizontal"); 

      transform.Translate(0,0,z); 
      transform.Translate(x,0,0); 

   } 

} }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by BoredKoi · Jun 21, 2010 at 05:22 PM

Well for motion, all you need to do is flip your x and y readings around for the accelerometer when in landscape:

Vector3 accelerator = iPhoneInput.acceleration;

float currentXf = accelerator.x; // store current X accelerator.x = -accelerator.y; // X is now -Y accelerator.y = currentXf; // Y is original X

Measuring tilt is a little different however; I use this trig-fu in a Steering Wheel fashion; basically it's like a moving pointer on a protractor and based on the current reading +/- degrees relative to 270. You can do what you want from there (i.e., apply stronger steering force based on whatever Lerp formula you like via that offset). Note, you'll want the code above of course to set the x/y relationship correctly when in landscape.

float rotationAngle = Mathf.Rad2Deg * Mathf.Atan2(accelerator.y, accelerator.x);

if (rotationAngle < 0f) rotationAngle = rotationAngle + 360.0f;

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

No one has followed this question yet.

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