Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Refactoring for 3-point character control

Discussion in 'Input System' started by dimex472, Sep 7, 2020.

  1. dimex472

    dimex472

    Joined:
    Jun 18, 2020
    Posts:
    7
    There is such a method of controlling a character that is spinning in the update in an endless runner:
    private void PlayerMovement()
    {
    moveVector = Vector3.zero;
    moveVector.z = variableJoystick.Horizontal * moveHorizontalSpeed; // was moveVectorZ
    moveVector.y = gravityForce;
    moveVector.x -= moveVectorX;

    characterController.Move(moveVector * Time.deltaTime);
    characterController.center = new Vector3(0, 0.85f, -0.3f);
    }


    On the X coordinate, the default forward acceleration is set, on the Y coordinate, custom gravity, and Z - horizontal control with the joystick. The problem is that you need to rewrite this method in such a way as to leave control of the joystick, but so that when swiping, the player is in the desired coordinate. Problem: When using the method described below, the character does not stop at the desired coordinate, but continues to move and how to rewrite this code so that there is default control in the endless runner - swipes.


    private void CoordinatesMovement()
    {
    float leftPos = -2.3f;
    float rightPos = 5.3f;
    float centerPos = 2.0f;

    if (variableJoystick.Horizontal < 0)
    {
    moveVectorZ = Mathf.Lerp(transform.position.z, leftPos, Time.deltaTime);
    }
    else if (variableJoystick.Horizontal > 0)
    {
    moveVectorZ = Mathf.Lerp(transform.position.z, rightPos, Time.deltaTime);
    }
    else if(variableJoystick.Horizontal == 0)
    {
    moveVectorZ = 0;
    }
    else
    {
    moveVectorZ = Mathf.Lerp(transform.position.z, centerPos, Time.deltaTime);
    }

    }