unity3d - Unity - How can I add or subtract a chosen number of degrees from an angle? -
i wondering if there's way add (in case) 120 degrees everytime push 'buttona', , subtract 120 degrees everytime push 'buttonb', z-axis rotation of 2d sprite (prefab).
this code i'm using @ moment, rotates once left, , once right:
function touchonscreen () { if (input.touchcount > 0) { var touch = input.touches[0]; if (touch.position.x < screen.width/2) { var rspeed = 10.0f transform.rotation = quaternion.lerp ( transform.rotation,quaternion.euler(0,0,120), time.deltatime*rspeed); debug.log("rotateright"); } else if (touch.position.x > screen.width/2) { var lspeed = 10.0f transform.rotation = quaternion.lerp ( transform.rotation,quaternion.euler(0,0,-120), time.deltatime*lspeed); debug.log("rotateleft"); } } }
thanks in advance!
note: please use unityscript if can, i'm pretty new coding , unityscript know far.
from online documentation shown signature of function is
static function lerp(from: quaternion, to: quaternion, t: float): quaternion;
that means second parameter new rotation of object not rotation offset
you should use of sort
function touchonscreen () { if (input.touchcount > 0) { var touch = input.touches[0]; if (touch.position.x < screen.width/2) { var rspeed = 10.0f transform.rotation = quaternion.lerp ( transform.rotation,transform.rotation + quaternion.euler(0,0,120), time.deltatime*rspeed); debug.log("rotateright"); } else if (touch.position.x > screen.width/2) { var lspeed = 10.0f transform.rotation = quaternion.lerp ( transform.rotation,transform.rotation + quaternion.euler(0,0,-120), time.deltatime*lspeed); debug.log("rotateleft"); } } }
note second parameter transform.rotation + quaternion.euler(0,0,120)
(current rotation + offset right)
i'm not expert on unity engine (i started toying free version yesterday, literally)
Comments
Post a Comment