Ejemplo n.º 1
0
    void Update()
    {
        // Movement
        float _accelerationFactor = accelerationFactor;
        float _topSpeed           = topSpeed;

        if (Input.GetKey(KeyCode.LeftShift))
        {
            _accelerationFactor = shiftAccelerationFactor;
            _topSpeed           = shiftTopSpeed;
        }

        Vector3 newVelocity = rb.velocity * velocityDecay;

        newVelocity.x += Input.GetAxis("Horizontal") * _accelerationFactor;
        newVelocity.y += Input.GetAxis("Vertical") * _accelerationFactor;
        newVelocity.x  = Mathf.Clamp(newVelocity.x, -_topSpeed, _topSpeed);
        newVelocity.y  = Mathf.Clamp(newVelocity.y, -_topSpeed, _topSpeed);
        rb.velocity    = newVelocity;

        gm.ClampMe(transform);

        // Animation
        animator.SetFloat("x", MyMath.Map(rb.velocity.x, -_topSpeed, _topSpeed, -1f, 1f));
        animator.SetFloat("y", MyMath.Map(rb.velocity.y, -_topSpeed, _topSpeed, -1f, 1f));

        // Shooting
        timeSinceLastShot += Time.deltaTime;
        if (Input.GetKey(KeyCode.Space) && timeSinceLastShot >= fireRate)
        {
            Vector3 bulletSpread = gunTip.transform.forward;
            bulletSpread.x += Random.Range(-spread, spread);
            bulletSpread.y += Random.Range(-spread, spread);
            Instantiate(bullet, gunTip.position, Quaternion.Euler(bulletSpread + gunTip.rotation.eulerAngles));
            timeSinceLastShot = 0.0f;

            shootingOn.TransitionTo(0.1f);
            audioOn = true;
        }
        if (timeSinceLastShot > 0.3f && audioOn)
        {
            audioOn = false;
            shootingOff.TransitionTo(1.0f);
        }
    }