Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by maparizeau · Apr 20, 2010 at 09:07 PM · cameraaddforcedirection

How can I apply a force to an object (eg, a rolling ball) in the direction that the camera is facing?

Hi to all,

My player comes up to a point where he enters his vehicle in a tunnel, kind of like a subway system.

If I roll my ball on the first straightaway, the controls are going to react properly. Meaning up:moves forward, down:moves backwards, right:moves right and left:moves left. But as soon as I turn as I hit the first turn and no matter if I have some straightaways further ahead, the ball no longer react to the controls I'm sending him resulting in up:moves left, down:moves right, left:moves forward and right:moves backwards.

I've went through unityAnswer/unityForum but I can't find anything on this. I've tried AddTorque, AddForce

Is there a way of making sure that no matter what trajectory my "subway system" has, that the ball will react correctly to the up, down, left and right key?

Would it be possible to use the coordinates of the camera instead or something like that?

Comment
Add comment · Show 4
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 spinaljack · Apr 21, 2010 at 01:05 AM 0
Share

Using AddFoce should do it as it's on the world axis and shouldn't be affected by the vehicle. Would need to see more details really.

avatar image duck ♦♦ · Apr 21, 2010 at 09:55 AM 0
Share

P.S. there's rarely a need to mark your question "Community Wiki", so don't do it unless you need to. It means you don't get rep for upvotes to your question. It's mostly useful for answers which take the form of large and regularly growing collections of related information.

avatar image maparizeau · Apr 21, 2010 at 03:06 PM 0
Share

Thanks for the heads up on the Communuity Wiki. Being a noob here, I assumed that it would post there as well. I will be more careful next time. thanks.

avatar image duck ♦♦ · Apr 21, 2010 at 07:34 PM 0
Share

np. I think there should be a pop-up warning when you check it, because there's no way to un-do it afterwards, and 9/10 times I see it used, it's by mistake. It can also mean that you might get fewer users interested in answering your question because they don't get rep for their answers either.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer Wiki

Answer by duck · Apr 21, 2010 at 09:47 AM

To add a force to the ball in the direction that the camera is pointing, use this:

rigidbody.AddForce( camera.transform.forward * speed );

However, often your camera isn't pointing exactly horizontal in relation to your game, so you may want to remove the tilt angle of your camera before using its direction. Eg, if your camera is looking "across but angled down a bit", you don't want your force to be applied in the down direction at all. You can fix that by zeroing the Y value of the camera's forward transform before using it:

Javascript:

var forceDirection = camera.transform.forward;
forceDirection.y = 0;
rigidbody.AddForce( forceDirection.normalized * speed );

C#:

Vector3 forceDirection = camera.transform.forward;
forceDirection = new Vector3( forceDirection.x, 0, forceDirection.y );
rigidbody.AddForce( forceDirection.normalized * speed );

In order for this to provide you with good gameplay controls, you should make it so that your camera continues to follow the object and rotates according to the object's velocity direction.

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 maparizeau · Apr 21, 2010 at 03:07 PM 0
Share

I will try it out. and get back to you on this.

avatar image maparizeau · Apr 21, 2010 at 06:32 PM 0
Share

It worked great. But I only had to use the following...

for up/down rigidbody.AddForce((useObject.transform.forward speed) inputX);

and left/right rigidbody.AddForce((useObject.transform.right speed) inputY);

thanks!

avatar image duck ♦♦ · Apr 21, 2010 at 07:08 PM 0
Share

Yes it will work like that if your camera stays relatively horizontal in relation to the surface - my other examples we in case your camera can end up looking down on the ball, when it might cause problems.

avatar image cynamiter.du · Jul 01, 2010 at 09:24 PM 0
Share

I tried this on my project, but I always get the following error: ,,$$anonymous$$issingComponentException: There is no 'Camera' attached to the "Player" game object, but a script is trying to access it. You probably need to add a Camera to the game object "Player". Or your script needs to check if the component is attached before using it.

controller2.Update () (at Assets\controller2.js:146)" Even if the camera is attached to the "Player"-object, I'm getting this error...

I know this is a quite old post, but the script works for me. Exept the "zeroing the Y value"-part. :(

avatar image
0
Wiki

Answer by HolBol · Apr 20, 2010 at 09:17 PM

Try using

AddRelativeForce

Then forward will always be forward in relation to the ball's position, same for left right etc.

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 maparizeau · Apr 20, 2010 at 09:48 PM 0
Share

Hi Fishman92,

The problem with using AddRelativeForce is that as soon as the ball starts turning and you keep pressing the up button to move forward, the balls starts to go backwards and then forward. So you end up staying in place. Or is there something that I can use that will leave the coordinates in their original position even when the ball is rolling? I will post my scrip if you need to look at it.

avatar image HolBol · Apr 21, 2010 at 03:22 PM 0
Share

Oh... well worked for me :S

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

No one has followed this question yet.

Related Questions

How to change the first person controller speed? 4 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

add force in camera direction 1 Answer

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

How to get an object's direction relative to the camera? 3 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