- Home /
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
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.
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
}
}