Search Unity

FPSWalker

Discussion in 'Scripting' started by nomadic, Mar 4, 2010.

  1. nomadic

    nomadic

    Joined:
    Mar 4, 2010
    Posts:
    44
    Hi everyone,

    I've only been playing with Unity for a few weeks, so I'm sorry if this is a stupid question.

    I am having trouble with the standard FPSWalker class. When looking up and walking forward, (or backwards looking down) the camera bounces. The script moves the camera in the direction it is pointing with a y component, causing it to leave the ground shortly, or conversely, slow to a crawl when point at the ground. This makes no sense to me, because the following line should give the moveDirection only x and z values:

    Code (csharp):
    1. moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    It seems to me like there should be really simple solution for this, but I don't see it. Thanks in advance for any advice.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Hi, welcome to the forum!

    Are you sure the value of moveDirection isn't being modified elsewhere? Perhaps you could post the whole script?
     
  3. nomadic

    nomadic

    Joined:
    Mar 4, 2010
    Posts:
    44
    Here's the whole script. I don't think I've modified it at all, it's the original that came with Unity:

    Code (csharp):
    1. var speed = 6.0;
    2. var jumpSpeed = 8.0;
    3. var gravity = 10.0;
    4.  
    5. private var moveDirection = Vector3.zero;
    6. private var grounded : boolean = false;
    7.  
    8. function FixedUpdate() {
    9.     if (grounded) {
    10.         // We are grounded, so recalculate movedirection directly from axes
    11.         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    12.         moveDirection = transform.TransformDirection(moveDirection);
    13.         moveDirection *= speed;
    14.        
    15.         if (Input.GetButton ("Jump")) {
    16.             moveDirection.y = jumpSpeed;
    17.         }
    18.     }
    19.  
    20.     // Apply gravity
    21.     moveDirection.y -= gravity * Time.deltaTime;
    22.    
    23.     // Move the controller
    24.     var controller : CharacterController = GetComponent(CharacterController);
    25.     var flags = controller.Move(moveDirection * Time.deltaTime);
    26.     grounded = (flags  CollisionFlags.CollidedBelow) != 0;
    27. }
    28.  
    29. @script RequireComponent(CharacterController)
    Thanks for the response!
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Take a look at how the first person controller prefab in Standard Assets is set up.

    --Eric