예제 #1
0
        public override void NetShoot(IAmmoEffect effect, float timeDelay, Vector3 startPosition, Vector3 rayDirection)
        {
            // THIS IS A MODDED SHOOT COMMAND TO SUPPORT NETWORKTIME ROLLBACK
            // Just return if there is no effect
            if (effect == null)
            {
                return;
            }

            // Get root game object to prevent impacts with body
            Transform ignoreRoot = GetRootTransform();

            // could possibly lag comp the gun pos as well, but for not take the location of player shot.
            base.StartRayRollback(timeDelay);
            // Check for raycast hit
            Ray     ray = new Ray(startPosition, rayDirection);
            Vector3 hitPoint;
            bool    didHit = PhysicsExtensions.RaycastNonAllocSingle(ray, out m_Hit, m_MaxDistance, m_Layers, ignoreRoot, QueryTriggerInteraction.Ignore);

            if (didHit)
            {
                hitPoint = m_Hit.point;
            }
            else
            {
                hitPoint = startPosition + (rayDirection * m_MaxDistance);
            }

            if (didHit)
            {
                effect.Hit(m_Hit, ray.direction, m_Hit.distance, float.PositiveInfinity, firearm as IDamageSource);
            }

            base.StopRayRollback(timeDelay);
            // Draw the tracer line out to max distance
            if (m_TracerPrototype != null)
            {
                StartCoroutine(ShowTracer(hitPoint));
            }

            base.NetShoot(effect, timeDelay, startPosition, rayDirection);
        }
        void FixedUpdate()
        {
            if (m_Release)
            {
                if (m_RecycleDelay <= 0f)
                {
                    ReleaseProjectile();
                }
                else
                {
                    if (m_MeshRenderer != null && m_MeshRenderer.enabled)
                    {
                        m_MeshRenderer.enabled = false;
                    }
                    m_Timeout -= Time.deltaTime;
                    if (m_Timeout < 0f)
                    {
                        ReleaseProjectile();
                    }
                }
            }
            else
            {
                float time = Time.deltaTime;

                // Set position to target
                localTransform.position = m_LerpToPosition;

                // Reset interpolation for Update() frames before next fixed
                m_LerpTime         = Time.fixedTime;
                m_LerpFromPosition = m_LerpToPosition;

                Vector3 moveVelocity = (m_Velocity * time);
                Vector3 catchupValue = Vector3.zero;

                if (m_CatchupDistance.magnitude > 0f)
                {
                    Vector3 steps = (m_CatchupDistance * time);

                    m_CatchupDistance -= steps;

                    if (m_CatchupDistance.magnitude < (m_Velocity * 0.1f).magnitude)
                    {
                        catchupValue     += m_CatchupDistance;
                        m_CatchupDistance = Vector3.zero;
                    }
                }


                Vector3 desiredPosition = m_LerpFromPosition + (moveVelocity + catchupValue);
                float   distance        = Vector3.Distance(m_LerpFromPosition, desiredPosition);
                localTransform.LookAt(desiredPosition);

                // Enable renderer if travelled far enough (check based on from position due to lerp)
                if (!m_PassedMinimum && m_Distance > m_MinDistance)
                {
                    m_PassedMinimum = true;

                    if (m_ForgetIgnoreRoot)
                    {
                        m_IgnoreRoot = null;
                    }

                    if (m_MeshRenderer != null && m_MeshRenderer.enabled == false)
                    {
                        m_MeshRenderer.enabled = true;
                    }
                }

                Ray ray = new Ray(localTransform.position, localTransform.forward);
                if (PhysicsExtensions.RaycastNonAllocSingle(ray, out m_Hit, distance, m_Layers, m_IgnoreRoot, QueryTriggerInteraction.Ignore))
                {
                    // Set lerp target
                    m_LerpToPosition = m_Hit.point;

                    // Release back to pool
                    m_Release = true;

                    // Update distance travelled
                    m_Distance += m_Hit.distance;

                    m_AmmoEffect.Hit(m_Hit, localTransform.forward, m_Distance, m_Velocity.magnitude, m_DamageSource);

                    OnHit();
                }
                else
                {
                    // Set lerp target
                    m_LerpToPosition = desiredPosition;

                    // Update distance travelled
                    m_Distance += distance;

                    // Should the bullet just give up and retire?
                    if (m_Distance > k_MaxDistance)
                    {
                        ReleaseProjectile();
                    }
                }

                // Apply forces to the projectile
                m_Velocity = ApplyForces(m_Velocity);
            }
        }
