#Get the highest GameObject
1 messages · Page 1 of 1 (latest)
Let me record a little video for you. One second
Alright
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 🙂
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?
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
Are you dragging the cards according to the code you've previously sent?
https://paste.ofcode.org/zqi46GLVmfVfsfEgwsgQ9j
Could you, please, sent the updated version of it?
No, I am using an entire different script for the dragging
Then why are you sending a video, which uses a script not related to the discussed issue?
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)
I'm sorry, I don't seem to get what you mean
Is the card selected, shown in this video, assigned in your Raycast.cs script?
If so, please, send the script you might've updated
Sorry I was lawnmowing.
The raycast still doesnt hit the highest card.
I debug the card's instance id with the Raycast.cs script.
The raycast script is still the same
How should I be sure the wrong card is it?
Could you, please, try to drag it?
Because this code seems fine
https://paste.ofcode.org/zqi46GLVmfVfsfEgwsgQ9j
Because it debugs the same card 3 times
https://paste.ofcode.org/tPzp8RiBpv8sJJN9Zbi49Q
@torpid blade This it the updated script
But if I try to drag, nothing happens
Well, then the issue is not in the script you've sent
As it gets the card
If the card is gotten, and you cannot drag it, the issue is in not dragging the card correctly
Now everything is handled in the script I've sent here
Oh, I see, haven't mentioned it
Sorry
You don't need to be sorry, that's me who hasn't mentioned it, I'm sorry
Alright, now dragging works, but check this out:
Same old problem somehow ://
As I said, it doesnt get the highest card (the raycast)
First of all, the object you're dragging is not fixed
What do you mean by that?
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
}
}
https://paste.ofcode.org/VmYUB4uGiAiYe5GRMKF3hs
I noticed two things:
- The highest card raycast still doesnt work
- The dragging with the offset hides the card in the game view
Oh, damn, the thing I pointed out about Sort, that it's sorted from lowest to highest..
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
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]
RaycastHit2D hit = hits[hits.Length-1];
Yes, or reversed indexing with ^1
This one works, but the second issue stands
Thanks for fixing the first one!
and I learned something new
^1 = hits.Length - 1
^10 = hits.Length - 10
🙂 nice !
Yeah, that time I said your Sort method was wrong, it was actually correct
I miscalculated that part
I'm sorry
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));
Well, yeah, that's the issue, but it's surely not the best way to do it by converting it from world to screen and then again to world
Even if it works now, the code is still unclean
Yes it is, what would be the best solution though?
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
-
- Assigning the
Camera.mainto the variable
- Assigning the
private void Start()
{
_mainCamera = Camera.main;
}
-
- 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