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
0
Question by Databases · Oct 12, 2014 at 07:35 AM · inputaccelerometeracceleration

Please, can anyone explain this (input.acceleration)

Hi, everyone

can anyone please explain the following code:

using UnityEngine;

using System.Collections;

public class ExampleClass : MonoBehaviour {

 public float speed = 10.0F;
 void Update() {
     Vector3 dir = Vector3.zero;
     dir.x = -Input.acceleration.y;
     dir.z = Input.acceleration.x;
     if (dir.sqrMagnitude > 1)
         dir.Normalize();
     
     dir *= Time.deltaTime;
     transform.Translate(dir * speed);
 }

}

I want an explanation because it did work but not properly when i tried it in my game, so understanding may help me understand what is the issue.

Thank you all for your time.

Source: http://docs.unity3d.com/ScriptReference/Input-acceleration.html

Comment
Add comment · Show 3
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 fafase · Oct 12, 2014 at 08:18 AM 0
Share

We would also like some explanation on what is not working.

avatar image Databases · Oct 12, 2014 at 09:12 AM 0
Share

so when i start the scene the object just starts moving by itself in different directions and i have no control over it. Notice that the mobile is put on flat surface without me touching it, i guess the object should not move unless i touch the mobile.If i try to move the object using the mobile it doesn't move in the proper directions so what explains that irregular behavior?

$$anonymous$$ay u please try the code and see if it works?

I think if i understand the code i would be able to fix it :)

Thank you for your participation.

avatar image semaj678 · Mar 09, 2015 at 06:34 PM 0
Share

I have had this problem for ages and would love an explanation! $$anonymous$$y character only moves when the line of code: transform.Translate(dir * speed) is put in. It similarly spins uncontrollably.

3 Replies

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

Answer by tanoshimi · Oct 12, 2014 at 09:47 AM

With comments...

 // Assume that device is held parallel to the ground 
 // and Home button is in the right hand 
 // remap device acceleration axis to game coordinates: 
 // 1) XY plane of the device is mapped onto XZ plane 
 // 2) rotated 90 degrees around Y axis
 dir.x = -Input.acceleration.y; 
 dir.z = Input.acceleration.x;

 // Clamp acceleration vector to unit sphere 
 if (dir.sqrMagnitude > 1)
   dir.Normalize(); 

 // Make it move 10 meters per second instead of 10 meters per frame...
 dir *= Time.deltaTime; 

 // Move object
 transform.Translate (dir * speed);
Comment
Add comment · Show 1 · 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 Databases · Oct 16, 2014 at 08:23 PM 0
Share

Thank you for the explanation, it helps a lot.

avatar image
1

Answer by Lovelock · Oct 12, 2014 at 08:53 AM

I believe Input.acceleration returns data from anything using an accelerometer. That is, an input device moving through 3D space in real-life that can report that info to Unity. Something like a phone with an accelerometer or any other peripherals that have one.

What exactly are you wanting to do with this code?

It won't necessarily do anything if you don't have the proper inputs, or if it's not running on a device with an accelerometer.

If you want basic input for keyboard / gamepad, try

 float xInput = Input.GetAxis("Horizontal");
 float yInput = Input.GetAxis("Vertical");



Comment
Add comment · Show 1 · 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 Databases · Oct 12, 2014 at 09:08 AM 0
Share

Thank you for your participation, actually it did work but not properly, sorry i didn't mention that. I'm looking for an explanation for the dir.x and dir.z why we did change the values?

avatar image
1

Answer by semaj678 · Mar 10, 2015 at 08:48 PM

I have just recently discovered how to do this. The code is wrong. transform.Translate for me at least doesn't work. Use: GetCompenent().AddForce(dir); Please let me know if this has helped.

Comment
Add comment · Show 1 · 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 M-E · Jul 19, 2015 at 10:23 PM 0
Share

Hello first , it may be an old question but i think transform.Translate didnt work cuz the values are small but force is additive so it can make the object move

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

31 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Input.acceleration always returns zero on specific project 4 Answers

Detect if an acceleration sensor exist 1 Answer

Incremental acceleration with Imput 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Device Orientation Issue 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