コード例 #1
0
    void Update()
    {
        if (controller == null)
        {
            controller = GetComponentInChildren <CharacterController>();
        }
        doNextFrame?.Invoke();
        doNextFrame = null;
        if (animator == null)
        {
            animator = GetComponentInChildren <Animator>();
        }

        //transform.rotation = Quaternion.Euler(0, yaw, 0);
        //head.localRotation = Quaternion.Euler(pitch, 0, 0);

        if (velocity.y > 0 && (controller.collisionFlags & CollisionFlags.Above) != 0)
        {
            velocity.y = 0;
        }


        CheckForSnapToGround();
        UpdateDroneStuff();

        Vector3 moveInput = moveInputFunc?.Invoke() ?? Vector3.zero;
        Vector3 aimInput  = aimInputFunc?.Invoke() ?? Vector3.zero;
        TKeys   keyInput  = keysInputFunc?.Invoke() ?? TKeys.None;

        if (moveInput.sqrMagnitude > 1)
        {
            moveInput = moveInput.normalized;
        }


        Vector3 newMoveDir = moveInput;

        if (moveRoot != null && moveInput.sqrMagnitude > 0.0f)
        {
            Quaternion q  = moveRoot.rotation;
            Vector3    qe = q.eulerAngles;
            q = Quaternion.Euler(0, qe.y, 0);

            Vector3 forward = moveRoot.forward; forward.y = 0; forward.Normalize();
            Vector3 right   = moveRoot.right; right.y = 0; right.Normalize();

            newMoveDir = forward * moveInput.z + right * moveInput.x;

            //forward = transform.forward;		forward.y = 0;	forward.Normalize();
            //right = transform.right;			right.y = 0;	right.Normalize();

            //forward = Vector3.Project(forward, moveDir);
            //right = Vector3.Project(right, moveDir);

            //moveDir = forward + right * sideSpeedRatio;
        }

        float moveRate = 1.0f;

        if (moveInput.sqrMagnitude > tapThreshold)
        {
            lastMoveDir  = moveInput;
            lastMoveTime = 0;
        }
        else
        {
            lastMoveTime += Time.deltaTime;
        }
        if (keyInput.Has(TKeys.Sprint))
        {
            moveRate *= sprintPower;
        }
        moveDir = Vector3.Lerp(moveDir, newMoveDir, Time.deltaTime * (moveRate * responseBase / mass));

        if (Mathf.Abs(newMoveDir.x) > 0 || Mathf.Abs(newMoveDir.z) > 0)
        {
            Vector3 target = new Vector3(newMoveDir.x, 0, newMoveDir.z);
            //float target = Mathf.Sign(moveInput.x);
            Vector3 fwd = transform.forward;
            fwd = Vector3.Slerp(fwd, target, Time.deltaTime * lookDampening);

            Debug.DrawLine(transform.position, transform.position + fwd * 4);
        }

        RagCam rcam = moveRoot.GetComponent <RagCam>();

        if (rcam)
        {
            rcam.camHardPush = rcam.mousePushCamera ? Vector3.zero : aimInput;
        }
        if (aimInput.sqrMagnitude > 0)
        {
            Vector3 targetPos = transform.position + aimInput;
            targetPos.y = transform.position.y;
            Quaternion prevRotation = transform.rotation;
            transform.LookAt(targetPos, Vector3.up);
            transform.rotation = Quaternion.Lerp(prevRotation, transform.rotation, Time.deltaTime * animResponse);
        }

        Vector3 targetMoveXYZ = new Vector3(Vector3.Dot(moveDir, transform.right), 0, Vector3.Dot(moveDir, transform.forward));

        animMoveXYZ = Vector3.Lerp(animMoveXYZ, targetMoveXYZ, Time.deltaTime * animResponse);

        aniData.Record("MoveX", animMoveXYZ.x);
        aniData.Record("MoveZ", animMoveXYZ.z);
        aniData.Record("MoveAnimSpeed", 2.2f - .4f * moveDir.magnitude);

        bool fire = keyInput.Has(TKeys.Fire);

        aniData.Record("Fire", fire);
        if (muzzleflash != null)
        {
            if (guides.Length > 0)
            {
                muzzleflash.transform.position = guides[0].position;
                muzzleflash.transform.LookAt(guides[0].position + guides[0].forward, Vector3.up);
            }
            if (fire)
            {
                muzzleflash.gameObject.BroadcastMessage("Play", SendMessageOptions.DontRequireReceiver);
            }
        }

        aniData.Record("Crouch", keyInput.Has(TKeys.Crouch));
        aniData.Record("Reload", false);
        aniData.Record("Throw", keyInput.Has(TKeys.Throw));
        if (keyInput.Pressed(TKeys.Use, lastKeys))
        {
            if (false)
            {
                //Use(trackedUsable);
            }
            else
            {
                aniData.Record("Reload", keyInput.Has(TKeys.Use));
            }
        }

        if (keyInput.Pressed(TKeys.SwitchWeapons, lastKeys))
        {
            ChangeWeapon(currentWeaponKind == 0 ? 1 : 0);
        }
        else
        {
            aniData.Record("SwapWeapon", false);
        }


        Vector3 movement = moveDir * speed;

        movement += velocity;
        controller.CheckMoveBack(movement * Time.deltaTime);
        lastInput = moveInput;
        lastKeys  = keyInput;

        if (animator != null && animator.enabled)
        {
            aniData.SetTo(animator);
        }
    }