Now when you press play and hit the space bar bullets should fly(slowly) up to the right of the screen. The values save automatically and we can return out of the prefab by clicking the < in the hierarchy view. Do the same for the BulletSprite just in case. Now we can change the BulletObject’s transform position to 0, 0, 0. Open up the bullet prefab by selecting it in our project window and selecting Open Prefab in the Inspector window. Nested prefabs unity 2019 full#Full Unity 2D Game Tutorial 2019 – Editing A Prefab If the bullet is appearing from an odd position(maybe to the left of the player) This is because we moved the BulletObject to see it in our scene view. Now if we save everything and press play in Unity we should be able to fire a bullet once per key press. We pass it the bullet prefab(the object to create) and the PlayerObject’s transform(the place to create it). Instantiate is used to create a new GameObject in the world. If we used GetKey we would fire every frame while the space bar is pressed down. We have used Input.GetKeyDown to check if the player pressed space. We added a public GameObject bulletPrefab to store our prefab in, we will need to add this is the inspector by dragging the prefab from our prefab folder to the input box on our PlayerObject This isn’t what we aim to have in our finished product but we want some way to test the bullets first. Nested prefabs unity 2019 update#Okay, let’s update our player so that they shoot when we press space. Done! you now have a prefab which can be used to make many bullets. Then simple drag the BulletObject from the hierarchy view into the prefabs folder. Bullets which we will be creating a lot of, is an ideal object to turn into a prefab. It is basically as object you have already created with all its settings set, and then saved in the project assets folder for you to use again later. You should now have a BulletObject with components like the image below:įull Unity 2D Game Tutorial 2019 – Creating a prefabĪ prefab is a pre-fabricated object. We need to destroy them or eventually we will have millions of bullets lagging up the game. Finally we have an Update method which decreases lifetime until it fallse below zero and then uses the built in Unity function Destroy to destroy the BulletObject. Again in the start we find our RigidBody2D except this time we don’t store it because once we add force to it we never use it again. These are the data we will need in order to move the bullet and eventually damage enemies. Nested prefabs unity 2019 code#In this section of code we added some public variables speed, targetVector, lifetime and damage. On the BulletObject add a component and create a new script called Bullet and add the following code: Full Unity 2D Game Tutorial 2019 – Bullet Code The bullet will be using physics so lets add a RigidBody2D to the BulletObject and set the gravity to zero and the Interpolate to Interpolate. You can also disable the Camera icon and other gizmo icons with the gizmo toggle highlighted in the image below. Then use the move tool to move the BulletObject to the left. If you can’t see the bullet on the screen make sure the BulletObject and sprite transforms are both set with the z position to zero. Now we get to use the sorting layer we created earlier in the tutorial. The image should appear half the size of our player. This time we will set the transform scale for the sprite image to 0.5, 0.5, 1. Add a sprite and set the sprite image to the Knob image again. Full Unity 2D Game Tutorial 2019 – Creating The BulletĬreate an empty to hold all our bullet parts and name it BulletObject. In this section of the tutorial we will look into creating prefabs (prefabricated objects) in order to fire bullets from our player. Full Unity 2D Game Tutorial 2019 – Firing Bullets
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |