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 /
This question was closed Aug 25, 2017 at 06:39 PM by FortisVenaliter for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by GBT · Aug 18, 2013 at 04:46 PM · c#cameraaddforcedirection

add force in camera direction

I'm using this script to make a crate to be pushed strongly in the direction of my FPS camera:

 void Update () {
         if(Input.GetButtonDown("fus")){
             crate.rigidbody.AddForce(cameraRelativeRight * power);
             crate.rigidbody.AddForce(cameraRelativeForward * power);
         }
     
     }

when the variables are:

 cameraRelativeRight = Camera.main.transform.TransformDirection(Vector3.right);
 cameraRelativeForward = Camera.main.transform.TransformDirection(Vector3.forward);

Already tried:

combine the forces in the same term (like this: (cameraRelativeRight+cameraRelativeForward) * power

make it with this variables:

     this.cam = Camera.main.transform;
     this.cameraRelativeX = cam.TransformDirection(Vector3.right);
     this.cameraRelativeZ = cam.TransformDirection(Vector3.forward);

by adding my main camera as "cam"

nothing worked, the crate just pushed wherever it wants...

Comment
Add comment · Show 2
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 Slobdell · Aug 18, 2013 at 04:49 PM 0
Share

Why are you pushing right if you want it to go forward?

avatar image GBT · Aug 18, 2013 at 05:07 PM 0
Share

I want it to go away from the camera by the x and z terms, but it's pushing the same direction all the time

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Aug 18, 2013 at 04:52 PM

If you want the crate pushed towards the camera, you need to construct a vector from the crate towards the camera:

 Vector3 pushDir = Camera.main.transform.postion - crate.transform.position;
 crate.rigidbody.AddForce(pushDir.normalized * power);
Comment
Add comment · Show 8 · 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 GBT · Aug 18, 2013 at 04:57 PM 0
Share

Actually I want the exact opposite, but I will try something with this logic

doesn't working, I want it to be pushed away only by the X and Z axises

avatar image robertbu · Aug 18, 2013 at 05:07 PM 1
Share

You said towards the camera. For something opposite reverse the calculation:

 Vector3 pushDir =  crate.transform.position - Camera.main.transform.postion;
 crate.rigidbody.AddForce(pushDir.normalized * power);
avatar image GBT · Aug 18, 2013 at 05:11 PM 0
Share

Yeah I tried it, the crate just disappear now

avatar image JoaquinRD · Aug 18, 2013 at 05:18 PM 0
Share

Your power variable might be too high. Did you try following the crate in scene view while you play?

avatar image GBT · Aug 18, 2013 at 06:17 PM 1
Share

so in the end I did the most simple thing, just: crate.rigidbody.AddForce(Camera.main.transform.forward * power);

and it's working like a magic

avatar image yuliwei GBT · Aug 25, 2017 at 06:33 PM 0
Share

Nice man! I use crate.rigidbody.AddForce(Camera.main.transform.forward * power);

and it's working like a magic too. But now it is a "forward throw" the object, I want to add a little bit "throw upward" as well. Do you know how to do it? Like throw a basketball, not a base ball.

Show more comments

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Raycasting with Rotation of Camera 0 Answers

Relative AddForce 0 Answers

How to change Add.Force direction depending on the camera angle in the XZ plane 0 Answers

Multiple Cars not working 1 Answer

Release camera 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