c# - Keeping my object from rolling out of the screen -
i using tilt function on phone control object roll left , right. not want roll beyond screen width.
as in simple illustration below, dotted lines represent width of screen. 'o' object , max signs indicate maximum point object allowed roll to.
max--------o--------max
but using code, object still rolls out of screen. tried testing both height n width , ended same result object rolls out of screen. please advice doing wrong. thank you.
public float speed = 10.0f; void update() { vector3 dir = vector3.zero; dir.x = input.acceleration.x; if (dir.sqrmagnitude > 1) dir.normalize(); dir *= time.deltatime; if (!(mathf.round(dir.x) > mathf.round(screen.width/2)) || !(mathf.round(dir.x) < -mathf.round(screen.width/2))) { transform.translate(dir * speed); } } **updated public float speed = 10.0f; void update() { vector3 dir = vector3.zero; dir.x = input.acceleration.x; if (dir.sqrmagnitude > 1) dir.normalize(); dir *= time.deltatime; //transform.translate(dir * speed); if (transform.position.x < 0) { transform.position = new vector3(0, this.transform.position.y, this.transform.position.z); } else if (transform.position.x > screen.width) { transform.position = new vector3(screen.width, this.transform.position.y, this.transform.position.z); } else { transform.translate(dir * speed); } }
i think thereare several ways can go checking transfomrs position.
first off if using 2d camera use method like
leftbounds = camera.x - (camera.pixelwidth/2);
however because ortho camera angles not set @ particulare size @ x distace camera hard calculate.
i have seen instances coliders on camera outside render rand placed camera children
adding colision mask affect appropriate game object best.
Comments
Post a Comment