Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (m_bArrived)
        {
            //gameObject.rigidbody.velocity = Vector3.zero;
            m_wantedVelocity = Vector3.zero;
        }
        else if (m_path != null)
        {
            // Compute the seek position (the position we are traveling toward)
            float   currentDistAlongPath = m_path.MapPointToPathDistance(transform.position);
            float   futureDist           = currentDistAlongPath + m_lookAheadDistance;
            Vector3 seekPos = m_path.MapPathDistanceToPoint(futureDist);

            // Set the height of the seek pos, based on the terrain.
            seekPos.y = m_pathTerrain.GetTerrainHeight(seekPos);

            // Determine the new velocity
            Vector3 newVelocity = Vector3.zero;
            Vector3 destPos     = (m_path.Points[m_path.Points.Length - 1]);
            destPos.y = m_pathTerrain.GetTerrainHeight(destPos);
            Vector3 currentFloorPosition = transform.position;
            currentFloorPosition.y = m_pathTerrain.GetTerrainHeight(currentFloorPosition);
            float distToDestPos = Vector3.Distance(currentFloorPosition, destPos);
            if (distToDestPos <= m_arrivalDistance)
            {
                if (!m_bArrived)
                {
                    // No velocity if we are at our destination
                    newVelocity = Vector3.zero;
                    OnArrived();
                }
            }
            else
            {
                newVelocity = ComputeArrivalVelocity(seekPos, destPos, currentFloorPosition, m_wantedVelocity);//rigidbody.velocity);
            }

            //rigidbody.velocity = newVelocity;
            m_wantedVelocity = newVelocity;
        }
    }