Exemple #1
0
    // ---------------------------------------------------------
    // Name :   Update
    // Desc :   Called each frame by Unity
    // ---------------------------------------------------------
    void Update()
    {
        int turnOnSpot;

        // Copy NavMeshAgents state into inspector visible variables
        HasPath     = _navAgent.hasPath;
        PathPending = _navAgent.pathPending;
        PathStale   = _navAgent.isPathStale;
        PathStatus  = _navAgent.pathStatus;

        // Perform corss product on forard vector and desired velocity vector. If both inputs are Unit length
        // the resulting vector's magnitude will be Sin(theta) where theta is the angle between the vectors.
        Vector3 cross = Vector3.Cross(transform.forward, _navAgent.desiredVelocity.normalized);

        // If y component is negative it is a negative rotation else a positive rotation
        float horizontal = (cross.y < 0) ? -cross.magnitude : cross.magnitude;

        // Scale into the 2.32 range for our animator
        horizontal = Mathf.Clamp(horizontal * 2.32f, -2.32f, 2.32f);

        // If we have slowed down and the angle between forward vector and desired vector is greater than 10 degrees
        if (_navAgent.desiredVelocity.magnitude < 1.0f && Vector3.Angle(transform.forward, _navAgent.steeringTarget - transform.position) > 10.0f)
        {
            // Stop the nav agent (approx) and assign either -1 or +1 to turnOnSpot based on sign on horizontal
            _navAgent.speed = 0.1f;
            turnOnSpot      = (int)Mathf.Sign(horizontal);
        }
        else
        {
            // Otherwise it is a small angle so set Agent's speed to normal and reset turnOnSpot
            _navAgent.speed = _originalMaxSpeed;
            turnOnSpot      = 0;
        }

        // Send the data calculated above into the animator parameters
        _animator.SetFloat("Horizontal", horizontal, 0.1f, Time.deltaTime);
        _animator.SetFloat("Vertical", _navAgent.desiredVelocity.magnitude, 0.1f, Time.deltaTime);
        _animator.SetInteger("TurnOnSpot", turnOnSpot);

        // If agent is on an offmesh link then perform a jump

        /*if (_navAgent.isOnOffMeshLink)
         * {
         *  StartCoroutine( Jump( 1.0f) );
         *  return;
         * }*/

        // If we don't have a path and one isn't pending then set the next
        // waypoint as the target, otherwise if path is stale regenerate path
        if ((_navAgent.remainingDistance <= _navAgent.stoppingDistance && !PathPending) || PathStatus == UnityEngine.AI.NavMeshPathStatus.PathInvalid /*|| PathStatus==NavMeshPathStatus.PathPartial*/)
        {
            SetNextDestination(true);
        }
        else
        if (_navAgent.isPathStale)
        {
            SetNextDestination(false);
        }
    }
    public void ReplaceNavigationDebug(float newDistanceLeft, UnityEngine.AI.NavMeshPathStatus newStatus)
    {
        var index     = GameComponentsLookup.NavigationDebug;
        var component = CreateComponent <NavigationDebugComponent>(index);

        component.distanceLeft = newDistanceLeft;
        component.status       = newStatus;
        ReplaceComponent(index, component);
    }
    // ---------------------------------------------------------
    // Name	:	Update
    // Desc	:	Called each frame by Unity
    // ---------------------------------------------------------
    void Update()
    {
        // Copy NavMeshAgents state into inspector visible variables
        HasPath     = _navAgent.hasPath;
        PathPending = _navAgent.pathPending;
        PathStale   = _navAgent.isPathStale;
        PathStatus  = _navAgent.pathStatus;

        // Transform agents desired velocity into local space
        Vector3 localDesiredVelocity = transform.InverseTransformVector(_navAgent.desiredVelocity);

        // Get angle in degrees we need to turn to reach the desired velocity direction
        float angle = Mathf.Atan2(localDesiredVelocity.x, localDesiredVelocity.z) * Mathf.Rad2Deg;

        // Smoothly interpolate towards the new angle
        _smoothAngle = Mathf.MoveTowardsAngle(_smoothAngle, angle, 80.0f * Time.deltaTime);

        // Speed is simply the amount of desired velocity projected onto our own forward vector
        float speed = localDesiredVelocity.z;

        // Set animator parameters
        _animator.SetFloat("Angle", _smoothAngle);
        _animator.SetFloat("Speed", speed, 0.1f, Time.deltaTime);

        if (_navAgent.desiredVelocity.sqrMagnitude > Mathf.Epsilon)
        {
            if (!MixedMode ||
                (MixedMode && Mathf.Abs(angle) < 80.0f && _animator.GetCurrentAnimatorStateInfo(0).IsName("Base Layer.Locomotion")))
            {
                Quaternion lookRotation = Quaternion.LookRotation(_navAgent.desiredVelocity, Vector3.up);
                transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, 5.0f * Time.deltaTime);
            }
        }

        // If agent is on an offmesh link then perform a jump

        /*if (_navAgent.isOnOffMeshLink)
         * {
         *      StartCoroutine( Jump( 1.0f) );
         *      return;
         * }*/

        // If we don't have a path and one isn't pending then set the next
        // waypoint as the target, otherwise if path is stale regenerate path
        if ((_navAgent.remainingDistance <= _navAgent.stoppingDistance && !PathPending) || PathStatus == UnityEngine.AI.NavMeshPathStatus.PathInvalid /*|| PathStatus==NavMeshPathStatus.PathPartial*/)
        {
            SetNextDestination(true);
        }
        else
        if (_navAgent.isPathStale)
        {
            SetNextDestination(false);
        }
    }
    static int get_pathStatus(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            UnityEngine.AI.NavMeshAgent      obj = (UnityEngine.AI.NavMeshAgent)o;
            UnityEngine.AI.NavMeshPathStatus ret = obj.pathStatus;
            ToLua.Push(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o, "attempt to index pathStatus on a nil value"));
        }
    }
    // ---------------------------------------------------------
    // Name	:	Update
    // Desc	:	Called each frame by Unity
    // ---------------------------------------------------------
    void Update()
    {
        // Copy NavMeshAgents state into inspector visible variables
        HasPath     = _navAgent.hasPath;
        PathPending = _navAgent.pathPending;
        PathStale   = _navAgent.isPathStale;
        PathStatus  = _navAgent.pathStatus;

        // If we don't have a path and one isn't pending then set the next
        // waypoint as the target, otherwise if path is stale regenerate path
        if ((!HasPath && !PathPending) || PathStatus == UnityEngine.AI.NavMeshPathStatus.PathInvalid /*|| PathStatus==NavMeshPathStatus.PathPartial*/)
        {
            SetNextDestination(true);
        }
        else
        if (_navAgent.isPathStale)
        {
            SetNextDestination(false);
        }
    }
 public override void Reset()
 {
     gameObject = this.self;
     pathStatus = UnityEngine.AI.NavMeshPathStatus.PathComplete;
 }