Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Cloky · May 24, 2020 at 09:16 AM · unity 2daddforcedirectionmath

addForce to Character in direction of other object

I have a character (the little, green, slimy thing on the attached picture) and an arrow that rotates between up and right. At the tip of the arrow, there's an Object called "arrow_tip" which I want to use to tell my Character in which direction the character should fly.

I want to do this with addforce as it's triggered once by a mouse click.

How do I tell the character to add force in the direction of the "arrow_tip" object?

I have tried the following:

  if (Input.GetMouseButtonDown(0)
 {
    direction = new Vector2(arrow_tip.transform.position.x * strength, arrow_tip.transform.position.y * 
    strength);
    rb.AddForce(direction);
 }

alt text where - arrow_tip is the arrow_tip object (via a Serialized Field) - strength is a float value between 0 and 10

Help is appreciated, if you need more information just tell me :)

anmerkung-2020-05-24-110710.png (24.2 kB)
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 KoenigX3 · May 24, 2020 at 09:24 AM 1
Share

If you subtract the arrow base coordinates from the tip coordinates, you get a vector that starts from the base of the arrow and looks towards the tip.

 direction = new Vector2(arrow_tip.transform.position.x - arrow_base.transform.position.x, arrow_tip.transform.position.y - arrow_base.transform.position.y) * strength;

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by blinkafrootable · May 25, 2020 at 04:42 AM

If you subtract your "destination" vector (the arrow tip) by your "start" vector (the base of the arrow), you'll get a vector that points in the direction of the destination from the start. However, you want to normalize that vector (make it so that its magnitude/size is no larger than 1 unit) because, otherwise, the distance from the "start" to the "destination" vectors will impact the amount of force added. This can be expressed using the following code:

 direction = (arrow_tip.transform.position - transform.position).normalized;
 rb.AddForce(direction * strength);
Comment
Add comment · Show 1 · 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 Cloky · May 25, 2020 at 06:10 AM 0
Share

This worked, thanks! The only thing I had to change was to add rb.AddForce(direction * strength * 1000) as strength itself is a very low value I guess.

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

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

Related Questions

Unity AddForce Problem (not a bug problem, but a math problem) 2 Answers

add force in camera direction 1 Answer

Calculate a new position instead of using Vector3.back 1 Answer

Need Help with MousePos and dashing 0 Answers

How can I apply a force to an object (eg, a rolling ball) in the direction that the camera is facing? 2 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