Hello All...I am trying to create a simple version of a tank like controller. I originally just put in controls using the default inputs for the "vertical" and "horizontal",(arrow keys or WASD). I am pretty new to scripting in general so ... please be gentle. :)
// define variable for the move speed and the rotation speed
var speed = 3.0;
var rotateSpeed = 3.0;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
// if A or D is pressed independently then Rotate that direction around the y axis
transform.Rotate(0, Input.GetAxis ("Horizontal") *rotateSpeed, (forward));
// If A and D are pressed simultaneously Move forward
NOT SURE HOW TO DO THIS PART .....
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("BOTH HORIZONTAL INPUTS");
controller.SimpleMove(forward * curSpeed);
}
Thanks for any help you can offer...
Grateful Noob...
`var leftDown = Input.GetButton("Left");
var rightDown = Input.GetButton("Right");
if (leftDown && !rightDown)
// Rotate left
else if (rightDown && !leftDown)
// Rotate right
else if (leftDown && rightDown) {
// Move forward
`
Set up "Left" and "Right" in the input manager. GetAxis isn't useful in this particular case since you need to check the keys individually. You generally don't want to use GetKey unless you intend on writing your own input manager. GetKeyDown (or GetButtonDown) won't work for this, because that only returns true for the one frame when you press the button down.
reason for else if instead of just another if, is because then you could (i think) be moving twice each frame if you held down all 4 keys. this will make it so that if you press A and D then you will go forward OR if you press left arrow and right arrow you will go forward.
another way it can be done (but this is not for sure, its just a maybe) is:
var curSpeed = speed * Input.GetAxis ("Horizontal") * -(Input.GetAxis("Horizontal"));
use whichever way you want, first way will work for sure, but requires a little extra length, second way im not sure will work, but is a single line.
Put a debug statement above your move forward code, see if the block is even being called. If the debug prints something, then the keys are working fine, but your move forward method is failing.