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
1
Question by TheBigBang · May 03, 2014 at 03:14 AM · androidcontrolsaccelerometertilt

Android Tilt controls help

So i'm developing a game where you can move the spaceship only on the x-axis like space invaders. So i am having a little trouble trying to implent tilt controls, i have google searched constantly and i looked into the unity manual, everytime i test out my game the ship disappears. Here is my code for movement right now i have it set for keyboard commands because the accelerometer wasnt working

 function Update () {
     
     
     if(Input.GetAxis("Horizontal") > 0){
         if (collided_with != null){
     if (collided_with.tag == "Left"){
 return;
 }
 }
 transform.Translate(Vector3(1 * speed * Time.deltaTime,0,0));
 }
 if(Input.GetAxis("Horizontal") < 0 ){
     if (collided_with != null){
 if (collided_with.tag == "Right"){
 return;
 }
 }
 transform.Translate(Vector3(-1 * speed * Time.deltaTime,0,0));
 }

If you can help thank you!

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
2
Best Answer

Answer by iamvishnusankar · May 03, 2014 at 06:22 AM

The reason of spaceship going off screen might cause you've haven't clamped the position to a particular value. Here's a solution, Hope this will help to solve your problem.

 function Update()
 {
     var dir : Vector2 = Vector2.zero;
     
     dir.x = Input.acceleration.x;
     dir.y = 0;
     
     // 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);
     
     //Clamp transform position
     //Here transform's x position will get clamped to a value of -3 to 3 change it as per your requirement
     transform.position = Vector2(Mathf.Clamp(transform.position.x,-3,3),
                                      0);
 }
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 TheBigBang · May 08, 2014 at 10:25 PM 0
Share

Thank you!

But just 2 things i forgot to mention im using portrait mode for my game and when i test out the script it worked but everything was invereted(ex. Left was right, and right was left) and it placed my gameobject out of the game scene where nothing could collide with it.

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

21 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

Related Questions

Face Direction of Tilting 1 Answer

How to calibrate my accelerometer? (available options not working) 0 Answers

Hi .... i made a pc game controls work fine....how do i use accelerometer for android...for ex if i wanna move right i tilt right...etc 1 Answer

Accelerometer question 1 Answer

roll ball in two axis using devices accelerometer. 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