- Home /
Adding force in relation to the camera issues
Hi everyone. I'm having a bit of trouble with my script for adding force to the player controlled object in a project. I've got the force applying to the object (it's a ball with a rigidbody) when pressing the W key and it works fine for whichever direction the camera is facing, but I can't seem to figure out how to get it to apply force for the A, S and D keys in the proper directions.
Here is the script:
function Update () {
if (Input.GetKey ("w")) rigidbody.AddTorque(Camera.main.transform.right * 3000);
if (Input.GetKey ("d"))
rigidbody.AddTorque(Camera.main.transform.right * 2000);
if (Input.GetKey ("s"))
rigidbody.AddTorque(Camera.main.transform.up * 2000);
if (Input.GetKey ("a"))
rigidbody.AddTorque(Camera.main.transform.right * 2000);
}
If anyone could shed any light on the situation for me, that'd be great :)
Answer by AngryOldMan · Mar 25, 2011 at 11:42 AM
1st of all map your input keys so they are all named with relative conventions. for example use names such as W,a,s,d, instead of w,right,down,left. This would be your 1st port of call for checking what's wrong, go to edit ~> project settings ~> input to check the correct naming conventions and keys of your valid inputs.
Second of all using forces as high as you are (2000 +) means that your level and objects may be scaled too big, i suggest keeping your player unscaled then scaling everything elese around your player, I promise you, you will only benifit from this (but don't forget to reduce all of your forces to suit)
Thirdly are you having an errors, if so what do they say and what line are they on? if no errors then put debug logs in all over your script so you can tell exactly how the script is executing itself.
Finally the only problem I could tell of is the direction that your adding torque, I'm assuming you want to rotate the object/camera right? otherwise use rigidbody.AddForce to move it around.
if (Input.GetKey ("w")) rigidbody.AddTorque(Camera.main.transform.forward * 3000);//or transform.up
if (Input.GetKey ("right")) rigidbody.AddTorque(Camera.main.transform.right * 2000);
if (Input.GetKey ("down")) rigidbody.AddTorque(Camera.main.transform.forward * -2000);//or transform.up
if (Input.GetKey ("left")) rigidbody.AddTorque(Camera.main.transform.right * -2000);
Thanks for the response. No, there are no errors when I run it, but the weird thing is, if i add force to forward on the W key, the ball will roll to the left and when I tell it to add force to the right e.g Camera.main.transformright) the ball will roll forwards whichever way the camera is facing.
A work around for it could be that I can put in add force to the right to go forwards and adding force forward to make the ball roll left, but then I don't know how to make it roll backwards and right.
Thanks for your time :)
that is strange, check your transform.localPosition for the object and make sure it's not parented to anything, it sounds like your object is parented to your camera if so unparent it and ins$$anonymous$$d parent your camera to your game object! the answer above explains how to move them in the other directions, just add a negative number ins$$anonymous$$d of a positive, viola, so if +1 is move forward then -1 is move back, same as if +1 is move right -1 is move left. Post back with the problems if this doesn't help :)
I couldn't figure out why right moved forward and forward move left etc (the ball or camera wern't parented to anything either and when I tried to parent the camera to the game object it sent the ball flying off the level for unknown reasons :S), but on a good note I now have the ball moving EXACTLY how I wanted it to by just using the positive and negitive numbers like you suggested.
Thankyou very much for taking the time to help me with the problem, you've helped me out tremendously.
Thanks :)
ah... I think the problem is the massively high numbers for force your appling, try parenting the game object to the camera and reducing your force number, or better still make it a variable so you can control it in the inspector rather than messing about with scripts everytime. been a pleasure to be of use