How to control an animation by touch position along a path in Unity3D? -


i have gameobject want animate along specific path/curve, animation should controlled mouse/touch position. when touch/click on gameobject , move finger/mouse on/near path (or maybe easier move down) gameobject should follow defined path.

i itween, think not possible find solution using here, right?

edit: added image: enter image description here

it's quite simpler task might think.

basically it's question of remapping function (that takes input parameter) function (that express position along path). there several ways of doing that, depending on precise effect want implement.

the important choices have take are:

  • how describe path/curve
  • how handle input

example

for path easy , flexible way use sort of spline curves, such cubic bézier curve. it's easy implement , unity3d provides built-in functions draw them. have @ handles.drawbezier.

basically bézier function takes input parameter t in domain [0,1] , return result point in space (2d or 3d prefer). b(0) gives point @ begin of curve, b(1) end point. (side note: function not linear in general case incrementing @ constant rate t doesn't produce movement @ constant speed along curve. paper might useful).

for concern input simpler solution comes mind following:

  • accumulate somewhere vector describing offset position when touch started current touch position. (here's how handle touches, have @ deltaposition).

something like:

if (input.touchcount > 0 && input.gettouch(0).phase == touchphase.moved)  {   offsetfromstartpos += input.gettouch(0).deltaposition; } 

let's want swipe up/down finger moving forward/back object along path.choose "travel" distance (the domain of input function) finger in order complete movement along curve , normalize offset using such distance in order remap input [0,1] domain.

float t = offsetfromstartpos.y / maxdistancealongyaxis; vector3 pos = calculatebezier(t); transform.position = pos; 

it's hint put in right direction.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -