#how can i randomly put coins in the air

1 messages · Page 1 of 1 (latest)

static trout
topaz oak
#

hi

static trout
#

bruh

static trout
topaz oak
#

OK so let me get this right, you want the coins to spawn randomly, right?

#

within a certain radius

static trout
#

exaclty

topaz oak
#

OK that should be easy

#

make a script called CoinSpawner or something similar

static trout
#

LETS DO IT

topaz oak
#

alright firstly, do you know what arrays are?

static trout
#

done

#

no

topaz oak
#

do you know what ints are

static trout
#

wtf is arrays

static trout
topaz oak
#

do you know what gameobjects are

static trout
#

ofc

topaz oak
#

just double checking to see how in depth i have to explain this 😄

#

i'm a games programming student so im learning this stuff aswell

static trout
#

cool

topaz oak
#

alright, firstly you want to make a private gameobject called coins

static trout
#

got u

#

this is how i type GameObject

#

or Gameobject

#

or gameObject

topaz oak
#

should look something like this

#

don't forget the [SerializeField] and the [] on the gameobject

#

and obviously call the points coins

static trout
#

and

topaz oak
#

have you done that?

static trout
#

yep

topaz oak
#

Okauy

#

okay

#

the thing is im not sure how this is gonna work as its 2d but im still happy to try

static trout
#

ok

topaz oak
#

now head to unity and make a gameObject called Waypoints/CoinPoints etc

static trout
#

and

topaz oak
#

should look like this

static trout
#

it does

static trout
topaz oak
#

hi

#

be more patient

static trout
#

ok lets complete

static trout
topaz oak
#

OK now make 3 gameobjects as children of Waypoints or whatever you call it

#

and call them Spawnpoint 1, 2 and 3

#

should look something like this

static trout
#

and

topaz oak
#

so when you click the down triangle everything collapses

static trout
#

ok

topaz oak
#

does that happen?

static trout
#

what?

static trout
#

now?

topaz oak
#

nice okayu

#

now click on waypoint 1

static trout
#

and

topaz oak
#

click on this dropdown menu

static trout
#

and

topaz oak
#

it should have loads of colours

#

with shapes

#

click any of them

static trout
#

and

topaz oak
#

then do it for every waypoint

#

it can be the same colour

#

should be the same colour*

#

then send me a screenshot

static trout
topaz oak
#

make them the same colour and move them to where you want the coins to spawn

static trout
#

like on them

#

or in the raduis

#

betwwen them

topaz oak
#

show me your whole screen

static trout
topaz oak
#

ok make all the colors the same for the spawnpoints

static trout
#

and

topaz oak
#

just for now, put 1 in each corner

static trout
#

of the map?

topaz oak
#

just for now

#

😄

static trout
#

and

topaz oak
#

also you need to learn putting stuff in right folders but well do that later

#

make an empty game object called 'Spawner' and drag the script on to Spawner

static trout
#

and

topaz oak
#

you did that, yea?

static trout
#

yea

topaz oak
#

okay now go to the inspector, it should look like this

#

click the + and drag the waypoints you made in there

static trout
#

yep

#

wait

#

where do i add the script on the spawner

topaz oak
#

drag it

topaz oak
static trout
topaz oak
#

did you do it then

static trout
topaz oak
#

show me the script

static trout
static trout
#

i add it

#

oh sorry

topaz oak
#

ok now save

#

and re run

#

then show me it again

static trout
topaz oak
#

nice now go back to the script spawning

#

you're very new to coding, aren't you

static trout
#

yep

#

but not that new

#

i coded before

#

for like 3 days

topaz oak
#

you need to name your scripts something easier and memorable

topaz oak
static trout
topaz oak
#

wait go back to unity quickly

static trout
#

why?

topaz oak
#

click on this

static trout
#

i will add a tag and name it waypoint

#

just a min

topaz oak
#

tag it waypoint

#

yeah

static trout
#

done

topaz oak
#

nice

#

now uhh go back to the script

static trout
#

and

topaz oak
#

in your start function you need to do coin = GameObject.FindGameObjectsWithTag("waypoint");

#

so it finds all of the waypoints you created

static trout
#

i just stole your code

