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