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 durrab · Jul 07, 2013 at 09:27 AM · physicsaddforcemousepositionmousedown

AddForce at mouse position only on table area

alt text

I have a very specific question - First of all I am able to rotate stick around the ball according to the mouse position so this is working perfectly fine.

Now I need to add the force at the direction of cursor position within the surface of table only. so If I just click on the green area for example pointing to yellow ball and press mouse down or any where then it should move the ball there - Right now its only adding the force at the direction of x,y but z is always coming as 0.

here is the code as Force Script.

 void OnMouseDown()
     {
        Debug.Log("OnMouseDown in the table");
         Vector3 mouse_pos = Input.mousePosition;
         
         //transform is my Table Area.
 Vector3 object_pos=Camera.main.WorldToScreenPoint(this.transform.position);
         mouse_pos.x = mouse_pos.x - object_pos.x;
         mouse_pos.y = mouse_pos.y - object_pos.y;
         //This is the problem where z of mouse_pos always 0.0 in Debug.Log
         //Que_Ball.rigidbody.AddRelativeForce(mouse_pos,ForceMode.Force);
          Que_Ball.rigidbody.AddForce(mouse_pos);
      
     }
mysituation.png (472.2 kB)
Comment
Add comment · Show 5
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 robertbu · Jul 07, 2013 at 03:16 PM 0
Share

So what is happening now? Typically I would do these kinds of calculations by converting the mouse/screen coordinates into world coordinates rather than convert world coordinates into screen coordinates. But I what you have here should work, or should work if you get the amount of force correct. That is:

 Que_Ball.rigidbody.AddForce(mouse_pos.normalized * amount);

So you have a 2D surface. Why do you want any force applied in the 'Z' direction? A few seconds of video so see what is going on would be helpful.

avatar image durrab · Jul 07, 2013 at 04:21 PM 0
Share

I have a Top Camera - so only x and z coordinates will work for the direction on the table - So I am basically moving my mouse in x and z coordinates from top Camera.

I am trying your solution and see if that works then let you know.

avatar image robertbu · Jul 07, 2013 at 05:22 PM 0
Share

Try this:

 Vector3 v3 = new Vector3(mouse_pos.x, 0.0, mouse_pos.y);
 Que_Ball.rigidbody.AddForce(v3.normalized * amount);
avatar image durrab · Jul 08, 2013 at 08:37 AM 0
Share

Its still not working - still moving in x direction where I need it to move in the angular direction - I have tried with Force$$anonymous$$ode.Impluse as well but still not working as desired.

avatar image robertbu · Jul 08, 2013 at 08:42 AM 0
Share

Try doing a Debug.Log(mouse_pos). You want to verify that the x and y position make sense with respect to the indicator. With this last change, you should be getting both X and Z components to the AddForce().

0 Replies

· Add your reply
  • Sort: 

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

15 People are following this question.

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

Related Questions

make addforce ignore player controller? 1 Answer

Does AddForce use Time.deltaTime or TIme.fixedDeltaTime or it depends on whether it is being used in Update() or fixedUpdate()? 2 Answers

Position synchronization problem in RPC Network System 0 Answers

Why use AddForce rather than modify velocity? 4 Answers

Apply A Force to Reach a Specific Height 3 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