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 Mario15gl · Jul 03, 2015 at 01:54 PM · androidmobileaccelerometer2d platformer

How to use accelerometer in a 2d game

Hello,

I'm trying to make a new game for android and I need the character to move left and right when you move your phone left and right. I konw that there is a script called "AccelerometerInput" which is in the unity tutorials (I think). The problem is that it also moves the character in the z axis but I only want it to move in the x axis (left and right).

This is the script:

 using UnityEngine;
 using System.Collections;
 
 public class AccelerometerInput : MonoBehaviour 
 {
     void Update () 
     {
         transform.Translate(Input.acceleration.x, 0, Input.acceleration.y);
     }
 }

I would like to know if there is a script that works well or there is a way to change this script to make it the way I want it. I'm pretty sure this is a stupid question but I'm new on scripting.

Thanks in Advance.

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
0

Answer by moodymagyar · Sep 19, 2015 at 08:53 PM

@Mario15gl I would suggest removing the Input.acceleration.y as the third argument in transform.Translate() and replace it with 0. I hope that helps.

Comment
Add comment · Show 2 · 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 Mario15gl · Sep 19, 2015 at 09:11 PM 0
Share

Thanks, but I've already found another script that works for me:

 #pragma strict
 
  function Update () {
  
      var dir : Vector3 = Vector3.zero;
        
      dir.x = Input.acceleration.x*.5;
          
      transform.position.x += dir.x;
      }

I will check if your suggestions work anyways.

avatar image liveswithoutfear · Nov 14, 2018 at 06:20 PM 0
Share

Certainly helped me! You're a legend! I couldn't figure out why my character was disappearing off into the background, was hard to tell in 2D mode. Thanks.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera linked directly to device orientation 0 Answers

Using the accelerometer to move a player as the real user walks 2 Answers

accelerometer speed 1 Answer

Accelerometer - accounting for gravity for arbitrary device angle 1 Answer

Can you get the height that you are holding the phone? 2 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