예제 #3
0
        public override void Shoot(float accuracy, IAmmoEffect effect)
        {
            // Just return if there is no effect
            if (effect == null)
            {
                return;
            }

            // Get root game object to prevent impacts with body
            Transform ignoreRoot = GetRootTransform();
            //if (firearm.wielder != null)
            //    ignoreRoot = firearm.wielder.gameObject.transform;

            // Get the forward vector
            Vector3 muzzlePosition = m_MuzzleTip.position;
            Vector3 startPosition  = muzzlePosition;
            Vector3 forwardVector  = m_MuzzleTip.forward;

            bool useCamera = false;

            if (firearm.wielder != null)
            {
                switch (m_UseCameraAim)
                {
                case UseCameraAim.HipAndAimDownSights:
                    useCamera = true;
                    break;

                case UseCameraAim.AimDownSightsOnly:
                    if (firearm.aimer != null)
                    {
                        useCamera = firearm.aimer.isAiming;
                    }
                    break;

                case UseCameraAim.HipFireOnly:
                    if (firearm.aimer != null)
                    {
                        useCamera = !firearm.aimer.isAiming;
                    }
                    else
                    {
                        useCamera = true;
                    }
                    break;
                }
            }
            if (useCamera)
            {
                Transform aimTransform = firearm.wielder.fpCamera.aimTransform;
                startPosition = aimTransform.position;
                forwardVector = aimTransform.forward;
            }

            // Get the direction (with accuracy offset)
            Vector3 rayDirection = forwardVector;
            float   spread       = Mathf.Lerp(m_MinimumSpread, m_MaximumSpread, 1f - accuracy);

            if (spread > Mathf.Epsilon)
            {
                Quaternion randomRot = UnityEngine.Random.rotationUniform;
                rayDirection = Quaternion.Slerp(Quaternion.identity, randomRot, spread / 360f) * forwardVector;
            }

            // Check for raycast hit
            Ray     ray = new Ray(startPosition, rayDirection);
            Vector3 hitPoint;
            bool    didHit = PhysicsExtensions.RaycastNonAllocSingle(ray, out m_Hit, m_MaxDistance, m_Layers, ignoreRoot, QueryTriggerInteraction.Ignore);

            if (didHit)
            {
                hitPoint = m_Hit.point;
            }
            else
            {
                hitPoint = startPosition + (rayDirection * m_MaxDistance);
            }

            // Double check hit from gun muzzle to prevent near scenery weirdness
            if (useCamera)
            {
                Vector3 newRayDirection = hitPoint - muzzlePosition;
                newRayDirection.Normalize();
                ray = new Ray(muzzlePosition, newRayDirection);
                if (PhysicsExtensions.RaycastNonAllocSingle(ray, out m_Hit, m_MaxDistance, m_Layers, ignoreRoot, QueryTriggerInteraction.Ignore))
                {
                    hitPoint = m_Hit.point;
                    effect.Hit(m_Hit, newRayDirection, m_Hit.distance, float.PositiveInfinity, firearm as IDamageSource);
                }
            }
            else
            {
                if (didHit)
                {
                    effect.Hit(m_Hit, ray.direction, m_Hit.distance, float.PositiveInfinity, firearm as IDamageSource);
                }
            }

            // Draw the tracer line out to max distance
            if (m_TracerPrototype != null)
            {
                StartCoroutine(ShowTracer(hitPoint));
            }

            SendNetShootEvent(startPosition, rayDirection);
            base.Shoot(accuracy, effect);
        }
예제 #4
0
        public override void Shoot(float accuracy, IAmmoEffect effect)
        {
            if (m_ProjectilePrefab != null)
            {
                IProjectile projectile = PoolManager.GetPooledObject <IProjectile>(m_ProjectilePrefab, false);
                InitialiseProjectile(projectile);

                Transform ignoreRoot = GetRootTransform();
                //if (firearm.wielder != null)
                //    ignoreRoot = firearm.wielder.gameObject.transform;

                // Get the forward vector
                Vector3 muzzlePosition = m_MuzzleTip.position;
                Vector3 startPosition  = muzzlePosition;
                Vector3 forwardVector  = m_MuzzleTip.forward;

                bool useCamera = false;
                if (firearm.wielder != null)
                {
                    switch (m_UseCameraAim)
                    {
                    case UseCameraAim.HipAndAimDownSights:
                        useCamera = true;
                        break;

                    case UseCameraAim.AimDownSightsOnly:
                        if (firearm.aimer != null)
                        {
                            useCamera = firearm.aimer.isAiming;
                        }
                        break;

                    case UseCameraAim.HipFireOnly:
                        if (firearm.aimer != null)
                        {
                            useCamera = !firearm.aimer.isAiming;
                        }
                        else
                        {
                            useCamera = true;
                        }
                        break;
                    }
                }

                if (useCamera)
                {
                    Transform aimTransform = firearm.wielder.fpCamera.aimTransform;
                    startPosition = aimTransform.position;
                    forwardVector = aimTransform.forward;
                }

                // Get the direction (with accuracy offset)
                Vector3 rayDirection = forwardVector;
                float   spread       = Mathf.Lerp(m_MinimumSpread, m_MaximumSpread, 1f - accuracy);
                if (spread > Mathf.Epsilon)
                {
                    Quaternion randomRot = UnityEngine.Random.rotationUniform;
                    rayDirection = Quaternion.Slerp(Quaternion.identity, randomRot, spread / 360f) * forwardVector;
                }

                Ray     ray = new Ray(startPosition, rayDirection);
                Vector3 hitPoint;
                if (PhysicsExtensions.RaycastNonAllocSingle(ray, out m_Hit, k_MaxDistance, m_Layers, ignoreRoot, QueryTriggerInteraction.Ignore))
                {
                    hitPoint = m_Hit.point;
                }
                else
                {
                    hitPoint = startPosition + (rayDirection * k_MaxDistance);
                }

                if (useCamera)
                {
                    Vector3 newDirection = hitPoint - muzzlePosition;
                    newDirection.Normalize();
                    projectile.Fire(muzzlePosition, newDirection * m_MuzzleSpeed, m_Gravity, effect, firearm.wielder.gameObject.transform, m_Layers, firearm as IDamageSource);
                    SendNetShootEvent(muzzlePosition, newDirection);
                    projectile.gameObject.SetActive(true);
                }
                else
                {
                    projectile.Fire(startPosition, rayDirection * m_MuzzleSpeed, m_Gravity, effect, ignoreRoot, m_Layers, firearm as IDamageSource);
                    SendNetShootEvent(startPosition, rayDirection);
                    projectile.gameObject.SetActive(true);
                }
            }
            base.Shoot(accuracy, effect);
        }