예제 #1
0
    public virtual void FixedUpdate()
    {
        velocity = UnitVelocity();

        if (_navTarget)
        {
            navTargetAim = _navTarget.position;
            if (useIntercept == true)
            {
                if (interceptForPlatform && interceptForPlatform.target == _navTarget)
                {
                    // intercept for this target already computed by platform
                    navTargetAim = interceptForPlatform.targetAimLocation - (interceptForPlatform.weaponMount.position - transform.position);
                }
                else
                {
                    float _vel = velocity.magnitude;                     // use my speed
                    if (interceptForPlatform)
                    {
                        _vel = interceptForPlatform.shotSpeed ?? 0f;
                    }                     // use platform shot speed
                    // _navTarget velocity
                    Vector3 _navVelocity = Vector3.zero;
                    if (navRigidbody)
                    {
                        _navVelocity = navRigidbody.velocity;
                    }
                    else
                    {
                        _navVelocity    = navLastPosition - _navTarget.position;
                        navLastPosition = _navTarget.position;
                    }

                    navTargetAim = MFcompute.Intercept(transform.position, velocity, _vel, _navTarget.position, _navVelocity) ?? _navTarget.position;
                }
            }
        }
    }
    public virtual Vector3 AimLocation()
    {
        // adjust aim location to hit target
        Vector3 _aimLoc = Vector3.zero;

        if (_target)
        {
            _aimLoc = _target.position;

            if (useIntercept == true && shotSpeed != null)
            {
                Vector3 _targetVelocity = Vector3.zero;
                // target velocity
                if (targetRigidbody)                     // if target has a rigidbody, use velocity
                {
                    _targetVelocity = targetRigidbody.velocity;
                }
                else                     // otherwise compute velocity from change in position
                {
                    _targetVelocity    = (_aimLoc - lastTargetPosition) / Time.deltaTime;
                    lastTargetPosition = _aimLoc;
                }

                // point at linear intercept position
                Vector3?interceptAim = MFcompute.Intercept(exitLoc, velocity, (float)shotSpeed, _aimLoc, _targetVelocity);
                if (interceptAim == null)
                {
                    target = null;
                }
                else
                {
                    _aimLoc = (Vector3)interceptAim;
                }
            }
        }
        return(_aimLoc);
    }
예제 #3
0
    public static float?BallisticIteration(Vector3 exitLoc, float?shotSpeed, float aimRad, MFnum.ArcType arc, Transform target,
                                           Vector3 targetVelocity, Vector3 platformVelocity, Vector3 aimLoc_In, out Vector3 aimLoc_Out)
    {
        float?_ballAim = null;

        aimLoc_Out = aimLoc_In;
        // find new flight time
        float?_flightTime         = MFball.BallisticFlightTime(aimLoc_In, exitLoc, (float)shotSpeed, aimRad, arc);
        float _effectiveShotSpeed = Vector3.Distance(exitLoc, aimLoc_In) / (float)_flightTime;
        // find intercept based on new _effectiveShotSpeed
        Vector3?_intAim = MFcompute.Intercept(exitLoc, platformVelocity, _effectiveShotSpeed, target.position, targetVelocity);

        if (_intAim == null)
        {
            _ballAim = null;
        }
        else
        {
            aimLoc_Out = (Vector3)_intAim;             // modify target aim location
            // re-calculate ballistic trajectory based on intercept point
            _ballAim = MFball.BallisticAimAngle(aimLoc_Out, exitLoc, (float)shotSpeed, arc);
        }
        return(_ballAim);
    }
예제 #4
0
	public override Vector3 AimLocation() {
		// intercept and ballistics
		if ( _target ) {
			Vector3 _targetVelocity = Vector3.zero;
			targetLoc = _target.position;
			if ( useIntercept == true && shotSpeed != null ) { 
				// target velocity
				if (targetRigidbody) { // if target has a rigidbody, use velocity
					_targetVelocity = targetRigidbody.velocity;
				} else { // otherwise compute velocity from change in position
					_targetVelocity = ( targetLoc - lastTargetPosition ) / Time.deltaTime;
					lastTargetPosition = targetLoc;
				}
			}
			
			if ( useGravity == true && Physics.gravity.y != 0 && shotSpeed != null ) { // ballistic aim
				int _factor = -Physics.gravity.y > 0 ? 1 : -1;
				// find initial aim angle
				float? _ballAim = MFball.BallisticAimAngle( targetLoc, exitLoc, (float)shotSpeed, curArc );
				if ( _ballAim == null ) {
					target = null;
				} else {
					if ( useIntercept == true && playerControl == false ) { // ballistic + intercept
						// iterate for better ballistic accuracy when also using intercept
						int bi = 0;
						int biMax;
						if ( curArc == MFnum.ArcType.High ) {
							biMax = highArcBallisticIterations;
						} else {
							biMax = ballisticIterations;
						}
						while ( _target && bi++ < biMax ) {
							//							_ballAim = BallisticIteration ( exitLoc, (float)shotSpeed, (float)_ballAim, _targetVelocity );
							_ballAim = MFball.BallisticIteration ( exitLoc, (float)shotSpeed, (float)_ballAim, curArc, _target, _targetVelocity, velocity, targetLoc, out targetLoc );
							if ( _ballAim == null ) { target = null; } // no solution, release target
						}
					}
					if ( _target ) { // target can become null in balistic iteration if no solution
						Vector3 _cross = -Vector3.Cross( ( targetLoc - exitLoc ), Vector3.up );
						Quaternion _eleAngleDir = Quaternion.AngleAxis( _factor * (float)_ballAim * Mathf.Rad2Deg, -_cross );
						targetAimLocation = exitLoc + (( _eleAngleDir * Vector3.Cross( _cross, Vector3.up ) ).normalized * targetRange );
					}
				}
			} else { // no gravity
				if ( useIntercept == true && playerControl == false && shotSpeed != null ) {
					// point at linear intercept position
					Vector3? _interceptAim = MFcompute.Intercept( exitLoc, velocity, (float)shotSpeed, targetLoc, _targetVelocity );
					if ( _interceptAim == null ) {
						target = null;
					} else {
						targetAimLocation = (Vector3)_interceptAim;
					}
				} else { // point at target position
					targetAimLocation = _target.position;
				}
			}
		} else {
			targetAimLocation = Vector3.zero;
		}
		return targetAimLocation;
	}