• 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 AppFireDeveloper_DM · Jun 10, 2014 at 06:47 PM · tiltunity-androidmove speed

How to make move the player using the android device tilt

Hello everybody!

I really need your help cause I'm trying to create an android game based on the space shooter example of Unity, and I need to move the spaceship(player) using the tilt of the device. This is the code that I have used, but it doesn't work.. (the spaceship only rotates but doesn't move along the x and z axes as I want....)

Please help mee

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Boundary
 {
     public float xMin, xMax, zMin, zMax;
 }
 
 public class PlayerController : MonoBehaviour
 {
     public float speed;
     public float tilt;
     public Boundary boundary;
 
 
     void FixedUpdate ()
     {
         float moveHorizontal = Input.acceleration.x;
         float moveVertical = -Input.acceleration.z;
         
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         rigidbody.velocity = movement * speed;
         
 
         transform.position = new Vector3 
             (
                 Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 
                 0.0f, 
                 Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
                 );
 rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
     }
 }

In the Unity manual there's this code but I don't know how to use it (well I 've tryed but it didn't work..)

 transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
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 AppFireDeveloper_DM · Jun 13, 2014 at 04:15 PM 0
Share

I have a further problem: when I test the game using unity remote it works perfectly. But when i build the game and install it on my android phone the speceship doesn't and shoot (even if in the remote it worked!) How don't know at this point what's wrong with it.. Can you help me?

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ryanle · Jun 18, 2014 at 08:29 AM

I think you should be moving it on X and Y axis rather than X & Z

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

Answer by AppFireDeveloper_DM · Jun 16, 2014 at 12:38 PM

I've found out that the problem is another: the code works perfectly when i'm using unity remote , but when I build the game and install it on my device the spaceship doesn't move and shoot.. I've looked for a solution on the forum and unity answers but i havent found nothing useful.. Can you give some pieces of advice?

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
0

Answer by screenname_taken · Jun 12, 2014 at 07:51 AM

What is the

 transform.position = new Vector3 (
           Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 
           0.0f, 
           Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
           );

for?

If you have a rigid body that is not set to isKinematic you shouldn't try to move the gameobject. Just apply forces to the rigidbody. I see that you are trying to directly change the velocity of it.

Comment
Add comment · Show 4 · 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 AppFireDeveloper_DM · Jun 13, 2014 at 04:04 PM 0
Share

So what should I write in the code?

avatar image screenname_taken · Jun 13, 2014 at 09:43 PM 0
Share

Well, if "is$$anonymous$$inematic" is set to false, then just use rigidbody.AddForce (direction,Force$$anonymous$$ode.Impulse); (Put it in FixedUpdate(), not plain Update).

avatar image AppFireDeveloper_DM · Jun 13, 2014 at 10:28 PM 0
Share

Ok I'll try and I'll let you know thanks

avatar image AppFireDeveloper_DM · Jun 14, 2014 at 09:06 PM 0
Share

I've found out that the problem is another.. Look at the other comment

avatar image
0

Answer by darthbator · Jun 10, 2014 at 11:13 PM

Are you sure you're not accidentally multiplying speed by 0? I know a lot of people put unassigned public floats and ints in their scripts and then the editor initializes them as 0's. Are you getting bit by this in your playerController class there?

Comment
Add comment · Show 4 · 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 AppFireDeveloper_DM · Jun 12, 2014 at 07:20 AM 0
Share

Hi Darthbator!

The speed is not 0 because I've controlled it before building the apk. $$anonymous$$aybe have I to use the transform.Translate somewhere?

avatar image darthbator · Jun 12, 2014 at 05:41 PM 0
Share

If you have a ridigbody you want to call $$anonymous$$ovePosition on the body. This will maintain the integrity of the collider.

avatar image AppFireDeveloper_DM · Jun 14, 2014 at 08:56 AM 0
Share

Now there s another problem, the game in the remote works perfectly (the ship moves using the device tilt, shoots and rotates) but when I build the game, exporting the apk and then installing it on my device, it doesn't work! The ship doesn't move and shoot but all the other stuff in the sceneworks perfectly.. Do u know why I have this problem and how i can solve it ?

avatar image darthbator · Jun 23, 2014 at 10:27 PM 0
Share

You're going to want to get some output from the built game. Anything you print to debug.log prints to the android debug console. You can get the output form adb logcat and see that to see if the values are what you're expecting.

You could also add a guitext before you make the build if you don't feel like messing about with ter$$anonymous$$al arcana. Either way without knowing what kind of values are getting assigned into transform.position, movement, and speed it's going to be almost impossible to figure out what is going on. Just assign any value that will effect movement into a local variable and make sure to print it somewhere before you utilize it. Somewhere in there you're probably going to find a 0.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

I want to make tilt angle lock.. 3 Answers

Android tilt controls - taking player direction into consideration 2 Answers

Banking while turning 0 Answers

how to limit the rotation of an object by tilting the device? 0 Answers

how to change 2d image object when I tilt my smartphone 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