0
\$\begingroup\$

I'm currently rewoking my RTS cover system. This should highlight the closest coverspots from my mouse cursor and sent the units to these locations. everything works, but I can't get the closest sport, it always selects the sames ones, regardless to the mouse position.

These cover spots are child objects of the obstacle, that has this script attached.

This is my not working implementation of the idea:

 void Update()
    {

        var dist = Mathf.Abs(transform.position.z - Camera.main.transform.position.z);
        var v3Pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
        v3Pos = Camera.main.ScreenToWorldPoint(v3Pos);
        mousePos = v3Pos;
}

...

public void getPointDistances()
    {
        foreach (CoverPosition pos in coverPositions)
        {
            if (!pos.occupied)
            {
                pos.distanceToCursor = Vector3.Distance(pos.transform.position, mousePos);
            }
        }
        Array.Sort(coverPositions, delegate (CoverPosition x, CoverPosition y) { return y.distanceToCursor.CompareTo(x.distanceToCursor); });

    }

Later, I simply Iterate over the sorted array and make it visisible.

I'm woking on this for hours, but i cannot find, a solution why it always displays the same cover spots.

I hope, someone can find my (possibly stupid) error.

Edit:

I also tried this, but tata returned always the same values, regardless of my mouse positioning:

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
            mousePos = hit.transform.position;
\$\endgroup\$
2
\$\begingroup\$

It's hard to know exactly what the problem is without seeing how your scene is set up, but I suspect the issue is that your camera and your object are not even on the z-axis, which means that subtracting the z-vaules will not provide a true distance value to place the mouse pos in world space. Try the following:

    var dist = Vector3.Distance(transform.position, Camera.main.transform.position);
    var v3Pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
    v3Pos = Camera.main.ScreenToWorldPoint(v3Pos);
    Vector3 mousePos = v3Pos;

as for the edit, try using hit.point instead of hit.transform.position :

    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit)){
        Debug.DrawRay(hit.point,Vector3.up,Color.green);
        mousePos = hit.point;
    }

That being said, I would suggest using the ScreenPointToRay method to generate a point in worldspace to do a distance check.

EDIT:

You also may be having an issue with your sorting method. If you do not assign a value to a CoverPosition.distanceToCursor, it's default value is going to be 0, meaning a distance of zero, and these coverpositions are going to be pushed to the front of the array.

try something like:

 foreach (CoverPosition pos in coverPositions)
    {
        if (!pos.occupied)
        {
            pos.distanceToCursor = Vector3.Distance(pos.transform.position, mousePos);
        } else {
            pos.distanceToCursor = Mathf.Infinity;
        }

    }
    Array.Sort(coverPositions, delegate (CoverPosition x, CoverPosition y) { return y.distanceToCursor.CompareTo(x.distanceToCursor); });
}

Though, it may be more efficient to simply reorder the objects when doing the distance check, instead of setting the distance property, then doing a Sort by property.

\$\endgroup\$
  • \$\begingroup\$ Thanks fot he answers, the DrawnRay show the currect pint but I still get wrong numbers. I also tried ScreenPointToRay without success \$\endgroup\$ – pguetschow Feb 23 '17 at 20:18
  • \$\begingroup\$ See the edit I made, I believe its an issue with your sort method. \$\endgroup\$ – Michael Curtiss Feb 23 '17 at 20:19
  • \$\begingroup\$ Jup, that's true, I changed that already (also have the infinity part but cut that out to make the code shorter). The hit.point solved the problem. Thank you :) \$\endgroup\$ – pguetschow Feb 23 '17 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.