Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Kinifi · Apr 20, 2010 at 05:35 AM · iphoneballroll

iphone roll a ball game

I'm just wanting to do something simple like hold the iphone sideways and roll the ball depending on where I tilt the iphone. There is a roll a ball tutorial or resource but the controls are completely backwards for it. I tilt forward and it rolls backward.

This is what I want:

I want to tilt the iphone and the ball gains momentum as I continue to tilt the phone.

Bounce off of the walls that I put in it.

I know this is very simple but I'm pretty new to everything since I switched from torque script so help convert me over to unity :D

Here is the code I'm using:

public var force:float = 1.0; public var simulateAccelerometer:boolean = false;

function Start() {

 iPhoneSettings.screenOrientation = iPhoneScreenOrientation.Landscape;

}

function FixedUpdate () { var dir : Vector3 = Vector3.zero;

 if (simulateAccelerometer)
 {
     dir.x = Input.GetAxis("Horizontal");
     dir.z = Input.GetAxis("Vertical");
 }
 else
 {

     dir.x = -iPhoneInput.acceleration.y;
     dir.z = iPhoneInput.acceleration.x;


     if (dir.sqrMagnitude > 1)
         dir.Normalize();
 }

 rigidbody.AddForce(dir * force);

}

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

Answer by duck · Apr 20, 2010 at 09:14 AM

If the ball rolls in exactly the opposite direction, simply add a minus sign ("-") in front of the force vector that you're applying.

If the ball isn't gaining momentum as you tilt the phone, perhaps you're not using AddForce - which should continue to add force each frame while the phone is tilted.

Another method of implementing a ball rolling game is to adjust the gravity vector based on the phone's tilt angle - perhaps your code uses this? In this case, to switch the direction that the ball rolls, you'd want to only negate the X and Z gravity direction, but not the Y value.

Without seeing any of the code you're actually using, it's hard to be any more specific about it, so if none of these ideas work for you, edit your answer to show some of the code you're currently using to make the ball move.

Edit: given your code sample above, you could try just invert the values from iPhoneInput like this, to reverse the ball motion:

    dir.x = iPhoneInput.acceleration.y;
    dir.z = -iPhoneInput.acceleration.x;
Comment
Add comment · Show 3 · 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 Kinifi · Apr 20, 2010 at 01:50 PM 0
Share

The code I'm using is above. Tell me what you think.

avatar image duck ♦♦ · May 19, 2010 at 08:50 AM 0
Share

added code example - let me know how it goes.

avatar image touchgamedev · May 19, 2010 at 03:03 PM 0
Share

This can not be answered any better. Duck has it dead on.

avatar image
0

Answer by RadhaUnity3d · May 09, 2013 at 12:22 PM

Use this since IphoneInput is old,,,,,,,,,

dir.x = Input.acceleration.y; dir.z = -Input.acceleration.x;

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

1 Person is following this question.

avatar image

Related Questions

A node in a childnode? 1 Answer

make a wedge moveable 0 Answers

How to roll a ball? 1 Answer

iPhone ball roll problem 2 Answers

Sphere rotation in moving direction 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