#rocket

1 messages · Page 1 of 1 (latest)

tiny crag
#

@unkempt wedge I think what I'm having the most trouble with is "grabbing the rocket component"

unkempt wedge
#

Ok thats not too hard lets work trough it

#

So you have your 2 classes right?

tiny crag
#

yes

unkempt wedge
#

Ok so first step is to make your speed variable on your rocket public so that when we grab the rocket component we can modify the speed

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

public class rocket_move : MonoBehaviour
{
    Rigidbody rb;

    public float speed = 500f;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        
    }

    // Update is called once per frame
    void Update()
    {

        rb.AddForce(Vector3.forward * speed);

    }

    
}
#

I think I have that

unkempt wedge
#

k done

#

now all the other stuff is in the collision class or whatever you named it

#

Speedup I think

tiny crag
#

ok

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

public class Speed_Up : MonoBehaviour
{
    // Start is called before the first frame update


    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("Collided");
        GetComponent<rocket_move>().speed = 2000;
    }
}

this is what it looks like right now

#

ill get rid of the log thing

unkempt wedge
#

Ok so first lets talk about what GetComponent does so you understand

#

GetComponent<>() tries to access a component on the gameObject that its called on.

tiny crag
#

ah I see

unkempt wedge
#

So little quiz

tiny crag
#

so it was trying to find rocket_move on the collision object

unkempt wedge
#

What GameObject are you calling GetComponent<> right now?

#

And I mean in that example you showed me

tiny crag
#

im gonna say whatever gameobject has the speed_up script on it

unkempt wedge
#

Nope. And you really need to understand this because once this clicks its going to make your life much easier

#

Go look at your Rocket move script

#

and see how you made a variable named rb

tiny crag
#

oh shoot yeah

unkempt wedge
#

And now think about what I first said about GetComponent<>

tiny crag
#

hmm I honestly don't even know what to think

unkempt wedge
#

Ok i will nudge you lol

tiny crag
#

im sure its obvious

unkempt wedge
#

Lemme draw a pic

tiny crag
#

ok

unkempt wedge
#

take a look at this

#

You have your rocket GameObject yea?

#

on the very left

#

and its just this thing that is made up of Components

#

Those Components give it its behaviour

tiny crag
#

I see

unkempt wedge
#

Are you with me?

tiny crag
#

yeah

unkempt wedge
#

Ok

#

Whats the last component in the example picture?

tiny crag
#

rocket_move

unkempt wedge
#

Nice, yupp

#

your script you made, its also a component

#

So now we can look inside the rocket_move script

#

To access the information on the RigidBody Component on the GameObject

#

what did we have to do?

tiny crag
#

do getcomponent for the rigidbody

unkempt wedge
#

Yupp. We created a variable of Type Rigidbody called rb.
And then we used GetComponent<> to find it on the GameObject and stick that into rb.

#

So think about this for a minute.

#

What GameObject did GetComponent<> search through to find a RigidBody Component?

tiny crag
#

the rocket

unkempt wedge
#

Yupp.

#

Now look at your speed up class

#

What GameObject is GetComponent searching through to try and find Rocket_Move?

tiny crag
#

the rocket?

#

I feel dumb lmao

unkempt wedge
#

Its ok

#

Trust me

#

Once this hits

#

Your gunna be filled with joy

tiny crag
#

yeah I can already feel it

unkempt wedge
#

Ok so

tiny crag
#

what would the other choice even be tho

unkempt wedge
#

to make this easier

#

Scripts are components?

#

You understand that?

tiny crag
#

yes

unkempt wedge
#

You dragged a script onto your rocket GameObject yea?

tiny crag
#

yes

unkempt wedge
#

What script was that

tiny crag
#

rocket_move

unkempt wedge
#

Yupp. And what GameObejct did the GetComponent<> search through to find the Rigidbody?

tiny crag
#

the rocket

unkempt wedge
#

Yupp.

#

What GameObject did you drag the speed up to?

tiny crag
#

the trigger object that the rocket is supposed to collide with

unkempt wedge
#

Yupp. so what GameObject is the GetComponent<> as its used right now searching through ?

