Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by iamevn · Jan 20, 2012 at 03:45 AM · cameraaddforcerelative

Add force relative to camera direction?

How could I set this up so that it adds the force relative to the camera's direction instead of world? Currently have this attached to a ball object and slightly modified orbit script attached to camera.

 function FixedUpdate () {
     var dir : Vector3 = Vector3.zero;
     
     dir.x = Input.GetAxis("Horizontal");
     dir.z = Input.GetAxis("Vertical");
     
     rigidbody.AddForce(dir * force);
 }


I was thinking something like below, but that doesn't seem to work.

 function FixedUpdate () {
     var camdir : Vector3 = camera.forward;
     var dir : Vector3 = Vector3.zero;
 
     dir.x = Input.GetAxis("Horizontal");
     dir.z = Input.GetAxis("Vertical");
     
     var relativedir : Vector3 = Vector3.Cross(camdir, dir);
     rigidbody.AddForce(relativedir * force);
 }

Edit: came up with this, it partially works. The only problem is that it doesn't seem to like diagonal directions. If I rotate the camera 30 degrees to left and push forward, it will go towards 0 degrees. If I rotate camera 60 degrees and push forward, it goes towards 90 degrees.

 function Update() {
     var camDir : Vector3 = Camera.main.transform.forward;
     
     
     var inputDir : Vector3 = Vector3.zero;
     inputDir.x = Input.GetAxis("Left_Horizontal");
     inputDir.z = Input.GetAxis("Left_Vertical");
     
     var dir : Vector3 = Vector3.zero;
     dir.x = camDir.x * inputDir.x;
     dir.z = camDir.z * inputDir.z;
     
     rigidbody.AddForce(dir * force);
 }
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

3 Replies

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

Answer by syclamoth · Jan 20, 2012 at 03:50 AM

Given that you have the transform of the camera, you can always use this-

 var controlDirection : Vector3 = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
 var actualDirection = cameraTransform.TransformDirection(controlDirection);

Then, you have camera-relative directions! You may want to flatten it to the x-z plane if you want your character to walk around flat, but you might not.

Comment
Add comment · Show 4 · 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 iamevn · Jan 20, 2012 at 04:08 AM 0
Share

That doesn't really change much. I probably should have clairified that camera had the Camera.main's transform assigned to it.

avatar image syclamoth · Jan 20, 2012 at 04:23 AM 0
Share

Fixed my answer. Sorry, didn't quite understand the question!

avatar image iamevn · Jan 20, 2012 at 04:27 AM 0
Share

thanks! works great.

avatar image nobluff67 · Aug 31, 2019 at 04:26 PM 0
Share

Works really well for such a frustrating concept. I have been struggling to find such and easy and elegant solution.

avatar image
2

Answer by Imaginaire · Aug 20, 2012 at 11:55 AM

I know this was posted a long time ago, but just for others who might look here.

Change rigidbody.AddForce to rigidbody.AddRelativeForce

That should add force relative to your coordinate system

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 SterlingL · May 27, 2015 at 06:50 PM 0
Share

This works perfectly. I am working on an Accelerator style game and this worked exactly as I wanted it to in order to allow the camera to lead the direction of my movement. THAN$$anonymous$$S!

avatar image
0

Answer by diOriginal · Apr 12, 2013 at 01:39 PM

     if (Mathf.Abs(Input.GetAxisRaw("Vertical")) + Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.1f) {
         Vector3 forward = Camera.mainCamera.transform.TransformDirection(Vector3.forward);
         forward.y = 0;
         forward = forward.normalized;
         Vector3 right = new Vector3(forward.z, 0, -forward.x);
         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");

         Vector3 moveDirection = (h * right + v * forward);
         moveDirection = Quaternion.Inverse(this.transform.rotation) * moveDirection ;
         moveDirection = new Vector3(moveDirection.x, 0, moveDirection.z); 
         moveDirection = this.transform.rotation * moveDirection;
Comment
Add comment · 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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# AddForce Relative To Camera Left And Right Movement 1 Answer

AddForce to Camera 1 Answer

Add force to Child while it's parent rotate around something..... 0 Answers

Add force in the camera direction?? 1 Answer

AddForce Relative to Rotation 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