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 kievar1983 · Mar 25, 2012 at 03:04 PM · androidaccelerometer

help setting up a boundary wall for android accelerometer

I'm making a simple catcher base game and i'm using the tilt function to move the catcher back and forth.

i'm trying to set up a boundary on the screen so the object can't go past it. so i made an empty game object and set a collision box on it and a rigid body (to be safe)

on my moving game object i set up a collision box and a rigid body. and froze the positions in the directions i don't want to go as the catcher only moves on one axis.

for the movement script i have the following:

 var speed : float = 10;
 var moveThreshold : float = .2;
 
 private var movex : float; 
 private var iPx : float; 
 
 var wall1:GameObject;
 var wall2:GameObject;
 public var distanceToWall1:float;
 public var distanceToWall2:float;
 
 
 function Start()
 {
    Screen.sleepTimeout =  SleepTimeout.NeverSleep;
    wall1 = GameObject.FindGameObjectWithTag("wall1");
    wall2 = GameObject.FindGameObjectWithTag("wall2");
 }
 
 function Update()
 {
     movex = 0;
     iPx = Input.acceleration.y;
     var distanceToWall1 = Vector3.Distance (this.transform.position, wall1.transform.position);
     var distanceToWall2 = Vector3.Distance (this.transform.position, wall2.transform.position);
 
     if (Mathf.Abs(iPx) > moveThreshold)
     {
         movex = Mathf.Sign(iPx) * speed;
         if (distanceToWall1 <=2)
         {
             if (movex <=0)
             {    
                 rigidbody.velocity = Vector3(0,0,movex);
             }
         }
         else if (distanceToWall2 <=2)
         {
             if (movex >=0)
             {
                 rigidbody.velocity = Vector3(0,0,movex);
             }
         }
         else 
         {
             rigidbody.velocity = Vector3(0,0,movex);
         }
     }
 }

I have tried add force, a raw translate of the object and updating the position directly. the results i get are the same. the object either forcibly passes through the wall or bounces back and offsets all the catchers (there can be up to three of them)

any help would be great

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by GagdetBoy · Mar 31, 2012 at 06:02 PM

You have to much going on. All you need to do is for the collision is import a cube, size it to what you want and unclick the mesh, to make it invisible. Then go to this website http://unity3d.com/support/documentation/Manual/Android-Input.html and scroll down to accelerometer and just take out the x & z lines of code.

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 clip911 · Oct 20, 2012 at 05:45 AM

Well i tried this and got a temp. solution. dont use collision or trigger events because they can give u r object unpleasant behavior. sometimes u r object can move out also. instead of that try this :

var dir : Vector3; // used for direction of an object.

var move : boolean = true; // used for condition to stop responding of movement if object go beyond limit.

private var speed : float = 50.0 ; // use for fast movements(needed in my game)

function Update() { dir.x = -Input.acceleration.y Time.deltaTime speed; // setting move for object in X direction only

  if(move == true) // accelerometer will work only in true condition
    {
       transform.Translate(dir.x); // [transform.Translate(Vector3(dir.x,0 , 0))] use if that dont work.
  //   ----------- move left and right ----------------------
 // i used values for temp. solution . however u can use screen width to //control 
 // the object. or u can place 2 objects and set them where ever u want & //calculate the distance.
  //notice the position difference                                                                                                      // should be as near as possible to                                                                                            // avoid shakes
 
     if(transform.localPosition.x < 0)   
     {
         move = false ;
         transform.position.x = 0.01 ;
         
         
     }
     
     if(transform.localPosition.x > 0)
     {
         move = true ;
     }
     
         if(transform.localPosition.x > 20)
     {
         move = false ;
         transform.position.x =  19.99 ;
         
         
     }
     
     if(transform.localPosition.x > 0)
     {
         move = true ;
     }
 
 // use can use it for up-down also What I Like in this stupid script is that
 // my object dont shake crazy when i tilt my MOBILE even on 90 deg. and above
 // I hope this could help
 CYA
 
    }

}

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

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Accelerometer Frequency on Android 0 Answers

Jet Accelerometer Script 1 Answer

How to make a menu like ironpants 0 Answers

Reloading the scene causes ads to reload. Is this ok ? 3 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