示例#1
0
    // Use this for initialization
    void Start()
    {
        move = GetComponent <Move>();
        seek = GetComponent <SteeringSeek>();

        path.CalcPositionByClosestPoint(transform.position, out current_percentage);
        distance_ratio      = move.max_mov_velocity / path.GetDistance();
        current_percentage /= path.GetDistance();
    }
示例#2
0
    // Use this for initialization
    void Start()
    {
        move = GetComponent <Move>();
        seek = GetComponent <SteeringSeek>();

        float distance;

        closest_point = path.CalcPositionByClosestPoint(transform.position, out distance);
        current_ratio = distance / path.Curve.Points.Length;
    }
示例#3
0
    // Use this for initialization
    void Start()
    {
        move = GetComponent <Move>();
        seek = GetComponent <SteeringSeek>();

        // TODO 1: Calculate the closest point in the range [0,1] from this gameobject to the path
        path.CalcPositionByClosestPoint(transform.position, out current_percentage);
        distance_ratio      = move.max_mov_velocity / path.GetDistance();
        current_percentage /= path.GetDistance();
    }
    // Use this for initialization
    void Start()
    {
        move = GetComponent <Move>();
        seek = GetComponent <SteeringSeek>();

        // TODO 1: Calculate the closest point from the tank to the curve

        //float min_dist = float.MaxValue;
        //int closestPoint = 0;
        //for (int i = 0; i < curve.Curve.PointsCount; ++i)
        //{
        //    float newDist = Vector3.Distance(transform.position, curve.Curve[i].PositionWorld);
        //    if (newDist < min_dist)
        //    {
        //        min_dist = newDist;
        //        closestPoint = i;
        //    }
        //}
        //trgPoint = closestPoint;

        totalPathDist = path.GetDistance();//If you don't put anything you get the total distance of the path

        float distance = 0f;

        transform.position = path.CalcPositionByClosestPoint(transform.position, out distance);
        trgPointIdx        = path.CalcSectionIndexByDistance(distance);
        currRation         = trgPointRation = distance / totalPathDist;
    }
示例#5
0
    // Start is called before the first frame update
    void Start()
    {
        float distance;

        closest_point = path.CalcPositionByClosestPoint(transform.position, out distance);

        current_ratio = distance / path.GetDistance();
    }
示例#6
0
    // Use this for initialization
    void Start()
    {
        NavMeshAgent agent = GetComponent <NavMeshAgent>();

        path.CalcPositionByClosestPoint(transform.position, out current_percentage);
        distance_ratio      = agent.speed / path.GetDistance();
        current_percentage /= path.GetDistance();
    }
