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
3
Question by AlexanderKFF · Jun 01, 2012 at 03:07 AM · androidiphonejumpaccelerometertilt

Accelerometer question

I am developing an game similar to Doodle Jump, where the character continually jumps upwards from platform to platform. The user controls the character by tilting his device left and right. There is a very specific effect that I am trying to achieve, and I'm going to have to ask for help to pull it off.

The character's position on the screen should be determined by how the player is tilting the device. When the device is held upright, the character should be at the center of the screen. When the device is tilted left or right, the character should smoothly move left or right, but he should always return to the center of the screen when the device is held upright again.

I've tried to achieve this effect using rigidbody.AddForce and rigidbody.velocity, but neither of those two methods produces the desired effect.

How would I make an object move left and right when tilting a device, and return to the center of the screen when the device is not being tilted? My attempts to pull this off have been catastrophically bad, so my approach is probably wrong. Before I waste any more time barking up the wrong tree, I've come here to ask: How would I accomplish the kind of behavior that I'm describing?

Comment
Add comment · Show 1
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 hirenkacha · Jun 01, 2012 at 07:01 AM 0
Share

using Input.acceleration.y you will get angle (+/-) of tilt and get the multipliers for smoothing the character movement.

1 Reply

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

Answer by aldonaletto · Jun 01, 2012 at 09:23 AM

1- Using Input.acceleration: The accelerometer reports the current acceleration in G's as a Vector3 in its own coordinate system (the Z axis is reversed relative to Unity's):

alt text

If you want to run your game in landascape orientation (home button at the right hand), the docs' Input.acceleration example is a good starting point. This example assumes that the screen shows the XZ plane (a typical top-down configuration). If in your game the screen shows the XY plane instead (a typical side-scroller), the code should be modified a little, like below - it's also a good idea to filter the acceleration data (it's too jerky without filtering):

// Move object in XY using accelerometer (home button at right hand) var speed: float = 6.0; var filter: float = 5.0;

private var accel: Vector3;

 function Start(){
     accel = Input.acceleration;
 }
 
 function Update(){
     // filter the jerky acceleration in the variable accel:
     accel = Vector3.Lerp(accel, Input.acceleration, filter * Time.deltaTime);
     // map accel -Y and X to game X and Y directions:
     var dir = Vector3(-accel.y, accel.x, 0);
     // limit dir vector to magnitude 1:
     if (dir.sqrMagnitude > 1) dir.Normalize();
     // move the object at the velocity defined in speed:
     transform.Translate(dir * speed * Time.deltaTime);
 }


2- Character position in the screen: The behaviour you describe looks more like a camera follow script (like SmoothFollow), which will follow the player when it's trying to go out of screen.

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 AlexanderKFF · Jun 01, 2012 at 04:42 PM 0
Share

That was an extremely in-depth answer, Aldo! I can see why you have almost 23,000 karma.

This script isn't exactly what I was looking for, because it doesn't return the gameObject to the center of the screen when the device is held without any tilt. However, it definitely helps me learn how to fix the jerky acceleration. I'll try my hand at modifying this script to see if it'll suit my needs. If I succeed, I'll come back and mark this question as answered!

avatar image jithinrao · Dec 09, 2012 at 03:19 PM 0
Share

This was exactly what I was looking for, big thanks for the code snippet, could you please help me in limiting the movement within a range? Say only -5 to + 5 not beyond it.

avatar image aldonaletto · Dec 09, 2012 at 07:49 PM 0
Share

Limit the object position? If so, you can clamp the object position after translating it: define the max and $$anonymous$$ X and Y coordinates and use $$anonymous$$athf.Clamp - like this:

 var maxX = 5;
 var $$anonymous$$X = -5;
 var maxY = 5;
 var $$anonymous$$Y = -5;

     ...
     // move the object at the velocity defined in speed:
     transform.Translate(dir * speed * Time.deltaTime);
     var pos = transform.position;
     // clamp new position:
     pos.x = $$anonymous$$athf.Clamp(pos.x, $$anonymous$$X, maxX);
     pos.y = $$anonymous$$athf.Clamp(pos.y, $$anonymous$$Y, maxY);
     // and assign it to the object:
     transform.position = pos;
 }

This code limits the object movement to an axis aligned rectangle defined by $$anonymous$$X, maxX, $$anonymous$$Y and maxY.

avatar image jithinrao · Dec 10, 2012 at 03:57 AM 0
Share

Thanks a lot again for the direction. It practically worked really smooth, I have never used clamp before and it gave me a smooth limiting feel.

avatar image RACHITSINGH433 · Jul 20, 2019 at 03:38 PM 0
Share

I know I m too late :"(

Bt still can I get my confusion cleared. . I am using this script to move my player on X axis and it's working properly . I also want to rotate my object at certain angle same time while it's moving . Like if it is moving from right to left, rotate left And if it move left to right ,rotate right Can u please help me how to do this and what function I should use to do this with above script :")

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to convert Input.mousePosition into iphone/android accelerometer? 1 Answer

tilt control for jump 0 Answers

Add tilt function to rotate a ball android device 0 Answers

Making player object jump other than tapping the screen in android phones 1 Answer

The build version of my android game doesn't work (in unity remote it works perfectly) 0 Answers


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