x


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?

more ▼

asked Apr 20 '10 at 09:07 PM

maparizeau gravatar image

maparizeau
13 2 2 8

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.

Apr 21 '10 at 01:05 AM spinaljack

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.

Apr 21 '10 at 09:55 AM duck ♦♦

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.

Apr 21 '10 at 03:06 PM maparizeau

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.

Apr 21 '10 at 07:34 PM duck ♦♦
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Apr 21 '10 at 09:47 AM

duck gravatar image

duck ♦♦
41k 92 148 415

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

Apr 21 '10 at 03:07 PM maparizeau

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!

Apr 21 '10 at 06:32 PM maparizeau

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.

Apr 21 '10 at 07:08 PM duck ♦♦

I tried this on my project, but I always get the following error: ,,MissingComponentException: 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 Assetscontroller2.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. :(

Jul 01 '10 at 09:24 PM cynamiter
(comments are locked)
10|3000 characters needed characters left

Try using

AddRelativeForce

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

more ▼

answered Apr 20 '10 at 09:17 PM

Fishman92 gravatar image

Fishman92
2.4k 101 113 128

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.

Apr 20 '10 at 09:48 PM maparizeau

Oh... well worked for me :S

Apr 21 '10 at 03:22 PM Fishman92
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5076
x3000
x286
x244

asked: Apr 20 '10 at 09:07 PM

Seen: 3665 times

Last Updated: Apr 21 '10 at 09:50 AM