tiny crag
#

the trigger object

unkempt wedge
#

Actually whats the name

#

Forget about the trigger for now

#

Thats tripping you up

#

What GameObject is your speed up on?

tiny crag
#

"after_mercury" 💀 (it makes sense the the grand scheme of things)

#

we can call it "box" or something for simplicity

unkempt wedge
#

So, forgetting about the trigger stuff.

#

What GameObject is GetComponent<> searching through

tiny crag
#

after_mercury

unkempt wedge
#

Yupp.

#

Whats NOT on after_mercury?

tiny crag
#

rocket_move

unkempt wedge
#

There ya go

#

So post your speed up thing again

#

so we can see it

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

public class Speed_Up : MonoBehaviour
{
    // Start is called before the first frame update


    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("Collided");
        GetComponent<rocket_move>().speed = 2000;
    }
}
unkempt wedge
#

See that (Collider other)

tiny crag
#

yeah

#

I have no idea what that is I just found it on the unity docs

unkempt wedge
#

Thats what is called a parameter

tiny crag
#

yeah

unkempt wedge
#

bascially this OnTriggerEnter is passed a collider object when that collision (trigger) happens.

#

To get informations from that object

#

you would type ?

#

This may be a bit to hard without knowing

tiny crag
#

yeah hold on what

unkempt wedge
#

but you would type other

#

other.name
other.tag
other. information I want from this thing

tiny crag
#

I see

unkempt wedge
#

So knowing what we talked about eariler, how GetComponent<> searches on the GameObject its presented

#

and knwoing that 'other' is how you access the colliding GameObject

#

what can you deduce

tiny crag
#

"other" is the rocket?

unkempt wedge
#

Yupp

tiny crag
#

ok ok

unkempt wedge
#

other could technically be anything btw that collides

tiny crag
#

yeah

unkempt wedge
#

But for now yes

#

for simplicity sake

#

And heres something

#

so type this

#

one sec lemme opne unity so i get all this right

#

First thing is to check to see what we collided with

#

so using the things name

#

how can you check to see if somethings name = seomthing

tiny crag
#

maybe

unkempt wedge
#

Your not far off

#

the strcture is there

#

But I want to show you some stuff

#

Now you replace

#

with what you have

tiny crag
#

where does "other" come into play here

#

or does it at all

unkempt wedge
#

other is the object colliding (in this case its actually a trigger)

#

So imagine you put this speed up script on a cube
other is any object that collides with said cube

#

other does come into play here

#

give it a try

tiny crag
#

ok..

unkempt wedge
#

if (the object's name that I collided with == "the name of my rocket")

#

type if your getting stuck or where the hang up is

#

so I can correct or steer you

tiny crag
unkempt wedge
#

Nope, what did we say was the way to access the gameObject earlier

#

using what

#

It wasnt Collider

tiny crag
#

getcomponent

unkempt wedge
#

Nope

tiny crag
#

im lost

#

sorry this is probably so frustrating lmao

unkempt wedge
#

other

#

Its not at all

tiny crag
#

ok yeah

unkempt wedge
#

I remember this from when I was there

tiny crag
unkempt wedge
#

^

tiny crag
#

alr

unkempt wedge
#

You know what here

#

this may makes more sense

#

change other to anything you want

#

you could name it trigger

#

collision

#

collider

#

any lowercase word

#

you want

tiny crag
#

ok yeah that will probably help

#

also I have to ask quickly, will I ever be dealing with the Collider input?

unkempt wedge
#

You mean the first word in Collider other?

tiny crag
#

yeah

unkempt wedge
#

No. Thats a Type.

tiny crag
#

ok ok

unkempt wedge
#

just like
Rigidbody rb;

#

Collider other;

#

Its the TYPE of what something is

tiny crag
#

yeah I keep forgetting

unkempt wedge
#

Its ok lets keep moving

#

So show me what you got so far

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

public class Speed_Up : MonoBehaviour
{
    // Start is called before the first frame update


    private void OnTriggerEnter(Collider other)
    {
        GetComponent<rocket_move>().speed = 2000;

        if(other.name == "RocketPlayer")
        {
            Debug.Log("Collider = rocket")
        }
    }
}
unkempt wedge
#

You know how to make comments?

tiny crag
#

yeah //

unkempt wedge
#

Yupp make a comment above the onTriggerEnter in your own words explaining that other is how you access the Gameobject that collides with this things

tiny crag
#

ok thats a good idea

unkempt wedge
#

Make one above the if (other.name == "RocketPlayer") too explaining that
we have to say
// if THIS is the OBJECT we want THEN do some stuff
or whatever in your own words

#

get rid of the GetComponen<> too

tiny crag
#

ok

unkempt wedge
#

Then show me what you got again

#

Almost done honestly

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

public class Speed_Up : MonoBehaviour
{

    // Collider = type for collider (don't worry about)
    // other = the object that is coliding 
    private void OnTriggerEnter(Collider other)
    {
        //if the name of the colliding object is equal to a chosen name ("RocketPlayer" in this case)
        if(other.name == "RocketPlayer")
        {   //prints
            Debug.Log("Collider = rocket")
        }
    }
}
unkempt wedge
#

Yupp, now hit play and try it out to make sure it works

#

and you should see the debug when you hit it with rocket

tiny crag
#

alr

#

compile error 👍

#

oh mb

#

forgot semicolon

#

ok lets try that again

unkempt wedge
#

happens

tiny crag
#

im used to python

unkempt wedge
#

LETS GOOOOO

#

Yea thats mess u up lol

tiny crag
#

nothing happened lmao

unkempt wedge
#

You sure RocketPlayer is exact?

#

The spelling

tiny crag
#

Let me check the rigidbody settings and stuff I was messing with them

unkempt wedge
#

You sure this is a trigger?

#

that the names are correct?

tiny crag
#

is only the rocket supposed to be a trigger

unkempt wedge
#

No

#

The trigger should be the thing the rocket goes through

tiny crag
#

or wait it should be the other eay

#

yeah

unkempt wedge
#

Both objects need colliders

#

And at least one of them needs a rigidbody

tiny crag
#

ok trying that again

unkempt wedge
#

LETS GOOOOO

#

whoooo

tiny crag
#

nothing happened 🔥

#

ok i need to check names and stuff

unkempt wedge
#

You are absolutely positive that RocketPlayer is correct

#

A single typo or space

tiny crag
#

is it because its an empty with children

#

probably not

#

hmm

unkempt wedge
#

What?

#

it has to have a collider

tiny crag
#

it does

unkempt wedge
#

On the same object as the script

tiny crag
#

yeah

unkempt wedge
#

Ok

tiny crag
#

oh wait i think i might know

#

nvm

#

hmmm this is weird

unkempt wedge
#

Take screenshots of both objects pls

#

To make this really simple

tiny crag
#

my only last guess is because the collision object is a plane maybe even though convex is checked

#

ok

unkempt wedge
#

Dont do planes

#

You mean the trigger is a plane?

tiny crag
#

yeah

#

that might be messing with it

unkempt wedge
#

Could be

#

can it be a thin cube?

tiny crag
#

yeah ill change it

#

ok wait so the rocket shouldn't have "is trigger" checked but the cube should

#

?

unkempt wedge
#

yes

#

correct*

#

only the TRIGGEr should

tiny crag
#

its still not printing anything very odd

#

ill send screenshots

unkempt wedge
#

wait

#

put a debug log above the if statement

#

and print out anything

tiny crag
#

good idea

unkempt wedge
#

lets see if the condition is broken

#

or the entire trigger

tiny crag
#

like this right

unkempt wedge
#

yupp

tiny crag
#

oops lod

unkempt wedge
#

yea besides that lol

tiny crag
#

bruh semicolon fkglwer

unkempt wedge
#

give it a week

#

trust me, you keep at it and writing code and you'll forget less and less

tiny crag
#

it didn't print anything

unkempt wedge
#

Ok so now we know its broken in general

#

Now you can screen shot both GameObject and their components

tiny crag
#

oh wait

#

im an idiot

#

it was working

#

I had one of these things turned off

#

broo 😭

#

ok its fine

unkempt wedge
#

lolol lessons being learnt

tiny crag
#

ok well thats good at least

unkempt wedge
#

So you have everything working lets move on to the FINALE

tiny crag
#

yes

unkempt wedge
#

Get rid of the first debug.log

#

and the second one too i guess

#

we know its working

tiny crag
#

ok

unkempt wedge
#

What do you need to do now?

tiny crag
#

I guess just how to make it change the speed value

unkempt wedge
#

Yupp. Whats the speed variable stored in?

#

ima quiz you again

tiny crag
#

ah shi

unkempt wedge
#

weve learned alot and its time to put it all together lol

#

These arent trick questions just take some time

#

what is the speed stored on?

tiny crag
#

the speed variable is stored in the rocket_move script, which is a component of the RocketPlayer object

unkempt wedge
#

Boommmmm

#

So how can you access an object when it hits your trigger?

#

your already doing this btw

tiny crag
#

by using other?

unkempt wedge
#

yupppppppp

tiny crag
#

or whatevers its named

unkempt wedge
#

yupp

#

stick with other

#

for now lol

#

dont change up anything at this point

tiny crag
#

ok im guessing from here its gonna be something along the lines of getcomponent of other

unkempt wedge
#

Yupp

#

thats exactly what it is

#

so do it or attempt it and then show me

tiny crag
#

ok

#

something like this?

unkempt wedge
#

nooooooooooo come on lol

#

let me lead you with some questions

tiny crag
#

I have no idea how to format the getcomponent stuff

unkempt wedge
#

how did you access the name in the condition above?

tiny crag
unkempt wedge
#

yupp so access it again

#

and THEN get the component

tiny crag
#

oh wait

unkempt wedge
#

thats it

#

You are saying
grab other

#

and then GetComponenet<rocket_mover>()

tiny crag
#

fixed the rocket_mover lmao

#

ok I understand it now

unkempt wedge
#

Cool

#

Now set that to something

tiny crag
unkempt wedge
#

Yupp, now test it out

tiny crag
#

ok

unkempt wedge
#

Seems like a amassive speed change but well see

tiny crag
#

it worked 🔥 🔥

unkempt wedge
#

Cool now lets take this just a bit further, theres one more thing I want to show you

tiny crag
#

ok sounds good

unkempt wedge
#

You never really want to use magic numbers like you did. aka directly setting something to a number

#

instead set it to a variable

tiny crag
#

yeah I should have made variable

#

yeah

#

because that can be edited then

unkempt wedge
#

So create a variable at the top but use [SerializeField] float speedBoost = 2000f;

#

you sohuldnt make things public by default

#

or you might accidentily change them when you dont mean to

#

[SerializeField] lets us keep a variable private AND see and change it in the inspector

tiny crag
#

ah ok

#

I was wondering about that

unkempt wedge
#

Show me when done and I have one more thing to tell you

#

and I gotta bounce

tiny crag
#

yeah me too

#

ok

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


[SerializeField] float speedBoost = 2000f;

public class Speed_Up : MonoBehaviour
{

    // Collider = type for collider (don't worry about)
    // other = the object that is colliding 
    private void OnTriggerEnter(Collider other)
    {
        //if the name of the colliding object is equal to a chosen name ("RocketPlayer" in this case)
        if(other.name == "RocketPlayer")
        {   
            other.GetComponent<rocket_move>().speed = speedBoost;
        }
    }
}
unkempt wedge
#

Awesome

#

NOW you can drag this script onto ANY other gameObject that you want to be a trigger. And change that speedBoost in the inspector. You just have to make sure it has the collider with trigger etc....

#

You've now made a modular speed boost script

tiny crag
#

yeah

#

nice

unkempt wedge
#

different values on anything you want

tiny crag
#

mhm

#

thank you so much man

unkempt wedge
#

Cool well hoped you made some connections in your brain

#

takes time

tiny crag
#

Can't even tell you how grateful I am lol

unkempt wedge
#

All good. Youll see me in the discord im sure more.

#

Keep learning

#

and good luck

#

You can delete this whole thread whenever, no rush