Friday, 15 January 2010

vector - Unity3D - 2D object rotation based on touch moved (diff between touches) -



vector - Unity3D - 2D object rotation based on touch moved (diff between touches) -

i´m newbie in unity. want rotate 2d object based on user touch moved (moved finger on screen). have code:

void update () { if (input.touches.length > 0) { t = input.gettouch (0); if (t.phase == touchphase.moved) { vector3 movepos = new vector3 (t.position.x, t.position.y, 0); var objectpos = camera.main.worldtoscreenpoint (transform.position); var dir = movepos - objectpos; transform.rotation = quaternion.euler (new vector3 (0f, 0f, mathf.atan2 (dir.y, dir.x) * mathf.rad2deg)); } } }

this code rotate object based on user touch when touch screen 1 time again in position , touch move, rotate whole object actual touch , right object rotation based on touch move.

and dont´t want rotate whole object based on touch position rotate object based on touch move. understand me? can help me? how should rewrite code?

if understand you, seek utilize code below:

private float turnspeed = 5f; private vector2 movement; void update() { vector2 currentposition = transform.position; if (input.touchcount > 0) { touch touch = input.gettouch(0); if (touch.phase == touchphase.moved) { vector2 movetowards = camera.main.screentoworldpoint(touch.position); motion = movetowards - currentposition; movement.normalize(); } } float targetangle = mathf.atan2(movement.y, movement.x) * mathf.rad2deg; transform.rotation = quaternion.slerp(transform.rotation, quaternion.euler(0, 0, targetangle), turnspeed * time.deltatime); }

let me know if want. also, there finish sample here: https://github.com/joaokucera/unity-2d-object-rotation

vector rotation unity3d

No comments:

Post a Comment