Beispiel #1
0
    void FixedUpdate()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical   = Input.GetAxis("Vertical");

        if (!aim)
        {
            if (cam != null)
            {
                camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
                move       = vertical * camForward + horizontal * cam.right;
            }
            else
            {
                move = vertical * Vector3.forward + horizontal * Vector3.right;
            }
        }
        else
        {
            move = Vector3.zero;

            Vector3 dir = lookPos - transform.position;
            dir.y = 0;

            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(dir), 20 * Time.deltaTime);

            anim.SetFloat("Forward", vertical);
            anim.SetFloat("Turn", horizontal);
        }

        if (move.magnitude > 1)
        {
            move.Normalize();
        }

        bool walkToggle = Input.GetKey(KeyCode.LeftShift);

        float walkMultiplier = 1;

        if (walkByDefault)
        {
            if (walkToggle)
            {
                walkMultiplier = 1;
            }
            else
            {
                walkMultiplier = 0.5f;
            }
        }
        else
        {
            if (walkToggle)
            {
                walkMultiplier = 0.5f;
            }
            else
            {
                walkMultiplier = 1;
            }
        }

        lookPos = lookInCameraDirection && cam != null ? transform.position + cam.forward * 100 : transform.position + transform.forward * 100;

        move *= walkMultiplier;
        character.Move(move, aim, lookPos);
    }
Beispiel #2
0
    void FixedUpdate()
    {
        //our connection with the variables and our Input
        horizontal = Input.GetAxis("Horizontal");
        vertical   = Input.GetAxis("Vertical");



        //if we are not aiming

        if (!aim)
        {
            lookInCameraDirection = false;
            if (camera.GetComponent <FreeCameraLook>())
            {
                camera.GetComponent <FreeCameraLook> ().OffAim();
            }
            if (cam != null)            //if there is a camera
            {
                //Take the forward vector of the camera (from its transform) and
                // eliminate the y component
                // scale the camera forward with the mask (1, 0, 1) to eliminate y and normalize it
                camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;

                //move input front/backward = forward direction of the camera * user input amount (vertical)
                //move input left/right = right direction of the camera * user input amount (horizontal)
                move = vertical * camForward + horizontal * cam.right;
            }
            else
            {
                //if there is not a camera, use the global forward (+z) and right (+x)
                move = vertical * Vector3.forward + horizontal * Vector3.right;
            }
        }

        else                              //but if we are aiming
        {
            lookInCameraDirection = true; //bug de girar
            if (camera.GetComponent <FreeCameraLook>())
            {
                camera.GetComponent <FreeCameraLook> ().OnAim();
            }
        }

        if (move.magnitude > 1)         //Make sure that the movement is normalized
        {
            move.Normalize();
        }

        bool walkToggle = Input.GetKey(KeyCode.LeftShift) || aim;          //check for walking input or aiming input

        //the walk multiplier determines if the character is running or walking
        //if walkByDefault is set and walkToggle is pressed
        float walkMultiplier = 1;

        if (walkByDefault)
        {
            if (walkToggle)
            {
                walkMultiplier = 1;
            }
            else
            {
                walkMultiplier = 0.5f;
            }
        }
        else
        {
            if (walkToggle)
            {
                walkMultiplier = 0.5f;
            }
            else
            {
                walkMultiplier = 1f;
            }
        }

        //Our look position depends on if we want the character to look towards the camera or not
        //lookPos = lookInCameraDirection && cam != null ? transform.position + cam.forward * 100 : transform.position + transform.forward * 100;

        //apply the multiplier to our move input
        move *= walkMultiplier;

        //pass it to our move function from our character movement script
        character.Move(move, aim, lookPos, squat, jump);
    }