Random Movement of Objects

#1

Hello! I am a student, working on a project in CoSpaces and I want to know, if there is a way to generate random movement of objects? I’m trying to make a VR environment and it’s an underwater environment. I have a dolphin that I want to make move randomly without a path. How can I do that using CoBlocks?

#4

Hi @Marissa_Newbold,

To generate random movement for your underwater animals you can use a function block that repeats movement forever. Here’s how:


Let’s assume that our “random movement” means swimming forward and turning around. This is repeated forever in a loop.

Let’s create a function “setRandomMovement” and give it a few parameters so that we can change the swimming distance and speed of the animals.

Now we add the movement CoBlocks!

To make the movement more random, we pick a number between -180 and 180 whenever the item should turn around

Now we can use this function below the yellow When Play clicked - CoBlock.

In this block we select the Dolphin1 item to be moved 3 meters in 2 seconds.

Let’s check out this code in action! After pressing “Play” you can also press the code-icon in the top-right to check out the CoBlocks debugger which shows you what your code is doing.

Our dolphin moves with a random pattern!

Let’s add more items. Use the run parallel - CoBlock to call this function on other animals.

Finally, we have several animals moving about randomly :slight_smile:


If you want to make a copy of the example space, check out the remixable example below by pressing the remix - button.

I hope this helps! If this got you curious about what else you can do with CoBlocks, head over to our CoBlocks forum tutorial section!

#5

@Stefan Wow! Thank you so much, that has helped a lot!