x


CharacterController unexpectedly moving up

CharacterController unexpectedly moving up when Move() is called, stays at same elevated y position except when jumping, returns to elevated y position when landing

Hi guys,

I have a CharacterController in a gameobject that's based on the Camera Relative Controls prefab from Standard Assets (Mobile). The Update() function has been modified to look like this, with the included helper functions (I wanted to keep my Update() function as clean as possible):

function Jump()
{
    // Apply the current movement to launch velocity
    velocity = character.velocity;
    velocity.y = jumpSpeed;
    canJump = false;
}

function GetButtonInputs()
{
    // Button inputs

    var tapCount = Input.touchCount;

    for ( var i = 0 ; i < tapCount ; i++ ) 
    {

        var touch = Input.GetTouch(i);

        // Now handle button touches

           if(touch.phase == TouchPhase.Began && jumpButton.HitTest(touch.position))
           {
             if ( character.isGrounded )
                 Jump();
            }
        //TODO: Add use buttons
    }
}

function Update()
{
    var movement = cameraTransform.TransformDirection( Vector3( moveJoystick.position.x, 0, moveJoystick.position.y ) );
    // We only want the camera-space horizontal direction
    movement.y = 0;
    movement.Normalize(); // Adjust magnitude after ignoring vertical movement

    // Let's use the largest component of the joystick position for the speed.
    var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
    movement *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );

    // Check for jump using right stick
    if ( character.isGrounded )
    {
       Debug.Log("Player grounded");
       if ( !rotateJoystick.IsFingerDown() )
         canJump = true;
    }
    else
    {       
       // Apply gravity to our velocity to diminish it over time, only do so when not in water.
       velocity.y += Physics.gravity.y * Time.deltaTime;

       // Adjust additional movement while in-air
       movement.x *= inAirMultiplier;
       movement.z *= inAirMultiplier;
    }

    GetButtonInputs();

    // Move player

    movement += velocity;

    movement += Physics.gravity;

    movement *= Time.deltaTime;

    Debug.Log("Movement x" + movement.x + "Movement y" + movement.y + "Movement z" + movement.z);

    // Actually move the character
    character.Move( movement );

    if ( character.isGrounded )
       // Remove any persistent velocity after landing
       velocity = Vector3.zero;

    // Face the character to match with where she is moving
    FaceMovementDirection();   

    // Handle camera rotation

    // Handle camera movement swipes

    for (var touch : iPhoneTouch in iPhoneInput.touches) 
    { 
       if (touch.phase == iPhoneTouchPhase.Moved)
       {
           var FingerDidMove=true;
       }    
    }

    // Scale joystick input with rotation speed

    var camRotation = rotateJoystick.position;
    camRotation.x *= rotationSpeed.x;
    camRotation.y *= rotationSpeed.y;
    camRotation *= Time.deltaTime;

    // Rotate around the character horizontally in world, but use local space
    // for vertical rotation
    cameraPivot.Rotate( 0, camRotation.x, 0, Space.World );
    cameraPivot.Rotate( camRotation.y, 0, 0 );
}

When I run my game, the player GameObject (which has a shadow blob projector and my mesh as its children) starts off at the position set in the editor, including the y position. However, when I move, the player GameObject (which has a CharacterController component) suddenly changes its y position to a level that makes my player character appear to be floating his own height above the ground. The player GameObject that has both the blob projector and the mesh as its children (the one with the Character Controller) attached is the one that moves up, not the mesh. Moreover, there are no other colliders on the Player mesh or any of its children, so consequently, changing my collision matrix to disallow Player/Player collisions (The player character is on its own layer, namely Player) didn't fix anything. This problem appears both in the editor and when I deploy the game to my iPad. The player can still jump, but the player lands at the same elevated height that I'm having problems with. What seems to be causing it to get elevated?

MachCUBED

more ▼

asked Oct 12 '12 at 02:30 AM

MachCUBED gravatar image

MachCUBED
116 22 32 36

Problem solved: Skin width issue. Always reduce your skin width if you have a really small character.

Oct 24 '12 at 05:05 AM MachCUBED
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5049
x3442
x1361
x667
x518

asked: Oct 12 '12 at 02:30 AM

Seen: 487 times

Last Updated: Oct 24 '12 at 05:05 AM