How to create a camera-window in Unity3D -
i'm new in unity , want create camera-window on website: http://gamasutra.com/blogs/itaykeren/20150511/243083/scroll_back_the_theory_and_practice_of_cameras_in_sidescrollers.php#h.elfjc4ap4hpe there example curb camera motion. want make camera-window pushes camera position player hits window edge.
any ideas, how realize this?
i used code:
using unityengine; using system.collections; public class cameracontroller : monobehaviour { public gameobject player; public vector3 min; public vector3 max; private vector3 offset; void start () { offset = transform.position - player.transform.position; } void lateupdate () { vector3 newpos = player.transform.position + offset; newpos.x = mathf.clamp(x, min.x, max.x); newpos.y = mathf.clamp(x, min.y, max.y); newpos.z = mathf.clamp(x, min.z, max.z); transform.position = newpos; } }
unfortunately, camera moving not correct. ideas, how create camera-window?
the main issue here need check targets position in screen space. object in screen coordinates. , move camera if target out of window in "screen coordinates". main function used here
camera.main.worldtoscreenpoint
following basic class should mimic effect in article. can improved alot should enough started in right direction.
using unityengine; using system.collections; public class curbcam : monobehaviour { public transform targetposition; public float camwindowdimension; vector2 targetscreenpos; float deltax; float deltaz; // use initialization void start () { } // update called once per frame void update () { //convert target pos 2d targetscreenpos = camera.main.worldtoscreenpoint (targetposition.position); if (targetscreenpos.x > (screen.width/2) + camwindowdimension) { deltax = targetscreenpos.x - ((screen.width/2) + camwindowdimension); transform.position = new vector3(transform.position.x + deltax, transform.position.y, transform.position.z); } if (targetscreenpos.x < (screen.width/2) - camwindowdimension) { deltax = targetscreenpos.x - ((screen.width/2) - camwindowdimension); transform.position = new vector3(transform.position.x + deltax, transform.position.y, transform.position.z); } if (targetscreenpos.y > (screen.height/2) + camwindowdimension) { deltaz = targetscreenpos.y - ((screen.height/2) + camwindowdimension); transform.position = new vector3(transform.position.x, transform.position.y, transform.position.z + deltaz); } if (targetscreenpos.y < (screen.height/2) - camwindowdimension) { deltaz = targetscreenpos.y - ((screen.height/2) - camwindowdimension); transform.position = new vector3(transform.position.x, transform.position.y, transform.position.z + deltaz); } } }
Comments
Post a Comment