void SpawnMachine()
{
Vector3 SpawnPoint = Vector3.zero;
Ray SpawnZone;
RaycastHit SpawnPos;
SpawnZone = GameCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(SpawnZone, out SpawnPos))
SpawnPoint = SpawnPos.point;
Instantiate(MachineFile.PropToUse, SpawnPoint, Quaternion.Euler(RoT));
}
}
Round what?
What you mean by ‘Round’?
SpawnPoint…
Something like this? Taken from a ProBuilds api file… apparent you can run it in realtime but I don’t get how and well I’ve actually got my system working apart from the snap… plus I also don’t want to really use files from other things. I actually need to write my own code because of my structure…
{
/**
* Static helper methods for rounding values.
*/
public static class pb_Snap
{
/**
* Snap a value to the nearest increment.
*/
public static float Snap(float value, float increment)
{
return Mathf.Round(value / increment) * increment;
}
public static Vector2 Snap(Vector2 value, float increment)
{
return new Vector2(
Snap(value.x, increment),
Snap(value.y, increment));
}
public static Vector3 Snap(Vector3 value, float increment)
{
return new Vector3(
Snap(value.x, increment),
Snap(value.y, increment),
Snap(value.z, increment));
}
}
}
Basically I want to take; SpawnPoint then do this to it and make it = SnappedSpawnPoint(vector3)
Could I BroadcastMessage(“Custom”); the SpawnPoint value then Broadcast it back after doing the math?
What I’ve done so far to give you an Idea…
I think I can rewrite this to work for me… But on the other hand… its seems to need to be on the floor… I need it to be a empty object…
You could use the modulus operator to get the remainder you do not want and subtract it from your value.
Vector2 TileSize;
Vector2 InputPos;
Vector2 usePos;
usePos.x = InputPos.x - (InputPos.x%TileSize.x);
usePos.y = InputPos.y - (InputPos.y%TileSize.y);
You might need to add an offset depending on what your target tilesize or grid is like
SpawnPointRounded = new Vector3(Mathf.RoundToInt(SpawnPoint.x), Mathf.RoundToInt(SpawnPoint.y), Mathf.RoundToInt(SpawnPoint.z));
If your game is 2D set the z to 0.
thankyou I will try that out… the grid size will be public variable…
SpawnPointRounded = new Vector3(Mathf.RoundToInt(SpawnPoint.x), Mathf.RoundToInt(SpawnPoint.y), Mathf.RoundToInt(SpawnPoint.z));
well that did something… but yea… like only reason i could tell was coz i changed the SpawnPoint bits to numbers… but yea that just did a horizontal & vertical line across the screen but hey I still learned something…
Vector2 TileSize;
Vector2 InputPos;
Vector2 usePos;
usePos.x = InputPos.x - (InputPos.x%TileSize.x);
usePos.y = InputPos.y - (InputPos.y%TileSize.y);
lol well I did this -
SpawnPoint.x = Input.mousePosition.x - (Input.mousePosition.x % GridSize.x);
SpawnPoint.y = Input.mousePosition.y - (Input.mousePosition.y % GridSize.y);
with this-
public Vector2 GridSize;
and they spawned all over the place in random spots lmao…
think I might need a z for it lol…
Check this tutorials on object management and positioning, there’s all you need and more
Looks cool^
SpawnPointRounded = new Vector3(Mathf.RoundToInt(SpawnPoint.x), Mathf.RoundToInt(SpawnPoint.y), Mathf.RoundToInt(SpawnPoint.z));
with this one I know that was really close!! other one not far off either but i think it might just be where I’m putting or something?
But yea anyway if the grid size is a public float! called GridSize for eg… which bit do i manipulate?
x & z? like this? vv
Vector3 halfSlots = GetComponent<Renderer>().bounds.size / 2.0f;
// Transform position is the center point of this object, x and z are grid slots from 0..slots-1
int x = (int)Math.Round(Math.Round(point.x - transform.position.x + halfSlots.x - grid / 2.0f) / grid);
int z = (int)Math.Round(Math.Round(point.z - transform.position.z + halfSlots.z - grid / 2.0f) / grid);
// Calculate the quantized world coordinates on where to actually place the object
point.x = (float)(x) * grid - halfSlots.x + transform.position.x + grid / 2.0f;
point.z = (float)(z) * grid - halfSlots.z + transform.position.z + grid / 2.0f;
I think Something like this is what I need, I managed to merge my file with that from the link i put up earlier but yea that’s got problems… like I cant use a toggle to enable disable it… coz its on the floor lol… My system is on an empty Object with the floor broadcasting a message to activate the build function… “SpawnMachine” so…
public float GridSize = 5.0f;
int x = (int)Math.Round(Math.Round(SpawnPoint.x - transform.position.x - GridSize / 5.0f) / GridSize);
int z = (int)Math.Round(Math.Round(SpawnPoint.z - transform.position.z - GridSize / 5.0f) / GridSize);
SpawnPoint.x = (float)(x) * GridSize + transform.position.x + GridSize / 5.0f;
SpawnPoint.z = (float)(z) * GridSize + transform.position.z + GridSize / 5.0f;
SpawnPoint.y = SpawnPos.point.y;
GOT IT!! THanks guys you totally helped me see the pattern etc…
The intention was to use the modulus method on the RaycastHit.point.
Other than that, if youre satisfied with that youve made so far, great.
Oh right I see… Lol literally 3 lines off… Well I mean the way it spawned was really interesting only problem is it was off screen but I guess you could use that to spawn enemies out of camera lol… Yea im happy coz I can now use my system to make machines, recipes & ingredients etc via scripted objects instead of game objects like most assets, and coz I’ve got the floor broadcasting that message I can just do a simple activate gameobject button to turn them on so now basically I can just yea right click create new machine then fill the options in/out then make a cube give it a child make a button n it’s pretty much done… And now I’ve got that as a base for all sorts of things… So yea “totes” happy… Just might have to goto using squares dunno… Think my floor might be in the wrong spot 0.0.0 is wrong when you do this isn’t it?
Here’s the outcome… Fixed the floor issue…