c# - Transform rotation to movement direction in 2D space -


i trying rotate gameobject facing in direction moving. not using rigidbody.velocity, using transform.position move object. here code:

public gameobject player;  void update ()  {     vector3 _origpos = new vector3(player.transform.position.x,player.transform.position.y, player.transform.position.z);      if (input.touchcount > 0)      {         // screen has been touched store touch         touch touch = input.gettouch(0);         if (touch.phase == touchphase.stationary || touch.phase == touchphase.moved) {             // if finger on screen, move object smoothly touch position             vector3 touchposition = camera.main.screentoworldpoint(new vector3(touch.position.x, touch.position.y, 10));                             transform.position = vector3.lerp(transform.position, touchposition, time.deltatime);             //transform.lookat(new vector3(touchposition.x,touchposition.y,0), vector3.up);             vector3 movedirection = gameobject.transform.position - _origpos;           }     } } 

any suggestions on how implement rotation? stumped

you using trigonometry. need 3 points of reference, luckily have 2 of those.

points needed:

  • a) going move.
  • b) current position @ center of mass.
  • c) tip of object, front.

calculate distance b c, call x. calculate distance b a, call y.

now use inverse tangent find angle.

angle = arctan(y/x) 

then apply object's rotation.

use world coordinates instead of local.

if use mathf inverse tangent, remember uses radians.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -