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 :)
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);