- Home /
This question was
closed May 06, 2015 at 07:48 AM by
alok-kr-029 for the following reason:
solved
Question by
alok-kr-029 · Mar 20, 2015 at 07:42 AM ·
dragmousedrag
Drag a object along curves
Hi I want to drag an 2d object along a curvy 2dpath
if the player drag in right path the object gets drag or else not , check the file attached
what I tried is on mouse drag I raycast through the object and checked if it hit the path then move else not but its not working smoothly and the object freezes
RaycastHit2D hits;![alt text][1]
hits = Physics2D.Raycast(transform.position, -Vector3.back, 10000f,7) ;
if(hits.collider != null && hits.collider.tag== "path" )
{
Lastpos = hits.point;
Debug.DrawRay(transform.position,-Vector3.back,Color.green,Time.deltaTime*10);
print (hits.collider);
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position= new Vector3(pos.x,pos.y,-0.1419618f);
}
.
[1]: /storage/temp/42981-pathline.png
pathline.png
(119.1 kB)
Comment
Answer by Utamaru · Mar 20, 2015 at 09:36 AM
Try to set a path as array of Vector3s, and every time mouse changes position, find closest point from array to your mouse's position. If its smaller then some constant allowable value - move your object, otherwise don't do anything.
but my levels will be generated dynamcally .. how can i set an static path vector