• 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 DaReign · May 10, 2019 at 11:02 AM · rigidbodyballrigidbody.addforce

Add force for long pass ball

Hi How to add correct forces for ball pass. For simple pass I do it that way

 rb.AddForce((target.position - transform.position) * thrust, ForceMode.Impulse);

so basicaly I calculate direction and apply some thurst (kick power). It behavior a little strange because it looks that summary force depends on how far the target.position is o_0. And second question when I want to make long pass I do it that

 rb.AddForce(transform.up * 5, ForceMode.Impulse); // this should add vertical force to move ball up
 rb.AddForce((target.position - transform.position) * thrust, ForceMode.Impulse); 

but it don't work correctly because ball sometimes go to diferent direction (because I think of this rb.AddForce(transform.up * 5, ForceMode.Impulse);)

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
1
Best Answer

Answer by tormentoarmagedoom · May 10, 2019 at 11:09 AM

Hallo.

First Qust: AddForce is a vector. Doing this;

  target.position - transform.position

You calculate a vector with the correct direction, but the magnitude of the vector depends on the distance between the 2 points. To have a better control of the force, you should convert that vector into a known magnitude vector with the same direction. Best way to do this is to Normalize the vector (so the magnitude will be always 1)


Second Qst:

Meybe the best solution if you want a parabole trajectory, is to add the vertical movement as a simple translate function (or changing directly its transform.position.y) To have a correct control of the height.

I think a good solution can be, once you know the origin and final points, you can calculate the middle point of that 2 points. Then while the object is moving increase its Y component a little each frame, until reach the midpoint (midpoint in X and Z components, as Y will change). Once reached this midpoint, decrease Y component again.

Good luck!

Bye!

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 DaReign · May 10, 2019 at 11:32 AM 0
Share

Hi thanks for answer

  1. For short pass I tried normalize vector and maybe if you also mention about that then it is right approach. I will try figure it out ;).

  2. For long pass I did stupid mistake I used transform.up insted Vector3.up the correct code shold look like this rb.AddForce((target.position - transform.position) * thrust, Force$$anonymous$$ode.Impulse); rb.AddForce(Vector3.up *5, Force$$anonymous$$ode.Impulse);

And it is working ;)

avatar image tormentoarmagedoom DaReign · May 10, 2019 at 11:39 AM 0
Share

Nice!

Using normalize, makes you know the "force quantity" independent of the distance. So its usefull to move things. You can also move it wothout forces to mantain a constant speed, using transform.translate or changing transform.position.

Good luck! Bye!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

145 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to have a fixed movement using rigidbodies without animations 0 Answers

Sudden surge of force applied to a rigidbody from continous jumping. 1 Answer

How could I make a bouncy object move mostly along the Z axis? 1 Answer

Flick ball in 3D 0 Answers

Rigidbody2D adding force vs modifying velocity for character jump 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges