- Home /
Question by
TaliDen · Mar 16, 2014 at 06:55 PM ·
2djavascriptiosscreen
2D Sprite moving outside screen edges (side bumpers)
Hi all,
I'm having trouble navigating a ship sprite on a 2D iOS game. I have the ship moving left and right on the X axis on drag. However I added side bumpers to restrict the ship motion and prevent it from going outside of the screen edges. Now the ship does collide with the bumpers but then it's stuck there; once it touches the bumper it can no longer move on the other side again. Here is the code:
function Update () {
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved){
if (collided_with != null){
if (collided_with.tag == "Left" || "Right"){
return;
}
}
var pos = Input.GetTouch(0).position;
pos = Camera.main.ScreenToWorldPoint(pos);
transform.position.x = pos.x;
}
}
function OnCollisionEnter2D(col : Collision2D){
collided_with = col.gameObject;
}
function OnCollisionExit2D(col : Collision2D){
collided_with = null;
}
Both side bumpers have 2D box colliders attached, while the ship is a Rigidbody 2D and has got a 2D box collider attached as well.
Any help would be great. Thanks a lot.
Cheers!
Comment
Your answer
Follow this Question
Related Questions
Destroy Object On Collision? 3 Answers
Render text on sprite prefab - 2D iOS 4 Answers
2D games; Javascript or C#? 1 Answer
Problem with jumping 1 Answer