#Get the highest GameObject

1 messages · Page 1 of 1 (latest)

torpid blade
#

What exactly doesn't work? The cards should be gotten correctly, aren't they?

night jetty
#

Let me record a little video for you. One second

torpid blade
#

Alright

night jetty
#

So the "Get the highest card" is only works debug wise

#

I mean "works"

#

Debug.Log(selectedObject.transform.position);

#

Oops its my bad, I didnt update the sortingOrder

#

Let me resend 🙂

torpid blade
# night jetty

Well, the highest card is gotten correctly, according to the x position

#

So you simply assign it as you wish now

#

What're the further questions?

night jetty
#

The debug doesnt seem to work, but I want to make a try. How can I assign it so the mouse drags the highest gameObject?

#

As I said I have an interact script assigned to every card. I'm controlling the object movement on OnMouseDown, OnMouseDrag

#

@torpid blade

torpid blade
#

Could you, please, sent the updated version of it?

night jetty
torpid blade
night jetty
#

Sorry for the confusion:

  • I'm getting the highest card with the Raycast.cs, assigned to one instance of a gameobject
  • Then I have another script called Interact.cs, which relates to the dragging and moving objects with mouse.
  • The highest gameobject only exists within the Raycast script, but I somehow want so that the mouse only picks up that (handled in the interact script, assigned to every card)
torpid blade
torpid blade
# night jetty

Is the card selected, shown in this video, assigned in your Raycast.cs script?

#

If so, please, send the script you might've updated

night jetty
#

Sorry I was lawnmowing.
The raycast still doesnt hit the highest card.
I debug the card's instance id with the Raycast.cs script.

night jetty
torpid blade
#

Could you, please, try to drag it?

night jetty
night jetty
#

But if I try to drag, nothing happens

torpid blade
#

As it gets the card

#

If the card is gotten, and you cannot drag it, the issue is in not dragging the card correctly

night jetty
torpid blade
#

Oh, I see, haven't mentioned it

night jetty
#

Sorry

torpid blade
#

You don't need to be sorry, that's me who hasn't mentioned it, I'm sorry

night jetty
#

Alright, now dragging works, but check this out:

#

Same old problem somehow ://

#

As I said, it doesnt get the highest card (the raycast)

torpid blade
#

First of all, the object you're dragging is not fixed

night jetty
#

What do you mean by that?

torpid blade
#

In the condition if (hits.Length > 0) the object selectedObject be assigned, which you do. The code, which performes the drag should be executed outside of the condition if (Input.GetMouseButton(0)) and check whether the selectedObject is null

#

Additionally, GetMouseButton should be changed to GetMouseButtonDown

#

It's strange how I haven't mentioned it before

#
if (Input.GetMouseButtonDown(0))
{
    // ...

    if (hits.Length > 0)
    {
        selectedObject = // ...
    }
}
else if (Input.GetMouseButton(0))
{
    if (selectedObject)
    {
        // perform drag
    }
}
night jetty
torpid blade
night jetty
#

This is an interesting issue, because as I see it, the sortingGroups orderInLayer is in good place. So I don't know why the raycast is not getting the highest card

torpid blade
#

Here you get the 1st hit, even though it's sorted from lowest to highest

RaycastHit2D hit = hits[0];
#

You should simply get the last one, hits[^1]

night jetty
#

RaycastHit2D hit = hits[hits.Length-1];

torpid blade
night jetty
#

This one works, but the second issue stands
Thanks for fixing the first one!

night jetty
torpid blade
#
^1 = hits.Length - 1
^10 = hits.Length - 10
night jetty
#

🙂 nice !

torpid blade
#

I miscalculated that part

#

I'm sorry

night jetty
#

No worries!

#

I would like to fix the offset part, and thats that done! 🙂
I'm thinking about it getting the wrong z position

#

Got it!!!

#

it was
offset = selectedObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(selectedObject.transform.position).z));

torpid blade
#

Even if it works now, the code is still unclean

night jetty
#

Yes it is, what would be the best solution though?

torpid blade
#
offset = selectedObject.transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition);

selectedObject.transform.position = offset + Camera.main.ScreenToWorldPoint(Input.mousePosition)
#

This way

#

Changing the z position is redundant

#

You should simply get the difference between the current and start mouse positions

#

Input.mousePosition is Vector2, so when transforming it into Vector3, the z position stays 0

#

Also I would consider

    1. Assigning the Camera.main to the variable
private void Start()
{
    _mainCamera = Camera.main;
}
    1. Creating a property for the world space mouse position
private Vector3 WorldMousePos => 
    _mainCamera.ScreenToWorldPoint(Input.mousePosition);
#

The Sort method should also be changed to

  • OrderBy
RaycastHit2D hit = hits.OrderBy(h => hit1.collider.GetComponent<SortingGroup>().sortingOrder)[^1];
  • OrderByDescending
RaycastHit2D hit = hits.OrderByDescending(h => hit1.collider.GetComponent<SortingGroup>().sortingOrder)[0];
#

collider.gameObject.GetComponent can be simplified by collider.GetComponent, as Collider is a Component, which has Component.GetComponent method

#

hits may be set directly

RaycastHit2D[] hits = Physics2D.RaycastAll..;

and offset should be renamed to something like startPos

#

That's pretty everything

#

Though I would still consider using 2 methods I have previously suggested in #💻┃code-beginner, which don't require enumerating through every raycast