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
1
Question by finndaly · Feb 24, 2012 at 10:10 PM · directionforcelookatrelative

Adding a force in the direction of an object.

I am adding a force to object A towords object B by wrighting

rigidbody.AddRelativeForce(target.position force);*

Im using relitive forces because i whant it to moove in lical not global space.

But for this to work i must use

transform.LookAt(target);

But i dont whant to have to rorait the object i whant to rotat the direction of the force towards object B, how can i do this or if not what can i do to give the same effect.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by homer_3 · Feb 24, 2012 at 10:31 PM

Couldn't you just get the vector going from your source to your target and use that with AddForce like

 dir = target.transform.position - transform.position;
 dir = dir.normalized;
 rigidbody.AddForce(dir * force);
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 Mutaz · Jun 23, 2015 at 08:14 PM 0
Share

Thank you. It helps.

avatar image
1

Answer by finndaly · Feb 24, 2012 at 10:34 PM

But will it update the direction if it is in the update function without rotaiting the object, and can you explain how it works.

dir = target.transform.position - transform.position; (is this a var in which holds a vector3 of the target?)

dir = dir.normalized; (What dose normalized do to dir.)

rigidbody.AddForce(dir * force); (and so im gesing that this adds the force to dir which is the set vector3 multiplyed by the force that i set.)

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 homer_3 · Feb 25, 2012 at 12:15 AM 0
Share

Subtracting the target's position from your position will get you the vector going from you to the target. Normalize is a well known mathematical operation. If you don't know what it means look it up. But you do it so you won't have to adjust your force value based on the magnitude of dir. AddForce's documentation is on Unity's website.

avatar image finndaly · Feb 25, 2012 at 09:43 PM 0
Share

Thanks lad that works exactly how i whant it ot thanks.

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

How to change gravity's direction on a single gameobject? 0 Answers

How to make a character look at the same direction it's moving? with c# 0 Answers

How to get object to face direction it's flying rather than looking at the target 2 Answers

Instantiating in a specific place with force 2 Answers

how to apply a force in direction of object 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