Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Orchestrator · Oct 24, 2011 at 03:59 AM · collisionmovementtouchmouse-drag

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.

alt text

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!!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Orchestrator · Oct 24, 2011 at 07:15 PM 0
Share

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:

  1. 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.

  2. 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!

avatar image syclamoth · Oct 24, 2011 at 10:47 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges