0

Right now I have a first person character with a movement script attached. I use a rigid body and a character controller. The camera controls the transformation of the parent object with the the player object, in this case a capsule.

The player object has the movement script on it, and the camera has a camera controller on it, that just allows the mouse to control the transformation of the player object.

When I jump, my script does so the player keeps the momentum, and can not stop mid air, but if I turn my camera, the player object also changes trajectory depending on the transformation. If I look right, while in mid air, my jump also follows the camera to the right. I want the jump motion in mid air, to keep the original trajectory. Can someone help me to add that function, but with the camera still able to look around?

My movement script:

void movement () {
        moveVector = Vector3.zero;
        moveVector.x = Input.GetAxisRaw("Horizontal");
        moveVector.z = Input.GetAxisRaw("Vertical");

        if (controller.isGrounded) {
            verticalVelocity = -1;

            if (Input.GetButtonDown("Jump")) {
                verticalVelocity = jumpforce;
            }

        } else {
            verticalVelocity -= gravity * Time.deltaTime;
            moveVector = lastMove;
        }

        moveVector.y = 0;
        moveVector.Normalize ();
        moveVector *= playerspeed;
        moveVector.y = verticalVelocity;

        worldMove = transform.TransformDirection (moveVector);
        controller.Move (worldMove * Time.deltaTime);

        //controller.Move (moveVector.z * transform.forward * Time.deltaTime);
        //controller.Move (moveVector.x * transform.right * Time.deltaTime);
        //controller.Move (moveVector.y * transform.up * Time.deltaTime);

        //controller.Move (moveVector * Time.deltaTime);
        lastMove = moveVector;
    }

Its the:

moveVector = lastMove;

that does so my player does not stop mid air.

Also it would be amazing if it was possible to do so you cannot change direction mid air, but you can drag the trajectory a bit, but in a smooth way, like in Counter Stike: Global offensive and so.

Update: Here is my camera code:

public class CameraController : MonoBehaviour {

    Vector2 mouseLook;
    Vector2 smoothV;
    public float sensitivity = 5.0f;
    public float smoothing = 2.0f;

    GameObject character;

    // Use this for initialization
    void Start () {
        character = this.transform.parent.gameObject;
    }

    // Update is called once per frame
    void Update () {
        var md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));

        md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
        smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
        smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);
        mouseLook += smoothV;

        //Låser kameraret så man ikke kan kigge længere ned eller op end 90 grader
        mouseLook.y = Mathf.Clamp (mouseLook.y, -90f, 90f);

        transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);
        character.transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up);
    }
}

2 Answers 2

1

Here is my part of code from spectate camera controller that used for mine project, hope it will help you.

void Update()
{
    CameraMovementValidation();
    CameraRotationValidation();
    Camerazoom();

    if (Input.GetKey(KeyCode.LeftShift))
    {
        doSlowMotion = true;
    }
    else if (Input.GetKeyUp(KeyCode.LeftShift))
    {
        doSlowMotion = false;
    }
}

public void FixedUpdate()
{
    CameraMovement();
    CameraRotation();
}

public void LateUpdate()
{
    Controller();
}

private void CameraMovementValidation()
{
    if (Input.GetKey(KeyCode.W))
    {
        doCameraUpDownMove = true;
        cameraUpDownMoveDirection = 1;
    }
    else if (Input.GetKey(KeyCode.S))
    {
        doCameraUpDownMove = true;
        cameraUpDownMoveDirection = -1;
    }
    else
    {
        doCameraUpDownMove = false;
    }

    if (Input.GetKey(KeyCode.A))
    {
        doCameraLeftRightMove = true;
        cameraLeftRightDirection = -1;
    }
    else if (Input.GetKey(KeyCode.D))
    {
        doCameraLeftRightMove = true;
        cameraLeftRightDirection = 1;
    }
    else
    {
        doCameraLeftRightMove = false;
    }

    if (Input.GetKey(KeyCode.Q))
    {
        doCameraForwardBackMove = true;
        cameraForwardBackDirection = 1;
    }
    else if (Input.GetKey(KeyCode.E))
    {
        doCameraForwardBackMove = true;
        cameraForwardBackDirection = -1;
    }
    else
    {
        doCameraForwardBackMove = false;
    }
}