#

lol

#

what else

topaz oak
#

now you need to make a vector2 function called getRandomSpawnPoints or something similar

static trout
#

public or private

topaz oak
#

up to you

static trout
#

i will do public

topaz oak
#

private makes more sense as it'll only be accessed in this script

static trout
#

U SAID ITS UP TO YOU

topaz oak
#

now inside your new function you need to make a Vector2 called pos

static trout
#

dont know i think Vector2.pos

topaz oak
#

Vector2 pos;

static trout
#

it

static trout
topaz oak
#

now on a newline, type pos.x =

static trout
#

and

topaz oak
#
pos.x = coins[Random.Range(0, points.Length)].transform.position.x + Random.Range(-7, 7)```
#

something like that

static trout
#

wow thx i will steal that

topaz oak
#

change points.length to coins.length

static trout
#

didnt understand

topaz oak
static trout
#

like what i change

topaz oak
#

the cofde

#

code

static trout
#

what part

topaz oak
#

points.length to fucking coins.length

#

its crossed out in the picture man

#

show me your script

static trout
#

what do i change it into

topaz oak
#

whats so hard man

static trout
topaz oak
#

when you screenshot code show me the line numbers

static trout
topaz oak
#

you see in line 22

#

its the same code as the one ive screenshotted multiple times and shown you what to change what into

#

do the same in your code

static trout
#

ohhhhhhhhhhhhh

#

now i understand

#

sorry

#

yay

#

now what

topaz oak
#

now you need to do the same for Y

#

but you don't want random y right?

#

if you don't, remove the + Random.range at the end

static trout
#

nvm

#

like that

topaz oak
#

remmove the ramdon range at the end

static trout
topaz oak
#

ur code is monkey

static trout
#

wdym

static trout
topaz oak
#

you only need 1 vector 2

#

called pos

#

not posx and posy

static trout
#

ohhhhh

topaz oak
static trout
#

cuz i dont understand

topaz oak
#

you don't know basic coding so learn that before you start projects i guess

static trout
#

anyways lets complete

topaz oak
#

type 'StartCoroutine(SpawnEnemies());' in the start function

static trout
#

and

topaz oak
#

now make a new IEnumerator function called SpawnEnemies

static trout
#

and

topaz oak
#

show me the script

static trout
topaz oak
#

the whole s cript

static trout
#

bruh now i have to go study

topaz oak
#

ok

#

bye

static trout
#

bruh your even happy

#

anyways well complete anthor time

topaz oak
#

okay

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnPoints : MonoBehaviour {
    
    #region Private Variables
    [SerializeField] private GameObject[] points;
    [SerializeField] private GameObject[] enemies;
    private int spawnCount, enemyCount;
    private float spawnTimer;
    [SerializeField] private Animator anim;

    #endregion

    void Start() {
        points = GameObject.FindGameObjectsWithTag("waypoint");

        spawnCount = 10;

        StartCoroutine(SpawnEnemies());
    }

    void Update() {

    }

    IEnumerator SpawnEnemies() {
        while (enemyCount < 1) {

            var spawnedEnemy = Instantiate(enemies[0], getRandomSpawnPoint(), Quaternion.identity);
            spawnedEnemy.transform.parent = GameObject.Find("====== ENEMY ======").transform;
            spawnedEnemy.name = $"Enemy " + enemyCount;
            yield return new WaitForSeconds(0.15f);
            enemyCount += 1;
        }
    }

    private Vector3 getRandomSpawnPoint() {
        Vector3 pos;
        pos.x = points[Random.Range(0, points.Length)].transform.position.x + Random.Range(-7, 7);
        pos.y = points[Random.Range(0, points.Length)].transform.position.y;
        pos.z = points[Random.Range(0, points.Length)].transform.position.z + Random.Range(-7, 7);


        return pos;

    }



}```
#

heres my whole script

#

you can figure it out, its super easy i believe in you

static trout
#

LETS DO IT

topaz oak
#

I'm busy doijgnmy uni work

#

After sure

static trout
#

ok sorry

static trout
topaz oak
#

I just got out of the shower man

static trout
#

ok ok

static trout
topaz oak
#

Probably not today