![]() ![]() You can then edit that variable in the Inspector instead of having to re-code a value into the script every time you have to change it. ![]() allows for a private variable to be accessed in the Inspector. – This is not necessarily needed for this example, however, most variables should be private to restrict unwanted access from other classes that may be programmed by mistake to access certain variables. public class ProjectilePlayer: MonoBehavior First we will make a reference to the projectile prefab. Open the script that is attached to the cube. In this example, when the Spacebar is pressed by the user, the sphere projectile will spawn at the player and shoot upwards in the scene. After creating the prefab, delete the sphere from the Hierarchy Tab so that it is no longer in the scene. Again, you can make a Prefabs folder in the Assets folder to store all the prefabs you have in your game. So drag your sphere projectile from the Hierarchy Tab into the Project Tab. You should also create a Prefab folder to hold your prefabs. ![]() Then drag it into the Assets folder of the Project Tab. To create a prefab, add a game object to the Hierarchy Tab. You can call them into the scene as needed. Their components and data are stored as an asset. Prefabs are basically game objects that are reusable. To be able to instantiate the Projectile game object into the scene, you have to make it a prefab. Scaling the game object isn’t necessary for a projectile, but it makes things easier to see in the scene. Make sure the sphere is selected and go to ‘Scale’ in the Transform component of the Inspector. Right click in the Hierarchy tab again, select 3D Object > Sphere. Note that you don’t need this cube to make a projectile, but in this example it will be used as for where the projectile will come from. The cube will be where the projectile is instantiated. With the Cube selected, go to the inspector and select Add Component at the bottom, then search for Script and select New Script. Right click in the Hierarchy tab, probably on the left, and select 3D Object > Cube. This will be a simple example of how to fire a projectile in Unity. Use “transform.Translate()” in the Update() function to move the projectile after it is instantiated.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |