Character collision turning

I am looking to have my character turn when they hit a wall, I can do it with one wall but not with multiple walls. Thanks for any help https://cospac.es/edu/7B2c

Hi @Outpost_31,

your code was getting close! In your if-statement you compare another item to the walls-variable, but not to the item inside of that list.

In order to get the item stored in the list and to move the item properly, you can do the following:

Code in action
CodeInAction

How it works
When the boy collides with a wall, we look through the walls - list for the item that the boy is colliding against. If the item is found, we turn the boy by calling the turnItem - function.

In the turnItem - function, we stop all transitions of the boy and then turn him around. Afterwards, we start the walkItem function again.

At the beginning of the scene, we also call the walkItem - function once so that the boy walks forward.

Hope this helps!

1 Like

Awesome, thanks so much. I am wondering why do you need “in list get walls #” why couldn’t you just place “in list walls find first occurance…” there instead?

@Outpost_31 That’s how arrays (lists) work. The in list ... find first occurrence of item command returns the index (position) of an item in a list. To get the value of that item you need to use in list ... get # command, passing it the index of the item.

1 Like

thanks for that, makes sense now

New to CoSpaces here :grinning:

Maybe we could streamline the conditional statement a bit.
Based on the help tip for the “in list [] find [first] occurrence of item [ ]”, if the item is not found, this block returns zero (0).
in_list_find

If I understand the request correctly, it does not seem too important to know what section of the wall the ‘Fancy boy’ collides with, so the conditional could be:
conditional

Hi @arivera,

that statement block was indeed a bit cumbersome to use. This is why we kept the block design more straightforward in CoBlocks:

26

Click here to see full CoBlocks code version

Functions

Play

1 Like

Hey,
When i tried the code it worked for the wall in front of the character. I added controls so that the character can move left and right but when you move the character left or right towards the wall it phases through it.
Could you help me with this?
Thanks
(link: CoSpaces Edu: Make AR & VR in the classroom )

Hi @SaltedKetchup,

please make sure to use the share-link of your space. The one you posted is your personal link only accessible by your account.

To get your share-link, click the share button in the toolbar header when editing the space.

Hey Stefan,
I can’t share my space since I am currently using a student account.
Here are a few screenshots of my code since i can’t give you a link.

!

Note: some photos are not in order

Hi @SaltedKetchup,

I see that you have two scripts in your scene. One made in CoBlocks, and one made in Blockly. It’s very likely that they create issues with each other.

Try disabling or removing the blocks of your Blockly script. Also make sure that you put the moving item (the girl) far enough behind so it does not collide the wall while she turns around, otherwise the TurnItem function will be called over and over.

Below you see a small version of your collision detection script. The space also has a few scenes that show how to manage your item events. Feel free to click the remix button to see how they were done :slight_smile:

Thanks @Stefan
It works well. Thanks for your help!

1 Like