Unity C# Coding Practices #1, Destroy a cube into pieces

  Рет қаралды 69,592

LearnGameCreating

LearnGameCreating

Күн бұрын

In this video we will destroy cube into pieces and give it an explosion effect. We will use OnTriggerEnter, CreatePrimitive and AddExplosionForce.
Download Script:
drive.google.c...

Пікірлер: 189
@learngamecreating1308
@learngamecreating1308 6 жыл бұрын
Don't forget to open SUBTITLES for details. Leave a commend for your questions. Experts will help you. Let's learn together, help each other.
@natalieweishuhn521
@natalieweishuhn521 4 жыл бұрын
#youreWelcome using System.Collections; using System.Collections.Generic; using UnityEngine; public class Explosion : MonoBehaviour { public float cubeSize = 0.2f; public int cubesInRow = 5; float cubesPivotDistance; Vector3 cubesPivot; public float explosionForce = 50f; public float explosionRadius = 4f; public float explosionUpward = 0.4f; // Use this for initialization void Start() { //calculate pivot distance cubesPivotDistance = cubeSize * cubesInRow / 2; //use this value to create pivot vector) cubesPivot = new Vector3(cubesPivotDistance, cubesPivotDistance, cubesPivotDistance); } // Update is called once per frame void Update() { } private void OnTriggerEnter(Collider other) { if (other.gameObject.name == "Floor") { explode(); } } public void explode() { //make object disappear gameObject.SetActive(false); //loop 3 times to create 5x5x5 pieces in x,y,z coordinates for (int x = 0; x < cubesInRow; x++) { for (int y = 0; y < cubesInRow; y++) { for (int z = 0; z < cubesInRow; z++) { createPiece(x, y, z); } } } //get explosion position Vector3 explosionPos = transform.position; //get colliders in that position and radius Collider[] colliders = Physics.OverlapSphere(explosionPos, explosionRadius); //add explosion force to all colliders in that overlap sphere foreach (Collider hit in colliders) { //get rigidbody from collider object Rigidbody rb = hit.GetComponent(); if (rb != null) { //add explosion force to this body with given parameters rb.AddExplosionForce(explosionForce, transform.position, explosionRadius, explosionUpward); } } } void createPiece(int x, int y, int z) { //create piece GameObject piece; piece = GameObject.CreatePrimitive(PrimitiveType.Cube); //set piece position and scale piece.transform.position = transform.position + new Vector3(cubeSize * x, cubeSize * y, cubeSize * z) - cubesPivot; piece.transform.localScale = new Vector3(cubeSize, cubeSize, cubeSize); //add rigidbody and set mass piece.AddComponent(); piece.GetComponent().mass = cubeSize; } }
@stodgystarch4798
@stodgystarch4798 5 жыл бұрын
I don't know if this is already somewhere in the comments, but something this explosion script does is overpopulate the scene with new cubes, so here is a quick fix for it :) in the createPiece function, at the very bottom, insert this little bit of code for an automatic deletion of the pieces so you don't encounter lag : Destroy(piece, UnityEngine.Random.Range(0.5f, 5f)); What this does; it grabs every new piece you just created, destroys is in a randomized way, clearing your now *very* populated hierarchy in a way that all the tiny cubes don't go away at once, making it look a bit more natural. Hope that helps :D
@learngamecreating1308
@learngamecreating1308 5 жыл бұрын
Yes, there is a comment like that but random idea was awesome. well done my friend and thank you for sharing
@erjuanjojj
@erjuanjojj 2 жыл бұрын
only one video?? this is one of the best tutorial formats I've ever seen, direct and detailed, please do more :(
@kagigreenscreen
@kagigreenscreen 5 жыл бұрын
oh this is very very nice coding thank you for this many thanks and thanks for the download script etc makes old slow unity learner very happy :)
@youssefabdulaziz2133
@youssefabdulaziz2133 5 жыл бұрын
that's amazing,keep going,pal👍👍
@CustomClass5
@CustomClass5 5 жыл бұрын
This was an excellent tutorial.
@mohsinnaseer4127
@mohsinnaseer4127 4 жыл бұрын
explosionPos, explosionUpward, explosionRadius is not defined in video
@matanyamin1
@matanyamin1 2 жыл бұрын
Fantastic video! Very clear and straight forward. Thanks.
@GameDevNerd
@GameDevNerd 3 жыл бұрын
Clever little bit of code!
@tonysang3660
@tonysang3660 4 жыл бұрын
exactly what i'm looking for thank you so much!
@oscarkiloproductions5860
@oscarkiloproductions5860 5 жыл бұрын
Hello! I was just wandering if sometime you could create a tutorial on how to make a grenade that when it explodes, it leaves a crater. It would be great for my new game!
@casper2277
@casper2277 4 жыл бұрын
Thanks! The tutorial was easy to follow and the explosion looks amazing👍🏻
@anix7533
@anix7533 4 жыл бұрын
Can you help me ? Unity says that there is a compiling error with the script but VS code doesn't find any problem
@Zarakikov
@Zarakikov 4 жыл бұрын
Very nice, thank you!
@brunosallesdev
@brunosallesdev 3 жыл бұрын
Great technique!
@d-bi5960
@d-bi5960 3 жыл бұрын
Thank you for a very good script))
@acanaldelbuencontenido
@acanaldelbuencontenido 3 жыл бұрын
Amazing video, thanks.
@rahulkukreti6491
@rahulkukreti6491 5 жыл бұрын
Excellent, Thanks alot :)
@xilongzhang6856
@xilongzhang6856 3 жыл бұрын
Could anyone tell me how to define explosionRadius, explosionForce and explosionUpward? 😭 I don’t know what are they, and they are not defined...
@xilongzhang6856
@xilongzhang6856 3 жыл бұрын
ohh for anyone that is wondering. I found it in the file linked in description. He skipped a few code in the video but you can find it when you download the code x
@itamarashofiniaw.a.6394
@itamarashofiniaw.a.6394 2 жыл бұрын
@@xilongzhang6856 Thanks a lot!
@Alex-tn7pv
@Alex-tn7pv 3 жыл бұрын
Great stuff!!!!
@mohamedahmedahmedfathi9382
@mohamedahmedahmedfathi9382 2 жыл бұрын
This is an amazing script thank you very much
@batuhanartan
@batuhanartan 5 жыл бұрын
Great Tutorial .. .thank you very much :)
@melom.
@melom. 4 жыл бұрын
What about the cutted objects (explosion radius, explosion force and explosion Upward?)
@Daviality
@Daviality Жыл бұрын
Very goods script! I modified it so I can use this on thin cards
@ЕвгенийШиряев-г5х
@ЕвгенийШиряев-г5х 5 жыл бұрын
Thanks a lot, very helpful
@ManaBlend
@ManaBlend 3 жыл бұрын
Thank you! So much verygood script🎁
@Aliozka
@Aliozka 5 жыл бұрын
Hey great tutorial thanks.. How do you change the colour of the cubes after the blast.. Thank you..
@learngamecreating1308
@learngamecreating1308 4 жыл бұрын
This is the most asked question :) read comments
@Aliozka
@Aliozka 4 жыл бұрын
@@learngamecreating1308 i manage to do it 👍😅
@learngamecreating1308
@learngamecreating1308 4 жыл бұрын
@@Aliozka Perfect, sorry for late answer
@Aliozka
@Aliozka 4 жыл бұрын
@@learngamecreating1308 no worries mate👍ones again great tutorial thank you 👌🏼👏
@Adul1k
@Adul1k 4 жыл бұрын
@@Aliozka how did you do it please :-)
@tim6873
@tim6873 6 жыл бұрын
Amazing work! Can even work with "Bullets" !! thank you! Make more videos please and thank you
@deadly_artist
@deadly_artist 6 жыл бұрын
thank u u broke my unity
@learngamecreating1308
@learngamecreating1308 6 жыл бұрын
Did you fix your unity? can we help you?
@deadly_artist
@deadly_artist 6 жыл бұрын
I did, always breaks whenever I run that program though
@veggiearchon5778
@veggiearchon5778 4 жыл бұрын
thank you man it worked perfectly :D
@jontyblair8034
@jontyblair8034 5 жыл бұрын
Very nice thanks man👌
@learngamecreating1308
@learngamecreating1308 5 жыл бұрын
Follow for more, new tutorials coming
@omercenikli2654
@omercenikli2654 4 жыл бұрын
Thank you for video. Nice job
@wariskbr
@wariskbr 5 жыл бұрын
Very Helpful, Appreciate it
@anix7533
@anix7533 4 жыл бұрын
Thank you so much for this video!
@L33T_Taco
@L33T_Taco 5 жыл бұрын
Thanks for the tutorial @LearnGameCreating quick question thought what can i do to the code to make the cube explode outward in a specific direction? like say if i applied this to a moving object, which i have done successfully, how would i make the explosion throw the smaller cubes forward in the direction its going.
@learngamecreating1308
@learngamecreating1308 4 жыл бұрын
use smaller mass, and let engine do it for you
@TheShiftingSounds
@TheShiftingSounds 6 жыл бұрын
How come anyone dislike! Amazing work a d thanks. Will work on this tomorrow.
@learngamecreating1308
@learngamecreating1308 6 жыл бұрын
thank you
@funkupgamer5364
@funkupgamer5364 5 жыл бұрын
Awesome...!!
@vilanstrikegaming5114
@vilanstrikegaming5114 3 жыл бұрын
Does it work for spheres
@rinodipaola761
@rinodipaola761 4 жыл бұрын
Congratulations! Thank you
@starchaser28
@starchaser28 5 жыл бұрын
This is awesome! Is there a way to define another model that results from the explosion rather than smaller cubes? Like butterflies. :)
@learngamecreating1308
@learngamecreating1308 5 жыл бұрын
Yes, you can do it, //add prefab variable top of script public GameObject piecePrefab; //old code (create cubes) piece = GameObject.CreatePrimitive(PrimitiveType.Cube); //new code (create any object) piece = Instantiate(piecePrefab, new Vector3(0, 0, 0), Quaternion.identity); //you can set position when you create or, after create like we did in script //don't forget to drag&drop your butterfly prefab to scripts "piecePrefab" variable in editor. good luck 🤞
@stevendeprez5392
@stevendeprez5392 4 жыл бұрын
came for the tutorial stayed for the music
@learngamecreating1308
@learngamecreating1308 4 жыл бұрын
Perfect :)
@XONRk
@XONRk 4 жыл бұрын
I was looking for this for a million years Just kidding 😁
@umeshy
@umeshy 6 жыл бұрын
Good tutorial! thanks
@atakanbacak8281
@atakanbacak8281 Жыл бұрын
for everyone public float explosionForce = 50f; public float explosionRadius = 4f; public float explosionUpward = 0.4f;
@chris.s.m.r4989
@chris.s.m.r4989 Жыл бұрын
you are mi savior
@loganjones7872
@loganjones7872 5 жыл бұрын
Friend, great video. However, can you help me understand why you must subtract pivot distance and what is it exactly.
@learngamecreating1308
@learngamecreating1308 5 жыл бұрын
Because main cubes pivot is center of object, if we create pieces over this pivot point (we adding positions to object position (center point)) so pieces was moving side, if you check there in video again, you will notice, that's why we calculate half of the cube, and subtract that position from pieces, then new created cubes same position as old destroyed cube. I hope it's clear now, good luck, best wishes
@urospocek4668
@urospocek4668 6 жыл бұрын
You didn’t set explosion Force, Radius and Upward but you have use them!?? How to define them to work?????????????
@茨城童子-n1i
@茨城童子-n1i 6 жыл бұрын
Look at 4:48. explosion Force, Radius and Upward are public variable
@dimitrioskoulialis6117
@dimitrioskoulialis6117 5 жыл бұрын
Thanks for the tutorial really helpful! What is the name of the song in the background?
@learngamecreating1308
@learngamecreating1308 5 жыл бұрын
Nice to see it helps you, music : Exotics - Kevin MacLeod
@MadhanLeMan
@MadhanLeMan 6 жыл бұрын
Thanks!. And can we add a custom GameObject instead of using cubes. I tried adding a public GameObject piece and it didn't work any ideas??
@learngamecreating1308
@learngamecreating1308 5 жыл бұрын
Yes, you can do it, //remove this line piece = GameObject.CreatePrimitive(PrimitiveType.Cube); //with this piece = Instantiate(piecePrefab); //so we will Instantiate a custom prefab, not Cube primitive. //And declare a prefab variable at the beginning of the script public GameObject piecePrefab; //then drag&drop your prefab in this variable in the editor
@bulletsponge6920
@bulletsponge6920 5 жыл бұрын
How could I adjust the code to have the size adjust to the cube size? For example I have three cubes with different x,y, and z, lengths, but when hit, it all makes the same explosion. I like the sizes of the pieces themselves, I just want the pieces to fill the bigger or wider or taller cubes
@learngamecreating1308
@learngamecreating1308 4 жыл бұрын
I'm gonna make another video about random explosion effect
@djukicdev
@djukicdev 5 жыл бұрын
Thanks dude.
@learngamecreating1308
@learngamecreating1308 5 жыл бұрын
Follow for more, new tutorials coming
@unamattina6023
@unamattina6023 4 жыл бұрын
where did explosionRadius came from?
@xilongzhang6856
@xilongzhang6856 3 жыл бұрын
I want to ask this as well. Where do we get that
@learngamecreating1308
@learngamecreating1308 3 жыл бұрын
I Forgot to put it in the video, check scipt, it's just a variable, public float explosionForce = 50f; public float explosionRadius = 4f; public float explosionUpward = 0.4f;
@frkngz
@frkngz 6 жыл бұрын
teşşekkürler çok hoş olmuş.
@StackBrains
@StackBrains 4 жыл бұрын
DESTROY CUBE INTO PIECES, THIS IS MY LAST RESPAAAWN
@TruuAmbition
@TruuAmbition Жыл бұрын
Anyone have tips on how to create the pieces from a pool of objects as to make the script more performant ?
@PixelTB
@PixelTB 6 жыл бұрын
I need help, so I have a cube that is a player and it has movement scripts and all that stuff, and there are obstacles and I want when I hit an obstacle my player cube to explode
@learngamecreating1308
@learngamecreating1308 5 жыл бұрын
You can trigger an explosion when the collision occurs. Put this script to your player object, and check when to explode OnTriggerEnter function, as we did in example (we checked if it is the floor)
@iskandermx
@iskandermx 6 жыл бұрын
Hi! Good work, how can change explotion no only color, texture too. Second question, i make a car and wanna exploition when car touch, how can make this? Best Regards.
@learngamecreating1308
@learngamecreating1308 5 жыл бұрын
//declare texture variable public Texture m_MainTexture; //put these lines to createPiece function material = piece.GetComponent().material; material.SetTexture("_MainTex", m_MainTexture);
@jroy7408
@jroy7408 6 жыл бұрын
Great tuto, thanks! I was wondering whether someone knew how to give pieces the same colour as the main cube??
@learngamecreating1308
@learngamecreating1308 6 жыл бұрын
you can set cubes material color with this lines. //put these lines to createPiece function material = piece.GetComponent().material; material.color=Color.red; //Color.red or you can send a value as parameter, same as main cube's material.color
@anix7533
@anix7533 4 жыл бұрын
Hey Unity says I should fix some compile errors and that I cant use it bcs of this , but in VS there dont seem to be any errors
@MrsAgathe
@MrsAgathe 5 жыл бұрын
Hello, I want my object to explode when I Press "E" for example, instead of Colliding with another object. Can you help me?
@AlexJDev
@AlexJDev 5 жыл бұрын
In the update function, call a getkey function and have explode() in that
@aborayan5268
@aborayan5268 6 жыл бұрын
awesome thank you
@damirshabayev344
@damirshabayev344 4 жыл бұрын
cool tutorial works too
@anix7533
@anix7533 4 жыл бұрын
Really? Can you help me , Unity says that there is a compile error while there isn't any ( I even tried copy pasting his code and changing the name of the things that needed to be changed )
@linkspecialproximity
@linkspecialproximity 5 жыл бұрын
In my project i'm using OnCollisionEnter instead of OnTriggerEnter. I'm new to this stuff and i'm having a hard time figuring out how i am going to explode my cube when i come in contact with an obstacle. i am doing the same as this Pixel guy: "I need help, so I have a cube that is a player and it has movement scripts and all that stuff, and there are obstacles and I want when I hit an obstacle my player cube to explode". There's a player movement and collision script. when i just change "floor" into "Obstacle" it does not work and when i make the obstacles triggers they just fall through the floor haha. Any ideas?
@saif_khan_gamers
@saif_khan_gamers 5 жыл бұрын
Change the tag of the object tht is colliding!
@aleprivi4575
@aleprivi4575 4 жыл бұрын
Thank you very much for helping! but I have a problem can you help me? how do I change the color of the cubes that are generated? and how do I get rid of them after some time
@learngamecreating1308
@learngamecreating1308 4 жыл бұрын
Please read other comments, you will find the answer, thank you
@sisitech
@sisitech 3 жыл бұрын
I dont know if im gonna get any answer on this 3 years after the upload but I cna always try I used this on a prefab but it doesnt look good if a brown crate explodes into white cubes so is it possible to let it create/clone a prefab instead of a cube //create piece GameObject piece; piece = GameObject.CreatePrimitive(PrimitiveType.Cube);
@jung-zen
@jung-zen 3 жыл бұрын
Here's a comment left by LearnGameCreating (The creator of this video) replying to somebody with a similar issue: Yes, you can do it, //add prefab variable top of script public GameObject piecePrefab; //old code (create cubes) piece = GameObject.CreatePrimitive(PrimitiveType.Cube); //new code (create any object) piece = Instantiate(piecePrefab, new Vector3(0, 0, 0), Quaternion.identity); //you can set position when you create or, after create like we did in script //don't forget to drag&drop your butterfly prefab to scripts "piecePrefab" variable in editor. good luck 🤞
@jung-zen
@jung-zen 3 жыл бұрын
I'm literally trying this right now, if you could let me know how you get on it would be much appreciated!:) Currently have an issue where the prefabs fall through the floor despite having set the colliders etc...
@sisitech
@sisitech 3 жыл бұрын
@@jung-zen I think I know why it goes trough I just had the same object but then I used the Prefab from my asset folder instead of from my hierarchy and now it works, only problem I have now is that it explodes very un realistic, It explodes and stays in a cube of small cubes
@jung-zen
@jung-zen 3 жыл бұрын
@@sisitech Thanks for the reply, I’ll take a look at this ASAP and get back to you. Mine also faces the same issue
@jung-zen
@jung-zen 3 жыл бұрын
@@sisitech Try bumping up the explosion force, by that I mean try 3000 or higher.
@coolride1401
@coolride1401 4 жыл бұрын
can you make a video of cutting objects?
@learngamecreating1308
@learngamecreating1308 4 жыл бұрын
For sure
@refikugur212
@refikugur212 5 жыл бұрын
How can i explode like 10 1 1 cube rectangle one, where collision happens? like from corner
@Minebender9
@Minebender9 4 жыл бұрын
Hey, just wondering how to make the pieces have no gravity, and also make the color orange, (none of the comments worked for me). Someone PLS help. Thanks!
@thefunny_69
@thefunny_69 4 жыл бұрын
Minebender9 watch a youtuber named brackeys and look at his how to make a game tutorial and it will teach you everything you need to know
@headshotgamer7422
@headshotgamer7422 3 жыл бұрын
It works m, thanks
@arnaudb4357
@arnaudb4357 6 жыл бұрын
when i use this script all obects in my scene fly in the air thats fun haha i dont understand why but now i seriously need it triggered on explosions is it an easy thing to do? (sorry french guy speaking english)
@learngamecreating1308
@learngamecreating1308 6 жыл бұрын
Your english flows :) But i can't understand "triggered on explosions" You can trigger it when ever you want, i used trigger collision, you can explode it from your code when ever you want
@arnaudb4357
@arnaudb4357 6 жыл бұрын
@@learngamecreating1308 I would like to trigger the explosion of gameobjects that are props or walls with a shock wave or a blast. I tried to give the particles several physical properties but the only way it works is with the contact between polygons. I would blowup a max of objects on my map now hahaha ^^ I adapted my script with the different names of particles that I use in my scene but without result :( (im a beginner in C# i use C usually so its hard to adapt even if its similar in lot of ways)
@learngamecreating1308
@learngamecreating1308 6 жыл бұрын
@@arnaudb4357 Ok, you want to filter rigidbodies in AddExplosionForce, cause your all objects explode. Vector3 explosionPos = transform.position; Collider[] colliders = Physics.OverlapSphere(explosionPos, explosionRadius); foreach (Collider hit in colliders) { //check colliders gameobject layer or tag //ofcourse don't forget to set your walls or props tags to 'walls' or whatever you want if (hit.gameObject.tag=="walls") { Rigidbody body = hit.GetComponent(); if (body!=null) { body.AddExplosionForce(explosionForce, explosionPos, explosionRadius, explosionUpward); } } }
@arnaudb4357
@arnaudb4357 6 жыл бұрын
@@learngamecreating1308 You are very helpful! Merci beaucoup! Im gonna try to understand C# better but i may will be back with more questions hehh Usually i make destroyable objects in blender but it's a long process to make them usable and not too greedy on the performance.
@learngamecreating1308
@learngamecreating1308 6 жыл бұрын
@@arnaudb4357 Your welcome my friend, i will make more videos, follow
@xinyueyang1962
@xinyueyang1962 4 жыл бұрын
BGM is outstanding
@Thesupperals
@Thesupperals 2 жыл бұрын
This is not the best practice. Sure, diagnosing so that code is functioning correctly is a good practice but what you are doing can be achieved with a coroutine, using addressables, not getting components, not adding a rigidbody and not using physics at all. You should think about using a particle system that utilizes an animation trim sheet while things are in air. As soon as they collide and die, you can instantiate an object in place of the dead particle and if you must- add a mesh collider to that instantiated object. On top of all that, the last thing to do is add another coroutine to destroy the object if it commonly occurs. This is best practice. Heck, don't get me started on Enums, Dictionaries, object pooling, Data Abstraction and Scriptable Objects.
@ecel_style
@ecel_style 2 жыл бұрын
I think you should make educational videos too :)
@gustavjakobsen4160
@gustavjakobsen4160 6 жыл бұрын
Is there a way to have the cubes destroy themselves after they hit the ground?
@learngamecreating1308
@learngamecreating1308 6 жыл бұрын
If you want to check collision with floor it would be more complicated, but an easy way i can suggest you to use Destroy command. //just put this line to end of createPiece function Destroy(piece, 3); //3 means after 3 seconds, remove object from scene. You can tweak it as you wish :)
@luiscarloscajas4823
@luiscarloscajas4823 6 жыл бұрын
cool ass video bro
@zaraali2169
@zaraali2169 3 жыл бұрын
works! thx!
@breakfastboi3344
@breakfastboi3344 6 жыл бұрын
When ur hp is at 0
@teetehi
@teetehi 5 жыл бұрын
Can this code be used on any object for example a tree if I click it it exploded if I make the correct modification to the code and if so can you tell me what to change?
@learngamecreating1308
@learngamecreating1308 5 жыл бұрын
A lot of questions about that, i will post a new tutorial about custom object exploding. But if i should answer, Yes, you can do it, You need small stick mesh objects. //add prefab variable top of script public GameObject piecePrefab; //old code (create cubes) piece = GameObject.CreatePrimitive(PrimitiveType.Cube); //new code (create any object) piece = Instantiate(piecePrefab, new Vector3(0, 0, 0), Quaternion.identity); //you can set position when you create or, after create like we did in script //don't forget to drag&drop your stick prefab to scripts "piecePrefab" variable in editor. good luck 🤞
@upperarmhumor
@upperarmhumor 4 жыл бұрын
I used this technique as a crash effect in a test game I made connect.unity.com/mg/other/block-juke
@skodaskoda6633
@skodaskoda6633 6 жыл бұрын
how to choose color for explosion objects?
@learngamecreating1308
@learngamecreating1308 6 жыл бұрын
You can set color in createPiece() function, add this line; piece.GetComponent().material.color = new Color(255,0,0); //RGB red
@skodaskoda6633
@skodaskoda6633 6 жыл бұрын
Thank you Bro!!!
@kristianulrych6094
@kristianulrych6094 4 жыл бұрын
Is there a way how to make each different part of exploded cube colored?
@dubdub9747
@dubdub9747 4 жыл бұрын
Yes, when creating the cubes, you can access the Renderer for that cube and assign a material. Now you've just got to create the materials you want to use. There are a few ways of doing it, but I'd suggest creating a 'System.Random' object (which will generate random numbers, part of .NET library). You then create a new material, and set the color of the material to a new 'Color' object. In the constructor, generate three values between 0-1 for the new color. Ends up looking something like this: piece.GetComponent().material.color = new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble()); Note: When creating the 'System.Random' object, make sure it's not inside the 'CreatePiece' method, it should be at the top of your script as a private variable, you can initialise it there as well.
@elnurdamirov556
@elnurdamirov556 6 жыл бұрын
Hello bro.How can i change tag of the created pieces through script
@learngamecreating1308
@learngamecreating1308 5 жыл бұрын
Declare the tag in the tag manager, then //put this in createPiece function piece.tag = "TagName";
@aryan.puthran
@aryan.puthran 5 жыл бұрын
How do you make it so when you hit a trigger further away, you can see the cube fall and break?
@blastback27
@blastback27 5 жыл бұрын
one intuitive way is that you can make a sphere and put it part of the "exploding object" so a child of it and make that sphere as large as the range of where you want the trigger to start, uncheck the sphere meshes (so you cant see it) and make that sphere's collider a trigger and now do the same thing that he did, is just that within the code of the sphere make a trigger code like we see at the earlier part of the video when it says "onTriggerEnter" make that function for the sphere and make another script for the object that is exploding as he did, all you need to say in the code for the sphere is that when it is triggered, then call the function in the "exploding-object" code that is separate, to do so you just type FindObjectOfType()." the methods name"(); and this will activate the function within the code of the "exploding-object" code. not in quotation ofc. And if you wanna know what function he called check the same part and try to figure the rest out, which should be self-explanatory.
@blastback27
@blastback27 5 жыл бұрын
Or use if (Physics.CheckSphere(transform.position, sphereRaduis, LayerID)) when it returns true then it will run the code you put in, which is the same one as the onTriggerEnter stuff..
@aryan.puthran
@aryan.puthran 5 жыл бұрын
Ok! Thank you!
@equinox1346
@equinox1346 5 жыл бұрын
How can i change the colors of the pieces?
@aborayan5268
@aborayan5268 6 жыл бұрын
How to destroy after a period?
@samduartec
@samduartec 6 жыл бұрын
@Amine Kh that doesnt work because the object with the script is no longer active
@alphaspace1100
@alphaspace1100 4 жыл бұрын
thx
@bittublastgamer202
@bittublastgamer202 2 жыл бұрын
can you help my bosx is not exploding
@atakanbacak8281
@atakanbacak8281 Жыл бұрын
you forgot public float explosionForce = 50f; public float explosionRadius = 4f; public float explosionUpward = 0.4f;
@mrcryptographic4803
@mrcryptographic4803 4 жыл бұрын
how can i have the piece destroyed after a certain time in this script?
@thewildreamer
@thewildreamer 4 жыл бұрын
you can use Invoke("functionname",time by f); for example : Invoke("explosion",2f);
@mrcryptographic4803
@mrcryptographic4803 4 жыл бұрын
@@thewildreamer i mean how can i destroy the piece after a time and not how can i spawn the explosion after a time. or did I not understand something?
@The_Mavrik
@The_Mavrik 5 жыл бұрын
Классный урок. Но почему у тебя всего 1 видео на канале???
@learngamecreating1308
@learngamecreating1308 5 жыл бұрын
Thank you, new content will be coming soon.
@Adul1k
@Adul1k 4 жыл бұрын
how do I change material of those small cubes ?
@dubdub9747
@dubdub9747 4 жыл бұрын
Get the 'Renderer' component for the cube, then select the material propery, and assign it to the desired material (for example, if you want it to be the same as the original cube, you want (gameObject.getComponent().material)
@aniketmeghani1627
@aniketmeghani1627 5 жыл бұрын
i learn about make game how can i start???
@learngamecreating1308
@learngamecreating1308 5 жыл бұрын
You started already, keep going!
@adamhilali806
@adamhilali806 4 жыл бұрын
why it doesn't work it really help me sure but it didn't work
@mateus10pras14
@mateus10pras14 4 жыл бұрын
My Unity crashed at 02:22 and my computer is a i5 8400 + rtx 2070 super + 16 gb ram
@kumelogames
@kumelogames 6 жыл бұрын
how do you change color
@learngamecreating1308
@learngamecreating1308 6 жыл бұрын
//put these lines to createPiece function material = piece.GetComponent().material; material.color=Color.red;
@djukicdev
@djukicdev 5 жыл бұрын
​@skipsbiceps assign public Material red; , then just use it wherever you need as piece.GetComponent().material = red; Dont forget to link mat in inspector tho
@Adul1k
@Adul1k 4 жыл бұрын
How can I destroy those cubes after like 2 seconds ?
@MamMam-zo1sm
@MamMam-zo1sm 4 жыл бұрын
Destroy(Gameobject , time);
@LEARN-UNITY3D
@LEARN-UNITY3D Жыл бұрын
Wow its working ! But i want to k ow how to destroy these cube pieces bcos it makes pollution in my game! Please if u can solve this
@manuelabarcacrespo8298
@manuelabarcacrespo8298 Жыл бұрын
Destroy(cube, time);
@mohsinnaseer4127
@mohsinnaseer4127 4 жыл бұрын
oh i copied from drive script by the way thanks for the video
@factsspace8326
@factsspace8326 6 жыл бұрын
You can slow down your speed and explain the code to us it would be so nice
@learngamecreating1308
@learngamecreating1308 6 жыл бұрын
thank you for your feedback. i explain everything in subtitles. Did you try it? I hope you like it.
@bonniedunlap2399
@bonniedunlap2399 7 жыл бұрын
Bonnie Dunlap
@hanssarpei8687
@hanssarpei8687 6 жыл бұрын
How can I destroy this pieces?
@larcondos909
@larcondos909 6 жыл бұрын
In the section of code at the end of "createPiece", you can add another script that destroys those pieces after some condition (Say, 2 seconds, or on touch)
@learngamecreating1308
@learngamecreating1308 6 жыл бұрын
as Larcondos mention above; //just put this line to end of createPiece function Destroy(piece, 3); //3 means after 3 seconds, remove object from scene.
@afkfish4368
@afkfish4368 4 жыл бұрын
it is not working like the code it say that i need to fix it
@KamikazeSensei
@KamikazeSensei 4 жыл бұрын
try changing the name for your script to "Explosion"
@oppenheimeroficial
@oppenheimeroficial 4 жыл бұрын
Same... function turns red... feels like he's just making the functions up
@아이디없음-t1b
@아이디없음-t1b 4 жыл бұрын
Script is not working
Using Interfaces in Unity Effectively | Unity Clean Code
4:23
James Makes Games
Рет қаралды 65 М.
Creating a Flight Simulator in Unity3D (Part 1)
21:33
Vazgriz
Рет қаралды 93 М.
How Strong Is Tape?
00:24
Stokes Twins
Рет қаралды 96 МЛН
She made herself an ear of corn from his marmalade candies🌽🌽🌽
00:38
Valja & Maxim Family
Рет қаралды 18 МЛН
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
I Made the Same Game in 8 Engines
12:34
Emeral
Рет қаралды 4,4 МЛН
4 Unity Destruction Libraries Compared - Which is best for you?
18:45
Explosions in Unity! Let's blow some stuff up ;)
6:00
Tarodev
Рет қаралды 46 М.
I coded a Geometry Dash RIPOFF GAME!
18:11
Tride
Рет қаралды 240 М.
Awesome Easy DESTRUCTION in Unity! (Add SECRETS!)
12:38
Code Monkey
Рет қаралды 67 М.
How to Explode a Cube | Unity Tutorial
2:24
Thomas Friday
Рет қаралды 12 М.
Can I 100% Superliminal and Get a Refund?
23:36
Gronf
Рет қаралды 407 М.
A Great Way To Setup POWERUPS In Your Unity Game
13:43
He said I Couldn't Make a 3D Game... So I Made One!
10:29
Dani
Рет қаралды 9 МЛН
Spare Your Pain Using UNITY EVENTS Tutorial
7:24
BMo
Рет қаралды 27 М.
How Strong Is Tape?
00:24
Stokes Twins
Рет қаралды 96 МЛН