#rocket
1 messages · Page 1 of 1 (latest)
@unkempt wedge I think what I'm having the most trouble with is "grabbing the rocket component"
yes
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
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
k done
now all the other stuff is in the collision class or whatever you named it
Speedup I think
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
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.
ah I see
So little quiz
so it was trying to find rocket_move on the collision object
What GameObject are you calling GetComponent<> right now?
And I mean in that example you showed me
im gonna say whatever gameobject has the speed_up script on it
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
oh shoot yeah
And now think about what I first said about GetComponent<>
hmm I honestly don't even know what to think
Ok i will nudge you lol
im sure its obvious
Lemme draw a pic
ok
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
I see
Are you with me?
yeah
rocket_move
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?
do getcomponent for the rigidbody
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?
the rocket
Yupp.
Now look at your speed up class
What GameObject is GetComponent searching through to try and find Rocket_Move?
yeah I can already feel it
Ok so
what would the other choice even be tho
yes
You dragged a script onto your rocket GameObject yea?
yes
What script was that
rocket_move
Yupp. And what GameObejct did the GetComponent<> search through to find the Rigidbody?
the rocket
the trigger object that the rocket is supposed to collide with
Yupp. so what GameObject is the GetComponent<> as its used right now searching through ?
the trigger object
Actually whats the name
Forget about the trigger for now
Thats tripping you up
What GameObject is your speed up on?
"after_mercury" 💀 (it makes sense the the grand scheme of things)
we can call it "box" or something for simplicity
So, forgetting about the trigger stuff.
What GameObject is GetComponent<> searching through
after_mercury
rocket_move
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;
}
}
See that (Collider other)
Thats what is called a parameter
yeah
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
yeah hold on what
but you would type other
other.name
other.tag
other. information I want from this thing
I see
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
"other" is the rocket?
Yupp
ok ok
other could technically be anything btw that collides
yeah
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
Your not far off
the strcture is there
But I want to show you some stuff
if (somethings.name == "TheNameIWant")
Now you replace
with what you have
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
ok..
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
so would it be Collider.name ==
Nope, what did we say was the way to access the gameObject earlier
using what
It wasnt Collider
getcomponent
Nope
ok yeah
I remember this from when I was there
so other.name ==
^
alr
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
ok yeah that will probably help
also I have to ask quickly, will I ever be dealing with the Collider input?
You mean the first word in Collider other?
yeah
No. Thats a Type.
ok ok
yeah I keep forgetting
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")
}
}
}
You know how to make comments?
yeah //
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
ok thats a good idea
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
ok
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")
}
}
}
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
happens
im used to python
nothing happened lmao
Let me check the rigidbody settings and stuff I was messing with them
is only the rocket supposed to be a trigger
ok trying that again
it does
On the same object as the script
yeah
Ok
my only last guess is because the collision object is a plane maybe even though convex is checked
ok
yeah ill change it
ok wait so the rocket shouldn't have "is trigger" checked but the cube should
?
good idea
like this right
yupp
oops lod
yea besides that lol
bruh semicolon fkglwer
give it a week
trust me, you keep at it and writing code and you'll forget less and less
it didn't print anything
Ok so now we know its broken in general
Now you can screen shot both GameObject and their components
oh wait
im an idiot
it was working
I had one of these things turned off
broo 😭
ok its fine
lolol lessons being learnt
ok well thats good at least
So you have everything working lets move on to the FINALE
yes
Get rid of the first debug.log
and the second one too i guess
we know its working
ok
What do you need to do now?
I guess just how to make it change the speed value
ah shi
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?
the speed variable is stored in the rocket_move script, which is a component of the RocketPlayer object
Boommmmm
So how can you access an object when it hits your trigger?
your already doing this btw
by using other?
yupppppppp
or whatevers its named
ok im guessing from here its gonna be something along the lines of getcomponent of other
I have no idea how to format the getcomponent stuff
how did you access the name in the condition above?
Yupp, now test it out
ok
Seems like a amassive speed change but well see
it worked 🔥 🔥
Cool now lets take this just a bit further, theres one more thing I want to show you
yeah its supposed to go into an overdrive type of thing
ok sounds good
You never really want to use magic numbers like you did. aka directly setting something to a number
instead set it to a variable
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
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;
}
}
}
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
different values on anything you want
Can't even tell you how grateful I am lol