Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 glipzcom · Mar 12, 2012 at 12:41 AM · vector3math

Make something move away from click point.

Goal:

Trying click on a point and have a ball bounce away from that point. I am trying to do this in 2D so I have it ignoring the z component on the rigid body.

My Solution?

I think what I want to do is take the position of the ball (x,y), subtract the position of the mouse click (x,y) and use that to create a new vector to give the rigidbody as a force.

So Far:

I was able to get the position of the click into a Vector3. I am not sure how to get the position of the ball into a Vector3. I don't know how to subtract the two, and if thats even the right idea. I know how to give it a push in a direction using AddRelativeForce.

I was afraid to ask because I feel like I should understand more before attempting to get help.

Comment
Add comment · Show 1
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 Kleptomaniac · Mar 13, 2012 at 10:13 AM 0
Share

How could you do this in a 2D context? If you wanted your ball to move away from the clicked point, that would entail movement in 3D axes! I'm not sure I understand how you want the ball to be repelled from the screen point if you are only using 2D axes ...

unless, of course, you mean 2.5D. In which case, it's entirely possible. :)

I think a bit of clarification is needed ... for me at least.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by stevenp · Mar 13, 2012 at 09:16 AM

To get the ball position:

 private var xPos:float;
 private var yPos:float;
 
 xPos = ballObject.position.x;
 yPos = ballObject.position.y;

Then you get the distance between your mouse click and ball position, see from here.

You can just use the distance to determine how powerful the force will be.

Comment
Add comment · 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
0

Answer by glipzcom · Mar 13, 2012 at 06:09 PM

Thanks guys. I kept hacking at it and this is what I came up with. I am working in 3D but I am doing all resulting motion in 2D.

 ballPoint = this.transform.position;
 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 clickPoint = ray.GetPoint (10) ; 
 clickPoint.z = 0;     
             
 Vector3 newVector = ballPoint - clickPoint;
 newVector.Normalize();
 
 rigidbody.AddForce(newVector*20,ForceMode.Impulse);

The part that was screwing me up, and I am still not entirely sure how it "works" is subtracting one vector from another, and then normalizing. But my ball now flys away from my mouse on click, so its started.

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unable to fully rotate GameObject on tap 2 Answers

How to compute the change in vector when it's referential vector changes? 0 Answers

Instantiating at Collision Point. 2 Answers

A fast triangle triangle intersection algorithm for unity? 4 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