예제 #1
0
 void Throw(ThrowParameters tParams)
 {
     target        = tParams.target;
     throwingAngle = tParams.throwAngle;
     m_Rigidbody   = GetComponent <Rigidbody>();
     gravity       = Mathf.Abs(Physics.gravity.y);
     StartCoroutine(nameof(ThrowGrenade));
 }
예제 #2
0
    void FixedUpdate()
    {
        // Cache the inputs
        float horizontal = Input.GetAxis("Horizontal");
        float vertical   = Input.GetAxis("Vertical");

        // Set player animation state
        bool  hasHorizontalInput = !Mathf.Approximately(horizontal, 0f);
        bool  hasVerticalInput   = !Mathf.Approximately(vertical, 0f);
        float speed;

        if ((hasHorizontalInput || hasVerticalInput) && (Input.GetKey("left shift") || Input.GetKey("right shift")))
        {
            speed = 1f; // Running
        }
        else if (hasHorizontalInput || hasVerticalInput)
        {
            speed = 0.5f; // Walking
        }
        else
        {
            speed = 0f; // Standing
        }

        bool isAiming    = Input.GetMouseButton(1);
        bool isAttacking = Input.GetMouseButton(0);
        bool isCrouching = (Input.GetKey("left ctrl") || Input.GetKey("right ctrl"));

        m_Animator.SetFloat(Speed, speed);
        m_Animator.SetBool(Aim, isAiming);
        m_Animator.SetBool(Attack, isAttacking);
        m_Animator.SetBool(Crouch, isCrouching);

        // Jumping
        if (Input.GetKey("space") && isGrounded)
        {
            // TODO: Multiply jump speed by gravity for easier jump height control
            m_Rigidbody.velocity += jumpSpeed * Vector3.up;

            isGrounded = false;
        }

        // Weapons
        // TODO: Turn player when aiming and shooting
        if (Input.GetKey("g") && canThrow)
        { // TODO: Move grenade throwing logic to a function
            canThrow         = false;
            timer            = 0f;
            m_ThrowDirection = cam.transform.forward;
            m_ThrowDirection.Normalize();
            Vector3 throwStart = gameObject.transform.position + Vector3.up;

            GameObject grenade = Instantiate(grenadeObject, throwStart, Quaternion.identity);
            // Player ignores collisions with grenades
            Physics.IgnoreCollision(m_Collider, grenade.GetComponent <Collider>());
            // grenade.GetComponent<Rigidbody>().velocity = transform.TransformDirection(m_ThrowDirection * throwVelocity);

            var camTransform = cam.transform;
            m_Collider.enabled = false; // Disable player collider during grenade targeting
            Ray ray = new Ray(camTransform.position, camTransform.forward);
            Physics.Raycast(ray, out var raycastHit);
            m_Collider.enabled = true; // Reenable player collider
            var m = new ThrowParameters(raycastHit.point, throwAngle);
            grenade.BroadcastMessage("Throw", m);

            Debug.DrawRay(throwStart, m_ThrowDirection, Color.red, 5f);
            Debug.DrawLine(throwStart, raycastHit.point, Color.magenta, 5f);
        }

        MovementManagement(horizontal, vertical);
    }