c# - Unity camera starting position -
hello created camera rts game i'm making on start camera on minhight position , cant figure out how change hope helps code bit of mess test here code want start maxhight position given , not on minhight .
using system.collections; using system.collections.generic; using unityengine; public class rtscamera : monobehaviour { public float zoomsensitivy = 15; private transform m_transform; public float screenedgeborder = 25f; public bool usescreenedgeinput = true; public float screenedgemovementspeed = 3f; // zoom stuff public float heightdampening = 5f; public float scrollwheelzoomingsensitivity = 25f; public float maxheight = 10f; //maximal height public float minheight = 15f; //minimnal height public layermask groundmask = -1; public bool autoheight = true; public bool limitmap = true; public float limitx = 50f; //x limit of map public float limitz = 50f; //z limit of map private float zoompos = 0; //value in range (0, 1) used t in matf.lerp public bool usescrollwheelzooming = true; vector3 temppos; public string zoomingaxis = "mouse scrollwheel"; private float scrollwheel { { return input.getaxis(zoomingaxis); } } private vector2 mouseaxis { { return new vector2(input.getaxis("mouse x"), input.getaxis("mouse y")); } } private vector2 mouseinput { { return input.mouseposition; } } private void start() { m_transform = transform; } // update called once per frame void update () { move(); limitposition(); heightcalculation(); } private void move() { if (usescreenedgeinput) { vector3 desiredmove = new vector3(); rect leftrect = new rect(0, 0, screenedgeborder, screen.height); rect rightrect = new rect(screen.width - screenedgeborder, 0, screenedgeborder, screen.height); rect uprect = new rect(0, screen.height - screenedgeborder, screen.width, screenedgeborder); rect downrect = new rect(0, 0, screen.width, screenedgeborder); desiredmove.x = leftrect.contains(mouseinput) ? -1 : rightrect.contains(mouseinput) ? 1 : 0; desiredmove.z = uprect.contains(mouseinput) ? 1 : downrect.contains(mouseinput) ? -1 : 0; desiredmove *= screenedgemovementspeed; desiredmove *= time.deltatime; desiredmove = quaternion.euler(new vector3(0f, transform.eulerangles.y, 0f)) * desiredmove; desiredmove = m_transform.inversetransformdirection(desiredmove); m_transform.translate(desiredmove, space.self); } } private void limitposition() { if (!limitmap) return; m_transform.position = new vector3(mathf.clamp(m_transform.position.x, -limitx, limitx), m_transform.position.y, mathf.clamp(m_transform.position.z, -limitz, limitz)); } private void heightcalculation() { float distancetoground = distancetoground(); if (usescrollwheelzooming) zoompos += scrollwheel * time.deltatime * scrollwheelzoomingsensitivity; zoompos = mathf.clamp01(zoompos); float targetheight = mathf.lerp(minheight, maxheight, zoompos); float difference = 0; if (distancetoground != targetheight) difference = targetheight - distancetoground; m_transform.position = vector3.lerp(m_transform.position, new vector3(m_transform.position.x, targetheight + difference, m_transform.position.z), time.deltatime * heightdampening); } private float distancetoground() { ray ray = new ray(m_transform.position, vector3.down); raycasthit hit; if (physics.raycast(ray, out hit, groundmask.value)) return (hit.point - m_transform.position).magnitude; return 0f; } }
set value of variable zoompos
on line 25 either 1 or 0 depending on whether want @ min height or max height, either in start()
or @ declaration of variable.
this because in heightcalculation
function sets height somewhere between min , max based off value of zoompos
, use scrollwheel value changes , zoom. altering zoompos
before run game allows set default starting height.
Comments
Post a Comment