Esempio n. 1
0
    private void Update()
    {
        var pos = transform.localPosition;

        pos.x = SmoothFunc(SquareWave, 0.1f, 12)(Time.time);
        transform.localPosition = pos;
    }
    public void Shake(ShakeMode mode, Vector2 dir, SmoothFunc smoothFunc = null, float trauma = 1f)
    {
        int             seed = Random.Range(-100, 100);
        CameraShakeData data = new CameraShakeData
        {
            trauma     = trauma,
            force      = shakeForces[(int)mode],
            speed      = shakeSpeeds[(int)mode],
            seed       = seed,
            dir        = dir,
            smoothFunc = smoothFunc ?? MathUtils.SmoothStart2
        };

        shakeData[shakeDataCount++] = data;
        Debug.Assert(shakeDataCount <= shakeData.Length, "Shake data is full");
    }
Esempio n. 3
0
    /// <summary>
    /// Called once every physics update
    /// </summary>
    private void FixedUpdate()
    {
        var    prestatus = GetPreStatus();
        Forces forces;

        if (prestatus.IsOnBoard)
        {
            Rigidbody.centerOfMass = OnBoardCenterOfMass;
            forces = OnBoardForces;
        }
        else
        {
            Rigidbody.centerOfMass = OffBoardCenterOfMass;
            forces = OffBoardForces;
        }
        var status = GetStatus(prestatus);

        if (Input != null)
        {
            // Get the rigidbody velocity
            var velocity    = Rigidbody.velocity;
            var force_locus = status.CombinedCenterOfMass + (Vector2)transform.TransformVector(ForceOffset);
            if (_Logic.HasHorizontal)
            {
                float force_coef, h_drag_coef;
                Func <Vector3, Vector3> itf_func, tv_func;
                if (status.IsPhysicallySupported)
                {
                    force_coef  = forces.HorizontalForce;
                    h_drag_coef = forces.HorizontalDrag;
                    itf_func    = transform.InverseTransformVector;
                    tv_func     = transform.TransformVector;
                }
                else
                {
                    force_coef  = forces.HorizontalForceInAir;
                    h_drag_coef = forces.HorizontalDragInAir;
                    itf_func    = x => x;
                    tv_func     = x => x;
                }

                var h_force = new Vector3(force_coef * _Logic.Horizontal, 0f);

                var vel     = Rigidbody.GetPointVelocity(force_locus);
                var rel_vel = itf_func(vel);
                var h_drag  = new Vector3(Mathf.Abs(rel_vel.x) * rel_vel.x * h_drag_coef, 0f);

                var force = tv_func(h_force - h_drag);
                Rigidbody.AddForceAtPosition(force, force_locus, ForceMode2D.Force);
            }
            // Jumping is allowed if the protagonist is physically supported
            if (_Logic.Jump && status.IsPhysicallySupported)
            {
                //velocity.y += JumpStrength;
                var     impulse_constant = 1f / Time.fixedDeltaTime;
                Vector3 jump_impulse     = impulse_constant * new Vector3(0f, forces.JumpStrength);
                Rigidbody.AddForceAtPosition(jump_impulse, force_locus, ForceMode2D.Force);
                _Logic.Jump = false;
            }
            // Set the rigidbody velocity
            Rigidbody.velocity = velocity;

            // Cancel griding by moving down
            status.GrindReady &= !_Logic.MoveDown;
        }

        if (EnableMaxLean)
        {
            // Limit the lean angle
            var up            = transform.up;
            var fwd           = transform.forward;
            var angle         = Vector3.SignedAngle(Vector3.up, up, fwd);
            var lim_func      = Curry2x1(Mathf.Clamp, -MaxLean, MaxLean);
            var limited_angle = lim_func(angle);

            var angle_oob = angle != limited_angle;

            // Reset the flag if angle returns in-bounds
            MaxLeanAirFlag &= angle_oob;

            if (!MaxLeanAirFlag)
            {
                if (angle_oob)
                {
                    _FreezeRotationTime = Time.fixedTime + Time.fixedDeltaTime;
                }
                var frozen = Time.fixedTime < _FreezeRotationTime;
                Rigidbody.freezeRotation = frozen;
                var soft_limited_angle = SmoothFunc(lim_func, 10f, 3)(angle);
                //var soft_limited_angle = lim_func(angle);
                transform.rotation = Quaternion.AngleAxis(soft_limited_angle, fwd);
            }
        }
        else
        {
            Rigidbody.freezeRotation = false;
        }

        if (status.GrindReady)
        {
            BoardCollider.gameObject.layer = LayerMask.NameToLayer("Board");
        }
        else
        {
            BoardCollider.gameObject.layer = LayerMask.NameToLayer("No Grind");
        }

        MaxLeanAirFlag &= !status.IsPhysicallySupported;
    }
 public void Shake(ShakeMode mode, SmoothFunc smoothFunc = null, float trauma = 1f) => Shake(mode, Vector2.zero, smoothFunc, trauma);