c# - Unity Camera Quaternion.RotateTowards without rolling/banking -


i have camera facing ground , want pan @ target object in distance.

currently, achieve following:

vector3 dir = targetpoint - transform.position; quaternion lookrotation = quaternion.lookrotation(dir); quaternion newrotation = quaternion.rotatetowards(transform.rotation, lookrotation, rotationdamping * time.deltatime); transform.rotation = newrotation; 

the camera performs rotation , ends pointing @ target object correctly, camera pans tilts 1 side making game world set @ angle viewer, pretty disorienting:

camera rotation example 1

how can constrain camera angle way horizon flat camera?

thanks!

update

adding line suggested @isaac below produces correct rotation in relation horizon, snaps abruptly z=0 @ start still not i'm looking for.

transform.localeulerangles = new vector3 (transform.localeulerangles.x, transform.localeulerangles.y, 0); 

camera rotation example 2

there excellent q/a on gamedev.stackexchange on subject. should try pitch/yaw system suggested there.
suggestion correct roll of camera during rotation.

public float rollcorrectionspeed;  public void update() {     float roll = vector3.dot(transform.right, vector3.up);     transform.rotate(0, 0, -roll * rollcorrectionspeed);      vector3 dir = targetpoint.position - transform.position;     quaternion lookrotation = quaternion.lookrotation(dir);     quaternion newrotation = quaternion.rotatetowards(transform.rotation, lookrotation, rotationdamping * time.deltatime);     transform.rotation = newrotation; }  

edit:
there easier solution: keep z rotation of quaternion rotating 0.

public void update() {     vector3 angles = transform.rotation.eulerangles;     quaternion = quaternion.euler(angles.x, angles.y, 0);      vector3 dir = targetpoint.position - transform.position;             quaternion = quaternion.lookrotation(dir);      transform.rotation = quaternion.rotatetowards(from, to, rotationdamping * time.deltatime); } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -