Hi, I need help. I want my character to walk or jump from square to square when you roll the dice, instead of teleporting. The number you get, that many squares he moves forward.
How do I do that? I tried with adding force, force towards the object, i tried even with tween, but it didn’t worked. After first few rolls, character started moving forward without stopping, no matter what number i got.
I would be very grateful if someone could help me please.
This is how my events are currently looking:
It looks like you have objects for each place the character can go (NewSprite[X]). It’d be handy to see how these are laid out in your game. For example, are the just in a long line? Or are they in a grid (like Snake & Ladders)? Or do they follow a winding path?
That might help come up with a solution to this for you…
Thanks @igorthegamedeveloper, that’s helpful.
My first thought (not fully fleshed out) is that you could maybe do something along these lines:
- Have a ‘direction’ variable for the character that can be ‘left’, ‘right’, ‘up’ or ‘down’
- Add hidden objects to each corner of the path that, when collided with, change the direction variable of the player (so if you go around clockwise, when you’re moving down the right side and collide with the object in the bottom-right corner, it changes the character’s direction variable to ‘left’)
- Set a variable that holds the current roll
- Have an event that moves the character one grid square as long as the current roll variable is > 0, then decrement it by one after each move
- Have sub-events that determine whether the character moves one grid square up, down, left or right based on the character’s direction variable
So the idea would be that you roll the dice, it sets the roll variable to, say, 5. Then the event kicks in that runs whenever the roll variable is > 0. If your current direction is ‘up’, it says to change the character’s Y position by a negative amount (whatever the size of your grid squares are). So the character moves/jumps up one square. Then the event decreases the roll variable by 1. Now it’s 4 (still > 0) so it moves you up again. If you collide with a hidden corner object, that changes your direction, so then the next move will be adding an amount to your X position (to move right).
Thanks @BWPanda, I will try🙂. What event should I use for moving my character so he will be walking or jumping from square to square? Now he just teleports and although it works, it doesn’t look too nice.
@anon14980890 I’ve tried to do as you told, but now my character moves for 6 squares, no matter what number I got on dice, and then doesn’t move at all when I roll again.
A few things -
- you haven’t provided enough information - can you screen snip the events that move the character?
- don’t use RandomFloatInRange() when you are after an integer value - use RandomInRange().
- you can reduce the dice roll animation and variable setting to one event, making it more compact and easier to work with :
Add the tween behaviour to you GenericCharacter2 object, and then use a positional tween to move the character :
Ah sorry, I forgot to mention that you’ll need a timer to make the movement slower. And you also don’t want/need a ‘Trigger once’ on the event that checks the ‘roll’ variable.
I wanted to make sure my ideas worked (and have a bit of fun too), so I whipped up this demo and it works nicely:
Hope that helps.
@anon14980890, if you modify the change position to a tween, then the movement will be smooth and better looking.
But otherwise, nice job, I hope @igorthegamedeveloper can learn from what you’ve done. And you’ve condensed 3+ screens of events into less than 1, and made it scalable so if the board takes on another shape/path, it will still work.
@BWPanda I’m trying to do as you did, but I ran into a problem when doing set animation of dice to ToString(dice.Variable(roll)). Did I missed something?
@anon14980890 What is movingIndicator and where should I place it?
You’re setting the animation number. You want to set the animation by name instead:
The movingIndicator is the 3 dots that act like a loading icon when the character is moving. You don’t need it. I just added it so it was easy to see when the character was moving so you know not to press the dice again. But there are better ways of doing that (e.g. setting the dice animation back to the question mark when the turn’s over and you can click it again).
Aha, I understand now. I’m trying to do as you did in demo. I hope it will work.
@BWPanda I don’t understand what’s with change direction. Why is it only one when on picture there are four, one for going up, down, right and one for left. And grid_size. Do I need to make a scene variable or I type the number which is size of my grid.
‘changeDirection’ is a sprite object with 4 different animations, labelled ‘left’, ‘right’, ‘up’ and ‘down’. Kind of like how the dice has different animations. With the dice we change the animation in events based on a click. But with the changeDirection object you can set the animation of each instance when you add it to the scene. So I put one in the top-right corner, and set the animation to ‘down’ for example. This is a handy way to simplify the game when you have objects that look and act similar - just have one object with multiple animations rather than 4 similar objects.
As for the grid_size, you can either set it as a variable or just type the number directly in the event. I’d recommend using a variable, because then you only need to set the value once, and don’t need to remember it. That’s the ‘proper’ way to do it. But totally up to you.
@BWPanda How do I change its animation for each instance when I add it to the scene?
@BWPanda I did it like this and character still doesn’t move when I roll the dice.
Uploading: Screenshotnew2.png…
Looks like you’re not setting the roll variable in the click event. You’re setting the dice animation twice…