private void CameraMovement()
{
    if (doCameraUpDownMove)
    {
        cameraPosition += transform.rotation * Vector3.up * moveCoefficent * cameraUpDownMoveDirection * camereSlowMotionMoveCoefficient;
    }

    if (doCameraLeftRightMove)
    {
        cameraPosition += transform.rotation * Vector3.right * moveCoefficent * cameraLeftRightDirection * camereSlowMotionMoveCoefficient;
    }

    if (doCameraForwardBackMove)
    {
        cameraPosition += transform.rotation * Vector3.forward * moveCoefficent * cameraForwardBackDirection * camereSlowMotionMoveCoefficient;
    }
}

private void CameraRotationValidation()
{
    if (Input.GetMouseButtonDown(1))
    {
        mouseXUpdateForRotation = Input.mousePosition.x;
        mouseYUpdateForRotation = Input.mousePosition.y;
    }

    if (Input.GetMouseButton(1))
    {
        doCameraRotation = true;
    }
    else
    {
        doCameraRotation = false;
    }
}

private void CameraRotation()
{
    if (doCameraRotation)
    {
        if (Input.mousePosition.x != mouseXUpdateForRotation)
        {
            cameraRotationX += (Input.mousePosition.x - mouseXUpdateForRotation) * rotationFactor;
            mouseXUpdateForRotation = Input.mousePosition.x;
        }

        if (Input.mousePosition.y != mouseYUpdateForRotation)
        {
            cameraRotationY += (Input.mousePosition.y - mouseYUpdateForRotation) * rotationFactor;
            mouseYUpdateForRotation = Input.mousePosition.y;
        }
    }
}

private void Camerazoom()
{
    mouseScrollCoefficient = Input.GetAxis("Mouse ScrollWheel");
    if (mouseScrollCoefficient > 0)
    {
        zoom += mouseScrollCoefficient * -cameraZoomCoefficent;
    }
    else if (mouseScrollCoefficient < 0)
    {
        zoom += mouseScrollCoefficient * -cameraZoomCoefficent;
    }
}

private void Controller()
{
    transform.position = Vector3.Lerp(transform.position, cameraPosition, moveSmoothlyCoefficient);
    transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(-cameraRotationY, cameraRotationX, 0), rotationSmoothlyCoefficient);
    camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoom, zoomSmoothlyCoefficient);

    if (doFolowToPlayer && transform.position.x <= cameraPosition.x + 0.5 && transform.position.x > cameraPosition.x - 0.5 &&
        transform.position.y <= cameraPosition.y + 0.5 && transform.position.y > cameraPosition.y - 0.5 &&
        transform.position.z <= cameraPosition.z + 0.5 && transform.position.z > cameraPosition.z - 0.5)
    {
        cameraRotationY = -mainCamera.transform.localEulerAngles.x;
        cameraRotationX = mainCamera.transform.localEulerAngles.y;
        doRotationFolowToPlayer = true;
        doFolowToPlayer = false;
    }

    if (doRotationFolowToPlayer && transform.localEulerAngles.x <= -cameraRotationY + 0.5 && transform.localEulerAngles.x > -cameraRotationY - 0.5 &&
        transform.localEulerAngles.y <= cameraRotationX + 0.5 && transform.localEulerAngles.y > cameraRotationX - 0.5)
    {
        doRotationFolowToPlayer = false;
    }
}   
Sign up to request clarification or add additional context in comments.

Comments

0

You seem to be confusing movement with the direction of camera. In First Person its common for the 'player' to move position in the same direction as the camera, but it doens't have to be that way. The camera orientation is goverened by the mouse, but you are using the camera orientation to set the direction of travel.

You need to store the current Direction Vector seperately and alter the Direction Vector based on some algorithm (for instance hyperbolic curve) and ignore the Camera Orientation during the players Jump. You can use the Camera Direction to change the View Matrix, but just dont change the camera location using hte camera orientation when in flight.

1 Comment

Could you maybe show me how that would be done to my code? i updated so my Camera code is in the post as well. But i still when grounded want W to move forward considering where the camera is looking. I am not very skilled in coding yet (started a few days ago)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.