- Home /
Throw an object on collision with mouse cursor
Hi all,
I hope the header isn't too confusing, it's difficult to describe such matter in such short length :)
Anyhow, what I want to do is not supposed to be too difficult.
I got a scene where my camera is above a table and it is pointing towards the table from above, so the camera is actually showing a top view of the table.
On the table I have a ball laying still.
I need to allow the user to hit the ball while gaining momentum from one of the sides surrounding the ball, that way the ball would fly to the opposite direction.
For example as shown below - 1. The user holds down the mouse on the right side of the table, far from the ball. 2. The user moves the mouse cursor while still holding the mouse button towards the ball 3. The ball collides with the mouse cursor, and depending on how "hard" it was hit, it should move to the other side faster/slower.
One thing to keep in mind - I need a method that will not be hard to port to Android/iOS touch interface in the future.
Thanks!!
Answer by syclamoth · Oct 24, 2011 at 04:52 AM
The way I would do this, would be to have an empty object which gets moved to the mouse position (using some combination of screenPointToRay and Physics.Raycast) every frame. In this object, I would keep a Vector3 variable which saves the position from the last frame (resets every time) and then use Physics.Linecast to determine 'collisions'.
To determine the speed, it's a simple as working out the instantaneous velocity of the object every frame, something like this-
Vector3 velocity = (transform.position - lastPosition) / Time.deltaTime;
Then I would use a rigidbody on the ball, and then inside the Linecast function, I would do something like this-
hit.rigidbody.AddForceAtPosition(velocity * someConstant, hit.point, ForceMode.Impulse);
Hey syclamoth,
Thanks for the answer!
Your suggestion is certainly a good start. So far I implemented the code as follows - I used a RayCastHit from my mouse position to detect collision with my ball. When the RayCast hits the ball I get the collision information and I'm able to throw the ball away, depending on where I'd hit its collider.
Few things I'd like to improve:
With the current solution the player can throw the ball away on his first click of the mouse, without applying a continuous drag motion with the mouse. I'd like to prevent the ball from being thrown unless the player applied a continuous drag mouse motion before hitting the ball.
I guess since my ray is thin, sometimes if I move the mouse too quickly, the ray skips the ball without hitting its collider. What would be the best way to fix this problem?
Thanks!
1- That's a bit of a hard one, because it doesn't quite make sense, physically. You could have the 'someConstant' value (which is basically the apparent mass of the 'finger') increase over time- which means that a longer touch will have a bigger effect. Of course, that would allow the player to just put their finger down right next to the ball, wait a $$anonymous$$ute, and then achieve massive power with the tiniest touch. You could make it dependent on distance? I'm really not sure what the right solution to this is, since the mouse cursor can't have inertia, because it's basically non-physical!
2 is why I use a linecast from the previous position to the current position, ins$$anonymous$$d of trying to extrapolate velocity. The only real difference is that collision detection happens one frame later. Also, try putting that stuff into FixedUpdate (if you're not already!).
Your answer
Follow this Question
Related Questions
Moving GameObjects with mouse and check contains 0 Answers
Player movement for iOS? 1 Answer
Trying to get Mecanim to work well with Touch Controls 1 Answer
Player glitching through wall when sprinting 1 Answer
2D Movement [HELP!] 0 Answers