- Home /
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?
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.
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.
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.
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.
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.
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!
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.
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. :(
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.
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.