示例#7
0
        private void Process(int i, bool suppressWarning = false)
        {
            var   point = objects[i].transform.position;
            float distanceUsingMath;
            var   posUsingMath = math.CalcPositionByClosestPoint(point, out distanceUsingMath);

            Debug.DrawLine(point, posUsingMath, Color.yellow);

            if (!CheckResults)
            {
                return;
            }

            float distanceUsingCheckMethod;
            var   posUsingCheckMethod = CalcPositionByClosestPoint(math, point, out distanceUsingCheckMethod);

            Debug.DrawLine(point, posUsingCheckMethod, Color.blue);

            var distanceCheck = Math.Abs(distanceUsingMath - distanceUsingCheckMethod) > .01f;
            var pointCheck    = Vector3.Magnitude(posUsingMath - posUsingCheckMethod) > 0.001f;

            if ((distanceCheck || pointCheck) && Mathf.Abs((point - posUsingMath).magnitude - (point - posUsingCheckMethod).magnitude) > BGCurve.Epsilon)
            {
                ErrorPointIndex = i;
                if (!suppressWarning)
                {
                    Debug.Log("Error detected. Simulation stopped, but erroneous iteration's still running. Use debugger to debug the issue.");
                    Debug.Log("!!! Discrepancy detected while calculating pos by closest point: 1) [Using math] pos=" + posUsingMath + ", distance=" + distanceUsingMath
                              + "  2) [Using check method] pos=" + posUsingCheckMethod + ", distance=" + distanceUsingCheckMethod);


                    if (pointCheck)
                    {
                        Debug.Log("Reason: Result points varies more than " + BGCurve.Epsilon + ". Difference=" + Vector3.Magnitude(posUsingMath - posUsingCheckMethod));
                    }
                    if (distanceCheck)
                    {
                        Debug.Log("Reason: Distances varies more than 1cm. Difference=" + Math.Abs(distanceUsingMath - distanceUsingCheckMethod));
                    }

                    var mathPos        = math.CalcByDistance(BGCurveBaseMath.Field.Position, distanceUsingMath);
                    var checkMethodPos = math.CalcByDistance(BGCurveBaseMath.Field.Position, distanceUsingCheckMethod);
                    Debug.Log("Distance check: 1) [Using math] check=" + (Vector3.SqrMagnitude(mathPos - posUsingMath) < BGCurve.Epsilon ? "passed" : "failed")
                              + "  2) [Using check method] check=" + (Vector3.SqrMagnitude(checkMethodPos - posUsingCheckMethod) < BGCurve.Epsilon ? "passed" : "failed"));


                    var actualDistUsingMath            = Vector3.Distance(point, posUsingMath);
                    var actualDistanceUsingCheckMethod = Vector3.Distance(point, posUsingCheckMethod);
                    Debug.Log("Actual distance: 1) [Using math] Dist=" + actualDistUsingMath
                              + "  2) [Using check method] Dist=" + actualDistanceUsingCheckMethod
                              +
                              (Math.Abs(actualDistUsingMath - actualDistanceUsingCheckMethod) > BGCurve.Epsilon
                                  ? (". And the winner is " + (actualDistUsingMath < actualDistanceUsingCheckMethod ? "math" : "check method"))
                                  : ""));
                }
            }
        }
示例#8
0
    // Use this for initialization
    void Start()
    {
        // moving_manager = GetComponent<MovingManager>();
        timer  = GetComponent <TimeCalculator>();
        arrive = GetComponent <Arrive>();
        radius = 0.1f;

        pos = path.CalcPositionByClosestPoint(transform.position);
    }
示例#9
0
    // Use this for initialization
    void Start()
    {
        move = GetComponent <Move>();
        seek = GetComponent <SteeringSeek>();
        //    path = GetComponent<FollowCurve>().path;

        // TODO 1: Calculate the closest point from the tank to the curve
        current_point  = path.CalcPositionByClosestPoint(transform.position, out current_ratio);
        current_ratio /= path.GetDistance();
    }
示例#10
0
    // Use this for initialization
    void Start()
    {
        move = GetComponent <Move>();
        seek = GetComponent <SteeringSeek>();

        // TODO 2: Calculate the closest point in the range [0,1] from this gameobject to the path
        closest_point = path.CalcPositionByClosestPoint(transform.position, out point_distance);
        tot_distance  = path.GetDistance();
        current_pos   = point_distance / tot_distance;
    }
示例#11
0
    void Update()
    {
        var curveDistance = 0f;
        var curvePos      = math.CalcPositionByClosestPoint(this.transform.position, out curveDistance);
        var curveTangent  = math.CalcTangentByDistance(curveDistance);

        Debug.Log($"{curveDistance}, {curvePos}, {curveTangent}");

        transform.rotation = Quaternion.LookRotation(curveTangent);
    }
示例#12
0
    // Use this for initialization
    void Start()
    {
        move = GetComponent <Move>();
        seek = GetComponent <SteeringSeek>();

        // TODO 1: Calculate the closest point from the tank to the curve
        float distance;

        closest_point = path.CalcPositionByClosestPoint(move.transform.position, out distance);
        current_ratio = distance / path.GetDistance();
    }
示例#13
0
    // Use this for initialization
    void Start()
    {
        move = GetComponent <Move>();
        seek = GetComponent <SteeringSeek>();

        // TODO 1: Calculate the closest point in the range [0,1] from this gameobject to the path
        float distance;

        closet_point = path.CalcPositionByClosestPoint(transform.position, out distance);

        range_distance = distance / path.GetDistance();
    }
