I have a 2D game where the users can create cars by clicking on the screen to add vertices and then drag these around as they see fit. These vertices are then used to draw a mesh. Users can also add wheels to the vertices (1 per vertex).
In the game there is also an option to have the computer generate a random car, which it does by creating eight vertices at random points inside of a unit circle.
These cars are stored and it is possible for the user to load them and re-use them. The two pictures below show a square button with a computer generated car (The green one with red wheels) which sits inside of the square as intended, and a user generated car (The white one with pink wheels) which sits outside of the square.
I know that this is because the mesh of the computer generated one draws triangles from Vector3.zero which is the center of the object, while the user created one only triangles between the vertices, which aren't necessarily placed around the center of the object (As in this example where they are all placed above and to the right of the center).
How would I go about centering the mesh of the user created car?
I could perhaps calculate the center of the mesh and then subtract that from the position of all the vertices. Or I could subtract that position from the position of the GameObject that holds the mesh when presenting it in the gui. Or are there better alternative solutions?
Using Mesh.Bounds
Transform car;
Bounds carBounds = car.GetComponent<MeshFilter>().mesh.bounds;
Vector3 whereYouWantMe;
Vector3 offset = car.transform.position - car.transform.TransformPoint(carBounds.center);
car.transform.position = whereYouWantMe + offset;
This will also give you access to a bunch of cool fields like center min max and size
Related
I am building firest person Firearm Simulator. When i fire bullet hall prefab showing on the target board.
this is my target
when i fire
Hole prefabs sticking on the target board like red rounds.
i need to get a range of the holes. need to get measured the holes on 4Inch, 6inch or 10-inch rounds.
First, you need to place a GameObject in the center of the target. Then you need to create a float variable for each circle that is equal to find how far away each circle is from the center. To do this, my suggestion is to copy and paste the center game object and move it to each circle on the x,y, or z axis and record how far away each circle is from the center. Once you have those numbers, you need to create an algorithm to find how far away the bullet is from the center. Finally, once you have how far away the bullet is from the center, develop a new algorithm to find between what circles the bullet is in. You will need to create an algorithm using if statements with greater than(>) and less than(<) values comparing the distance the bullet is from the center to the circles distance from the center to find what circles the bullet is between.
I want to create 2d game. The game is similar to the Scale Puzzle. I've already created nearly all the functionality. Only the last remains.
.
This is example.
And that's how I draw shapes.
.
I click inside a white square, and after 1 seconds a square is drawn regardless of the position of the ball. (x,y).
Square is created programmatically, and it is added to the parent element "SquaresList" with name New Game Object.
How can i do, so that the violet field becomes larger, and in the middle of the screen.
I made it so that for every 3 clicks, "SquaresList" increases Scale by 0.25f, and get negative position of the ball. Example:
SquareList.transform.position = new Vector2(-ball.pos.x, -ball.pos.y)
This does not work correctly.
What options can be? (intersections, find the max/min point, math formulas) ?
Hi NoName :) Your method
SquareList.transform.position = new Vector2(-ball.pos.x, -ball.pos.y)
would work perfectly if your object does not inherit other objects and there positions. To fix this make sure that you reset the position of all other game objects that you use as containers.
I have this scene in unity with a chessboard and pieces. Each figure and chessboard are models downloaded from asset store. So I want the user to click on the square and click on the figure to move my figure to this square on the chessboard. How can I do this with my game object? Should I define some coordinates of squares clicked by the user? And how can I track where the user clicked?
My scene looks like this:
I had only such scripts for moving figures to defined by me square. An example for a rook:
void OnMouseDown()
{
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + 71);
}
Rather than defining some grid, or rounding to nearest integers, you could simply have a collider on every square.
Each of those squares would be part of a square collision layer. Then you would do the same for all the pieces, allowing for the peice layer to interact with the square layer (so they don't fall through the table)
Then comes the easy part: You just do two raycasts, the first against pieces, to determine what piece the user is trying to move, and the second to determine where they are trying to move it (and move the piece to the center of the square)
This question also might be of some use in figuring out how to select objects.
I have tried to find any information on how the Unity assigns pivot points to object but all I keep finding is threads on how to move pivot points and that it can't be done. I am creating a 2D game with a background that is randomly created with meshes that are wrapped in empty GameObjects. These objects are organically shaped but they have a property that returns a rectangle that bounds the object so that they can be placed in a way that they are not overlapping. The trouble is that the algorithm assumes that the pivot point is going to be the center of the object. What I would like to know is how does Unity decide where the pivot point will be set to so that I can predict how much I will need to move my mesh inside the parent object so that the pivot point will be in the center of the bounding rectangle.
Possible fix:
Try create the meshes during runtime and see if it always places the pivot points at a certain corner or at least relatively speaking the same location.
If it does that you would know where the pivot point is and could take it into account in your code, if you also know the size of the mesh you spawn.
So I think most general and correct answer that I can come up with is that unity assigns the pivot point to the center of the GameObject that you apply the Mesh to. The local coordinates of the vertices of the mesh depending on how you create them mighht place your mesh so that its logical center is not the same as the that of the empty GameObject that it is attached to. What I did to fix the issue was to make a vector from local point (0,0,0) to the center of bounding rectangle and translate the vertices I use to make my mesh by that vector inverted. It wasn't perfect but by far close enough to ensure that I won't have any overlapping meshes.
I have a rectangle (sprite) and I need to place different game objects (sprites) inside that rectangle but so they are all "aligned" by their bottoms.
For the life of me, I cannot make it work in Unity.
Say that my box has a height of 5.
I want to place the different size objects so they are all "resting" at the 2.5 y axis inside the box.
Does anyone know how I can do that since transform.position measures from the center of the GameObject?
Thanks!
Don't use transform.position, use RectTransform properites, as they take anchor points into account. In particular you need to set the anchor position for the sprite in the prebab / inspector and then use RectTransform.anchoredPosition to position it.