slow reaction when moving

hey,

yesterday i had a question about SendMessage to another script.
well that works now… but now the problem is how to say that it has to walk forward and backwards.

the problem is almost fixed, but when i push the button it takes a second to walk forward.
and after release it will walk a second further.
another problem is that after i pushed the button once and push it again it will only walk backwards

var speed = 0.0;
var loopspeed = 6.0;
var sprintspeed = 9.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var turnSpeed = 0.0;
var walkForwardButton : int = 0;
var walkBackwardButton : int = 0;
var zVal : int;
var turnLeftButton : int = 0;
var turnRightButton : int = 0;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

function Update ()
{
	//print(moveDirection);
}

function FixedUpdate() {
	if (grounded) {
		// We are grounded, so recalculate movedirection directly from axes
		//zVal = Mathf.Max(walkForwardButton, Input.GetAxis("Vertical"));
		if (walkForwardButton == 1 || Input.GetAxis ("Vertical") == 1) zVal = 1;
		if (walkBackwardButton == -1 || Input.GetAxis ("Vertical") == -1) zVal = -1;
		if (walkForwardButton == 0  walkForwardButton  Input.GetAxis ("Vertical")==0) zVal = 0;
		
		moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, zVal);
		//moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		speed = loopspeed;
		
		if (Input.GetButton ("Jump")) {
			moveDirection.y = jumpSpeed;
			}
		if (Input.GetButton ("Sprint")) {
			speed = sprintspeed;
		}
	}

	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(moveDirection * Time.deltaTime);
	grounded = (flags  CollisionFlags.CollidedBelow) != 0;
	
	   // Check to see if the right mouse button is pressed
   if (Input.GetMouseButton(1))
   {
      // Get the difference in horizontal mouse movement
      x = Input.GetAxis("Mouse X") * turnSpeed;
   }

   // rotate the character based on the x value
   transform.Rotate(0, x, 0);
   //print (transform.rotation.eulerAngles);
}

function walkForward(val:float)
{
	walkForwardButton = val;
}

function walkBackward(val:float)
{
	walkBackwardButton = val;
}

function turnLeft(val:float)
{
	turnLeftButton = val;
}

function turnRight(val:float)
{
	turnRightButton = val;
}

@script RequireComponent(CharacterController)

(there may be some errors in the script because of translation from Dutch to English)

so the questions are:

  • how to fix the lagg.
  • why does it walk backwards after i pushed the button once before.
    (and maybe someone knows how to let my turning work)

Thnx

I remember it well :wink:

The problem is with the way you are using Input.GetAxis. This function “smoothes” the input - when a key is pressed, it gradually increases from zero to one. Since you are comparing the value to one, the object will only start moving after a second or so, after the build-up has finished. The same is true when stopping the object after you release the key - it takes time to decay back down to zero. Also, you will probably avoid some problems if you make zVal a local variable in the FixedUpdate function instead of a script variable. The issue with the script variable is that is stores the value between frame updates and this isn’t the behaviour you want here.

and how do i make this zVal an local var?!

another thing is that is walks backwards after i have pushed the button once.

“Local” just means the variable is declared inside the function body:-

function FixedUpdate() {
    var zVal: int;
    ...
}

As for the walking backwards thing, the walkBackwardButton variable is set in the WalkBackward function, but it is never cleared again anywhere else. Once you have set it to -1, the if statement where you check it will always be true, and so zVal will always get set to -1 (ie, backwards movement). You should either just have a single variable and set it to +1 for forwards, -1 for backwards and 0 for stop, or else clear walkForwardButton and walkBackwardButton to zero after you have tested them.