示例#14
0
    // Use this for initialization
    void Start()
    {
        move  = GetComponent <Move>();
        seek  = GetComponent <SteeringArrive>();
        agent = GetComponent <PoliceBehaviour>();

        // TODO 1: Calculate the closest point in the range [0,1] from this gameobject to the path
        float startDistance;
        float distance = path.GetDistance();

        desiredPoint = path.CalcPositionByClosestPoint(transform.position, out startDistance);
        currentRatio = startDistance / distance;
    }
示例#15
0
    private void Update()
    {
        Vector3 forwardVehiclePos = vehicle.position + (vehicle.forward * forwardDistance);
        Vector3 posHelper         = curve.CalcPositionByClosestPoint(forwardVehiclePos);
        Vector3 posEffective      = curve.CalcPositionByClosestPoint(vehicle.position);

        trackHelper.position = posHelper;

        float distance = Vector3.Distance(vehicle.position, posEffective);

        if (distance > minDistanceToShowHelper)
        {
            trackHelper.gameObject.SetActive(true);
            lookAtHelper.gameObject.SetActive(true);

            lookAtHelper.LookAt(posHelper);
        }
        else
        {
            trackHelper.gameObject.SetActive(false);
            lookAtHelper.gameObject.SetActive(false);
        }
    }
