- Home /
problem solved
throw objects based on mousemovement
Hi,
I want to throw an object into the distance when left button is released based on mousemovement (Like in Black and White: http://www.youtube.com/watch?feature=player_detailpage&v=sVSu-U1osQo#t=1009s).
I made a script (posted below) which lets you pick up the object it is attached to and throw it forward. However it is based on the mouseposition the moment you let go, I'd rather have it go further based on how fast you moved the mouse. I have no idea how to start, so if someone could point me into the right direction, that would be great!
the script:
bool move = false;
// Update is called once per frame
void Update ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 500))
{
if(hit.collider.gameObject == gameObject && Input.GetMouseButton(0))
move = true;
if(move)
{
Vector3 goTo = new Vector3(hit.point.x, 2, hit.point.z);
gameObject.transform.position = Vector3.Lerp(transform.position, goTo, Time.deltaTime * 100);
}
}
else
{
move = false;
}
}
void OnMouseUp()
{
move = false;
rigidbody.AddForce(Camera.main.transform.right * Input.GetAxis("Mouse X") * 15f, ForceMode.Impulse);
rigidbody.AddForce(Camera.main.transform.up * Input.GetAxis("Mouse Y") * 20f, ForceMode.Impulse);
}
}
EDIT: It would seem that delta mouse position is received by Input.getAxis("Mouse X/Y"). I confused it with horizontal/verical. So I'm guessing the distance already depends on how hard you move the mouse? I'm not really happy with the result..
Answer by Rheeenz! · Jan 04, 2013 at 03:15 PM
Hi, I'm only a beginner when it comes in programming. I want to ask if where can i put that code? you have posted above so that I can try it. Cheers! :)
$$anonymous$$ake a script, paste the code in it and attach the script to the object you want to throw.
Closing this question now..
Follow this Question
Related Questions
Throwing a ball, target unrecognized? 0 Answers
Can I have a rigidbody continue to calculate physics while being dragged by code? 2 Answers
How to throw objects/grenades in looking height? 1 Answer
Need Help! 0 Answers
Drag and throw Rigidbody2d? 3 Answers