All we need to do is to get our placeholder prefab, then instantiate this prefab in our scene, then update its position to our current mouse position. Now that we have our mouse position, let’s start displaying our placeholder building in our game.ĭoing this is super easy. Save the script and if you run the game once again, you should be able to see that the game is running much smoother than the previous run. If(_previousX != positionX || _previousZ != positionZ)ĭebug.Log(_previousX + " / " + _previousZ) So with that being said, head back to your script and update its code to the following: using System.Collections Therefore, it will stop checking and getting the position again and again which will save us memory. Then, we should check if the current mouse position is not equal to the previous mouse position. We can fix this by assigning our current mouse position into a variable. This is because we are getting and checking our mouse position in every frame per second which is really unnecessary. However, you may have noticed that our game becomes a bit slow. Run the game and you should be able to see in the console our current mouse position. Select the Main Camera and apply the script that we just created. If it does, then we get its point position both horizontal and forward axes. Then, we created a RaycastHit and check if it’s hitting something. To explain the written code above, all we did first is we created a Vector3 variable then inside the Update() function we used this variable to get the current position of our mouse which we’ll use to create a ray. If(Physics.Raycast(ray, out hit, Mathf.Infinity))ĭebug.Log(positionX + " / " + positionZ) Public class RTSBuildingSystem : MonoBehaviour Go ahead and create a new C# script and name it RTSBuildingSystem.cs and copy the following code. Now that we’re done setting up our game objects, let’s proceed by creating the script for our building system. Now, the next thing that we’re going to do is to use both of these prefabs into our scene. However, if you have more than one buildings then feel free to do the same process to the rest of your buildings. The placeholder’s trigger is set to true and the actual building is set to false.Īwesome! Now we’re done with the building and its placeholder. The difference between these two prefabs is their Box Collider‘s trigger value. Otherwise, what you change to the Placeholder will also overwrite the Building prefab. If you don’t know how to create a prefab then all you need to do is to drag the game object from the hierarchy to your project window.įor creating the prefab, make sure you create it as original and not as a variant. One is for the placeholder and the second is for the actual building. Next, we need to save this transform into two separate prefabs. This will help us later on if this game object is colliding with other game objects and so make sure your environment (especially the trees, rocks, and other game objects) have colliders. Then, with that building game object selected, navigate to its inspector and add a Box Collider component and set its trigger on. Once you have your assets imported, go ahead and place them into your scene like below. If you don’t have buildings, we suggest looking at the asset store as there are hundreds of free assets out there.įor this tutorial, we’ve used the free assets like Low Poly Environment Pack and the Low Poly Fantasy House. All we need is a platform where our buildings will stand and of course, the buildings that we’re going to place on the platform. We’ll start this tutorial by creating a very simple scene like below.ĭon’t worry, you don’t have to copy the exact scene.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |