- Home /
How to control a weapon through a single mouse movement?
This is probably more complicated than it sounds, but I'm not sure.
I want to be able to show mouse on screen (easy, I can do this) but then I want it so when the user clicks and drags across the screen, the game remembers this movement for just a moment, and when the movement is finished, it sends an object with the same rotation down the same path the mouse took.
Is this do-able in any way...?
Answer by Daniel-Brauer · Aug 31, 2010 at 07:46 PM
This is definitely doable. It just requires that you program it in the same terms that Unity handles that sort of input. Using Update() and Input.GetMouseButtonDown/Up() to act on the beginning and end of the drag. You should compare Input.mousePosition at those two times, and then do whatever you want with the information.
Thanks very much, it sounds like I'll have to do a lot of research as to how to program the middle part of this, but you've given me a great place to start! Thanks!
Answer by Techsuport · Sep 04, 2010 at 09:09 PM
for cick, unclick, instantiate, rotate, then move based on obj forward
if(Input.GetButtonDown("Fire1")){//on left mouse click(if deafault settings r used)
var ray=Camera.main.ScreenPointToRay(Input.mousePosition);//the ray, from can to were u click
var hit=Raycast;//var the raycast hit info will go in
if (Physics.Raycast (ray,hit,100)){//raycast
Debug.DrawLine (ray.origin,hit.point);//shows line
//save hit.position
}
}
if(Input.GetButtonUp("Fire1")){//on left mouse click(if deafault settings r used)
var ray2=Camera.main.ScreenPointToRay(Input.mousePosition);//the ray, from can to were u click
var hit2=Raycast;//var the raycast hit info will go in
if (Physics.Raycast (ray2,hit2,100)){//raycast
Debug.DrawLine (ray2.origin,hit2.point);//shows line
//~ save hit2.position
//~ var obj=instanciate obgect
//~ var dir:Vector3=(hit2-hit.position).normalized;//will give direction from hit to hit2 as a vector3 with magnitude of 1
//~ var correntForward=obj.transform.TransformDirection(Vector3.forwardl;//local forward Vector3 of obj
//~ obj.rotation=Quaternion.Lerp(correntForward,dir,1);//rotate instantiated object, not tested btw, dis is sudo code :)
}//from rotation forward to dir
}
ASK MOAR QUESTIONS, LEANING IS IMPORTANT!!!