• 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
Question by amcgoo · Nov 11, 2014 at 08:44 AM · vector3addforce

I need to add a force to an object based on the local vector of the object that is hitting it and not the world coordinates or local coordinates of the object being hit.

I have the following script attached the the "projectile" I thought that the "direction" variable take the vector3 of the owner of the script but it did not. I am a novice and I appreciate any help.`using UnityEngine; using System.Collections;

public class Projectile : MonoBehaviour

{ public float power; private Vector3 direction;

 void OnTriggerEnter(Collider other)
 {
     direction = Vector3.forward;
     other.rigidbody.AddRelativeForce(direction * power);
 }

}`

Thanks!

Comment

People who like this

0 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 robertbu · Nov 11, 2014 at 05:26 PM 1
Share

Not exactly sure what you want. You are probably looking for:

  direction = transform.forward;
  other.rigidbody.AddForce(direction * power);

This will add the force based on the forward direction of this object to the object being hit.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Baste · Nov 11, 2014 at 10:14 AM

Vector3.forward is the global forward (Z-axis, (0,0,1)) Vector. If you want the local forward, you need to use the transform's forward, helpfully named transform.forward.

So your code would be:

 void OnTriggerEnter(Collider other)
 {
     direction = transform.forward;
     other.rigidbody.AddRelativeForce(direction * power);
 }

Comment

People who like this

0 Show 3 · 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 robertbu · Nov 11, 2014 at 05:21 PM 0
Share

@Baste - AddRelativeForce() takes a local coordinate, not a world coordinate.

avatar image Bunny83 · Nov 11, 2014 at 05:24 PM 0
Share

That doesn't make much sense ;) AddRelativeForce expects a Vector in local space. transform.forward is actually a worldspace vector. Using it like a localspace vector of another object will result in some strange direction. The solution is to not use localspace at all. Use the normal AddForce and use a worldspace vector like transform.forward of the object you want.

avatar image amcgoo · Nov 11, 2014 at 05:27 PM 0
Share

Thank you Baste I have used transform.forward as you see below and it works pretty well.

    public float power;

    public Transform direction;
 
     void OnTriggerEnter(Collider other)
     {
         Vector3 localForward= power*direction.forward;
         other.rigidbody.AddRelativeForce(localForward);
         Destroy(gameObject, 1f);
     }
 }

this way the force goes with the projectile. thanks again.

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

My bullets just fall to the ground 1 Answer

Enemies are moving aroung wildly 1 Answer

Adding rotation to Physics.AddForce(); ... Getting weird error? 0 Answers

create a cannon ball based on cannon angles and cannon power 0 Answers

How would I go about finding the rotation corrresponding to the force added to the y and z axis of a object? 1 Answer


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