Greetings forums, I would like a bit of advanced help please.
A while ago, I made a proof-of-concept which upgrades the inventory example (Such as useable items, saving across scenes, raycast selection box, etc). When coming back to it, I want to add something else to it as well.
So, in most video games, inventories have the convenient feature which if the selection box is on the edge, and if the player tries to move it towards the edge, it will wrap back to the furthest inventory slot on the other end. Here is a visual aid (Gold square is the selector):
To accomplish the raycast box, I used the raycast example for the inventory. Although, I have no idea how to get the box to wrap to the opposite end with the raycase example. Can somebody help me on this please? Any help is appreciated. Here’s all the code for the selector (Mostly the same as the raycast example):
if right is pressed
->add (your distance between your slots) to X pos of selector.
→ if x pos of selector > (your max x) set X pos of selector to (your X pos of min X)
Do the opposite for left key
do the same for Y pos of selectors
and tbh the raycast doesnt really serve any super useful purpose here.
you can just add/substract X/Y positions. i did an inventory with this setup, and it worked perfectly. i also added a timer as condition for the position change of the cursor, for more precise controls (run through the inventory, by keeping the keys pressed).
Here’s what I have so far, which doesn’t work:
What do you mean by “add?” You mean store the distance in a variable?
no, i mean actual values.
example your inventory slots are 50pixel apart in x / y. aka slot 1 has X0/Y0 slot 2 has X50/Y0 …slot 5 has X0/Y50 slot 6 X50/Y50. the positions in your scene.
then you add that distance between them.
if right key is pressed add 50 to X pos of selector.
if left key is pressed subtract 50 from X pos of selector.
up = -50Y
down = +50Y
if slot 4 has pos X150 Y0
and slot 12 has X150 Y100
if right key
→ add 50 to xpos of selector
->if X of selector is >150 set x pos of selector = 0
You mean I should hard-code the values in? Here’s what I have right now:
When I press right, the selector moves right, but pushing right again doesn’t make it go any further.
Also, I would kind of like for the selector to be moved right onto the dead center of the slot. Should I put code to test the distance between corresponding slots for this to make sure the selector positions to the center and to prevent hard coding?
it doesnt move more, because you did not add (+) to the position, but set to (=) 160.
figure out the distance between your inventory slots in X and Y and then + or - that value.
if your inventory system is for a game you want to make, and the layout is pretty much set, then hardcoding is the best way.
here an example:
Okay, I did the hardcoding, and it works really well. To make sure the selector stays centered, I added an event to center the selector to the box it’s touching.
that should not be necessary, because the cursor should only be able to move by the distance of your inventory slots and therefore should always be centered automatically.
the only way this can be untrue would be, if ether your inventory slots do not have the same distance between them, or the initial position of your cursor is wrong.
I’m aware, I just added the centering event to play it safe