Hey Guys, I am making an RTS game and I am trying to snap my Ghost prefab building during run-time after I instantiate it using the UI button, and I want to use the Mathf Round function. However, for past month, I have tried many functions to get it to work, including the Mathf Floor, but the Ghost prefab does not snap to grid for some reason, so I removed the function. Here is my script, if anyone can help that would be nice. Thank you.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;
public class ObjectPlacement : MonoBehaviour {
public GameObject BuildingPrefab; //Actual building. This will be placed from another script called 'BuildingSafe'.
public GameObject GhostBuildingPrefab; //Ghost prefab which will be instantiated after clicking the UI button.
private Transform currentBuilding;
public float RotateSelf = 3f; // for Rotating the building during runtime before placement.
private Transform transform;
private BuildingSafe safeBuilding;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Ray ray = Camera.main.ScreenPointToRay (new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z));
RaycastHit hit = new RaycastHit();
if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
hit.point = new Vector3(hit.point.x, hit.point.y, hit.point.z);
currentBuilding.transform.position = hit.point;
}
if (Input.GetKeyDown(KeyCode.R)){
currentBuilding.transform.Rotate(Vector3.back, RotateSelf, Space.Self);
}
if (Input.GetKeyDown(KeyCode.Q)){
currentBuilding.transform.Rotate(Vector3.forward, RotateSelf, Space.Self);
}
}
//For instantiating a Ghost prefab after clicking the UI button.
public void CreateBuilding() {
currentBuilding = GameObject.Instantiate(GhostBuildingPrefab).transform;
safeBuilding = currentBuilding.GetComponent<BuildingSafe> (); //The Ghost prefab will check whether the building is safe to place or not before clicking.
safeBuilding.BuildingPrefab = BuildingPrefab;
}
}
How are you actually snapping this to the grid? In order for the prefab to snap it must have an object of something to snap to unless there’s a function somewhere I’m missing and you’re trying to use something already built into Unity.
Edit: Just read your code a bit more carefully and see that you’re attaching it to another building, my bad. I see you’ve not got any Debug.Log within your transform code, so can you try putting it in to see if there’s a single line of code being a problem or something else?
Thanks for the reply guys, I am trying to snap the prefab to the Terrain, but for some reason it does not want to work. I don’t know where I have gone wrong in the code?
I don’t think that’s going to work, the code will need something specific to snap to on the terrain so unless there’s a co-ordinates system I don’t know about on a terrain object itself then that won’t work right.
Are you sure you’re talking about ‘snap to grid’ are you perhaps thinking about making it so that the ghost prefab you’re putting onto the terrain stays on the surface as you’re moving it about and rotating? I could help you with that as that was annoying me a lot awhile back.
Hey, thanks for the quick reply. What I am wanting to do is illustrated very well in this video. Please have a look, because this is the type of building placement I am trying to make with my code. Note: notice in the video from 0:07 that the ghost prefab snaps to the grid on the Terrain. Now, I am not trying to make a separate grid but I just want to fake it with my code.
Ahhh! I see where you’re going with this, the reason it’s not working is because you’re simply placing the building at your raycast hit.point, first you’ll need to learn how to make a grid that goes over the terrain.
Like I said before, the same rule applies here, your code needs something to snap to in the first place for it to work properly in this case, you need to make a grid and have it snap to a mouse position on the grid.
Let me try looking up something that might help you.
Edit: Found just what you’ll need to learn about.
I should warn you, if you’re a noob and didn’t know anything about this, this is a very complicated subject to get into, it would actually be easier to do all this without a grid and you’ve already got the code set up.
Hey, thanks for the video. Before I try the method out, I was thinking about performance issue if I use quads like in this guy’s video because the method this guy was using he is using quads to generate a grid. Just asking, won’t this affect the computer performance? That why I was trying to fake it so that it does not affect performance.
Someone else with more experience will have to answer that unfortunately I don’t think though there’s much getting round that the more calculations you have to do the more it will affect performance. It’s a trade off, if you want certain game mechanics to work like this on a massive scale then you’re going to have to accept you’ll be cutting off various types of players who might not have the machine to run it and so on.
Yes, I will try the method later and let u know if it was successful… Unfortunately, I am busy with some other works so I can’t try the tutorials at the moment, but will try later.