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 Sazails · Jun 05, 2019 at 04:18 PM · rigidbodyinputdirectionoutput

Add force in direction to camera

Hello, I need to get the camera forward direction without the camera tilt direction and convert the balls forward direction to it?

Current setup:

 // Get horizontal and vertical inputs and assign to vector3 "move"
 Vector3 move = new Vector3(BaseInput.getHorizontal, 0f, BaseInput.getVertical);
 
 // Assign camera transform forward to the ball forward
 move = camera.TransformDirection(move);
 
 // Finally add force in the direction
 rigidbody.AddForce(move * _speed);

alt text

alt text

I really need help on this. Thank you!

1.png (36.7 kB)
2.png (30.2 kB)
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
0

Answer by SirPaddow · Jun 05, 2019 at 05:17 PM

The tilt of your camera is the y value of your forward vector, so something like this should do the trick:

 Vector3 move = new Vector3(camera.transform.forward.x * BaseInput.getHorizontal, 0, camera.transform.forward.z * BaseInput.getVertical);
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 SirPaddow · Jun 05, 2019 at 05:25 PM 0
Share

Sorry, that won't work. Try this ins$$anonymous$$d:

 Vector3 move = new Vector3(camera.forward.x, 0, camera.forward.z) * BaseInput.getVertical + new Vector3(camera.right.x, 0, camera.right.z) * BaseInput.getHorizontal;
avatar image Sazails SirPaddow · Jun 05, 2019 at 05:57 PM 0
Share

Unfortunately the same result. I could probably wrap my head around it after sleep but no luck today as I am just pure confused on this topic.

Thanks for the help, to give additional information adding force to the right or the left works fine and the angle doesn't matter.

So really the only thing I need is to get the forward direction as a Vector3 with a value of 1, so for example " new Vector3(1,0,0) " and use that to multiply the move vector.

I'll come back later after rest, figure it out and share it.

Thanks anyways.

avatar image Sazails · Jun 05, 2019 at 05:30 PM 0
Share

I still get the same results, when looking forward it adds more velocity than looking at a downwards angle of around 75 like in the picture sample, so it seems like the velocity is still depending on camera tilt.

Also I didn't mention this but my camera rotates forward on the X axis.

avatar image SirPaddow Sazails · Jun 05, 2019 at 09:27 PM 0
Share

Ow, ok, if think I finaly understood! You want to get a move.magnitude == 1, right? That's pretty easy, you only need to call ".normalized" on it:

 Vector3 forwardNoTilt = new Vector3(camera.transform.forward.x, 0, camera.transform.forward.z);
 Vector3 normalizedForwardNoTilt = forwardNoTilt.normalized;

So, all together:

 Vector3 forward$$anonymous$$ovement = new Vector3(camera.forward.x, 0, camera.forward.z).normalized * BaseInput.getVertical;
     
 Vector3 lateral$$anonymous$$ovement = new Vector3(camera.right.x, 0, camera.right.z).normalized * BaseInput.getHorizontal;
     
 Vector3 move = forward$$anonymous$$ovement + lateral$$anonymous$$ovement;
 
 // If you normalize zero, behaviour can be weird
 if (move != Vector3.zero)
 {
     rigidbody.AddForce(move.normalized * _speed);
 }

avatar image
0

Answer by YBHOLTZ · Jun 05, 2019 at 07:23 PM

If you want the camera direction forward relative a object, you must:

  Vector3 dir = Target.transform.forward;
  Vector3 fixed_dir = camera.transform.rotation * dir;

will be the corrected forward direction for the forward camera based on the object.

Hope this helps.

Comment
Add comment · Show 5 · 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 Sazails · Jun 05, 2019 at 07:39 PM 0
Share

How would I implement this with my move (input of horizontal and vertical movement) with the direction multiplied by the speed (float) in the rigidbody.AddForce(); ? Thanks.

avatar image YBHOLTZ Sazails · Jun 05, 2019 at 07:56 PM 0
Share

In this case, you would have to do the move in two steps:

First we solve the forward then the right

Then you would simply multiply BaseInput.getVertical by found forward and move as you are already doing.

Then multiply BaseInput.getHorizontal by found right and do the move again.

Something like this (in the same frame):

 rigidbody.AddForce(fixed_forward * _speed);
 
 rigidbody.AddForce(fixed_right * _speed);

But ideally you would move the camera by following another object. I mean, ins$$anonymous$$d of moving it yourself. The camera would only follow an object and you would do more "normal" moving the object and the camera would only follow

avatar image Sazails YBHOLTZ · Jun 05, 2019 at 08:47 PM 0
Share

I'll try the first option because the second option is not possible as the player has to look around.

I am trying to achieve movement like in this video: https://www.youtube.com/watch?v=aviL3HX3UEc

Thanks.

Show more comments

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

159 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 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

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Collision Not Working 1 Answer

Throwing knife goes straight 1 Answer

Not shooting in desired direction 1 Answer

transforming player position on input.get button down 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