示例#16
0
    void Update()
    {
        if (Time.time > startTime + delay)
        {
            // get children positions as ratios
            var maxDist                = math1.Math.GetDistance();
            List <Transform> trList    = new List <Transform>();
            List <float>     ratioList = new List <float>();

            foreach (Transform t in transform)
            {
                if (t != transform)
                {
                    // get current position ratio
                    float curDist;
                    math1.CalcPositionByClosestPoint(t.localPosition, out curDist);                     // compare using local position
                    var curRatio = curDist / maxDist;
                    trList.Add(t);
                    ratioList.Add(curRatio);
                }
            }

            // useful distances
            maxDist = math1.Math.GetDistance();
            var   spacingRatio = spacing / maxDist;
            float deltaRatio   = Time.deltaTime * speed / maxDist;

            // update positions in curve
            float ratio = 1f + spacingRatio - 0.01f;
            for (int j = 0; j < trList.Count && j < ratioList.Count; j++)
            {
                ratio = Mathf.Min(ratioList[j] + deltaRatio, ratio - spacingRatio);
                var pos = math1.CalcByDistanceRatio(BGCurveBaseMath.Field.Position, ratio);
                trList[j].localPosition = pos;                 // set via local position

                // if ratio is close to 1.0, remove from list of children and zoom to player
                if (handleIdle && ratio >= .95f)
                {
                    trList[j].parent = null;
                    gameCtrl.EnemyIdle(trList[j].gameObject);
                }
            }
        }
    }
    /// <summary>
    /// Update the player's velocity and direction of movement based on the current state
    /// </summary>
    /// <param name="input"></param>
    public void Move(float input)
    {
        if (!m_touchingGround && !m_droppingIn)
        {
            m_currentSpeed = Mathf.Lerp(m_currentSpeed, m_rb.velocity.magnitude, 0.05f);
        }
        if (m_touchingGround && m_rb.velocity.magnitude > m_currentSpeed)
        {
            m_currentSpeed = m_rb.velocity.magnitude;
        }

        //in vert
        if (m_vert)
        {
            Vector3 m_currentRampPos;
            // Find the position on the coping spline that is closest to the player
            m_currentRampPos = m_currentQuaterPipe.CalcPositionByClosestPoint(transform.position, out float dist, out Vector3 tangent);

            // Calculate the player's new up based on the tangent of the current position on the spline
            Vector3 norm        = Vector3.Cross(tangent, Vector3.up).normalized;
            float   signedAngle = Vector3.SignedAngle(norm, m_currentUp, Vector3.up);
            m_targetRotation -= signedAngle;
            m_currentUp       = norm;

            // Correct the position's y value based on the player's y position
            m_currentRampPos.y = transform.position.y;

            // If the player is still within the bounds of the ramp's coping spline
            if (dist > 0 && dist < m_currentQuaterPipe.GetDistance())
            {
                m_rb.MovePosition(Vector3.Lerp(m_rb.position, m_currentRampPos, 0.5f));
            }

            // Grab a reference to the player's velocity
            Vector3 velocity = m_rb.velocity;
            // remove the vertical component for now
            velocity.y = 0;
            // Find the direction on the spline that the player is moving (tangent)
            Vector3 tan = (Vector3.Dot(velocity.normalized, tangent) > 0) ? tangent : -tangent;
            // Take out the vertical component
            tan.y = 0;
            tan.Normalize();
            // Force the velocity to coincide with the tangent
            velocity = tan * velocity.magnitude;
            // Re-introduce the vertical component of the velocity
            velocity.y    = m_rb.velocity.y;
            m_rb.velocity = velocity;
            return;
        }

        //grinding
        if (m_grinding)
        {
            Vector3 m_currentRailPos;
            // Find the position on the coping spline that is closest to the player
            m_currentRailPos = m_currentRail.CalcPositionByClosestPoint(transform.position, out float dist, out Vector3 tangent);

            // Calculate the player's new up based on the tangent of the current position on the spline
            Vector3 tanOfTangent = Vector3.Cross(tangent, Vector3.up).normalized;
            m_currentUp = -Vector3.Cross(tangent, tanOfTangent).normalized;

            // If the player is still within the bounds of the rail's spline
            if (dist > 0 && dist < m_currentRail.GetDistance())
            {
                m_rb.MovePosition(Vector3.Lerp(m_rb.position, m_currentRailPos, 0.5f));
            }

            // Grab a reference to the player's velocity
            Vector3 velocity = m_rb.velocity;
            // Find the direction on the spline that the player is moving (tangent)
            Vector3 tan = (Vector3.Dot(velocity.normalized, tangent) > 0) ? tangent : -tangent;
            // Force the velocity to coincide with the tangent
            velocity         = tan * m_currentSpeed;
            m_rb.velocity    = velocity;
            m_touchingGround = true;

            // Rotate to face the correct direction
            float signedAngle = Vector3.SignedAngle(m_moveDir, tan, m_currentUp);
            m_targetRotation += signedAngle / 2;
            m_currentRotation = m_targetRotation;

            Quaternion rotation = Quaternion.Euler(0, m_currentRotation, 0);
            // Find the tilt around based on the player's relative up
            Quaternion tilt = Quaternion.FromToRotation(Vector3.up, m_currentUp);
            // Apply the rotation combined with the tilt to the player's rotation
            transform.rotation = tilt * rotation;

            return;
        }

        //grounded
        if (m_touchingGround)
        {
            //holding faster
            if (input > 0)
            {
                if (m_currentSpeed < m_cruisingSpeed * 2)
                {
                    m_currentSpeed = Mathf.Lerp(m_currentSpeed, m_cruisingSpeed * 2, 0.01f);
                }
            }
            //holding nothing
            if (input == 0)
            {
                if (m_currentSpeed < m_cruisingSpeed)
                {
                    m_currentSpeed = Mathf.Lerp(m_currentSpeed, m_cruisingSpeed, 0.01f);
                }
                else
                {
                    m_currentSpeed -= 0.1f;
                }
            }
            //holding slower
            if (input < 0)
            {
                m_currentSpeed = Mathf.Lerp(m_currentSpeed, 0, 0.1f);
            }

            // Set the player's velocity
            m_rb.velocity = m_currentSpeed * m_moveDir;

            // Move the player to the ground
            m_rb.MovePosition(Vector3.Lerp(m_rb.position, m_currentGroundPos, 0.05f));
        }
    }
示例#18
0
 public Vector3 GetClosestPointAndDistanceByPoint(Vector3 worldPosition, out float distance)
 => math.CalcPositionByClosestPoint(worldPosition, out distance);