Hot answers tagged

78

A convex polygon has a very nice property: The shortest path between any two points in the polygon, or anywhere on its edges, is just the straight line between those points, and that line segment lies wholly within the polygon. So if your polygon represents a section of your level known to be obstacle-free, then you're guaranteed you can do the very simplest ...


11

I will explain the concept in shadertoy then I will convert it to Unity because learning with shadertoy is easier. Shadertoy uses multiple passes, one per "Buffer". As the name indicates, this passes store the results in a buffer. A buffer is just a texture. I am sure Unity will let you render to textures too. Simple Liquid Buffer1: Painting void ...


4

The usual approach here is to pretend like you're solving a maze blindfolded: keep your left hand in contact with the wall, and follow its contours until you reach the exit (or in this case, until you return to your starting point) Assuming we start with some tile in the region we want to outline, but not necessarily on the edge of that region, we can keep ...


3

This is a pretty straightforward translation. First, let's compute the vector (i, i) * res, which is (offset / texture width, offset / texture height): Then we can multiply that by (1, 1) (no-op, so I skip that node), (1, -1), (-1, -1), and (-1, 1) to get the four shifts from our sample point, and add them to our UV to sample at those shifted positions. We ...


3

You can control the spawn rate by changing the value of secondsBetweenSpawn to a value between 1. One way to do that would be via script. For example, you can reduce the time at a rate of 10% every 5 seconds with this line in your Update function: secondsBetweenSpawn = Mathf.Pow(0.9f, Time.timeSinceLevelLoad / 5f); But another solution I find far more ...


3

I see you already have an answer to your question, but I'm going to add another answer for anyone who's trying to solve a simpler variant of the question. Civilization puts borders between hex tiles when the owner of the tiles are different. The simplest algorithm is to look at the edges between tiles: let H : iterate over all hexes let N : iterate over ...


3

This is a feature of the C# programming language called an explicit cast. When you have a reference of a type of a base class or interface, but you are sure that the object it points to is of a certain more specific sub-class, then you can use it to convert that reference to a reference of the more specific class. In this particular example, a Renderer is a ...


3

Renderer is a subclass of Component. So (Renderer)b casts current Component to Renderer. GetComponentsInChildren(Type type) (that returns Component[]) was used before its generic counterpart GetComponentsInChildren<T>() (that returns T[]) was added to Unity. In today's Unity this code can be simplified to: void SetTargetInvisible(GameObject Target) { ...


2

Unity has a built-in function for this: public static float DeltaAngle(float current, float target); Description Calculates the shortest difference between two given angles given in degrees. So you can write... if ( Mathf.Abs(Mathf.DeltaAngle(angleA, angleB)) > 90f ) { ... } Don't forget to search for past answers and read the documentation when you ...


2

What you are experiencing are various bottlenecks when performance isn't purposely limited. If you run a game (even a static none-moving cube) a typical game engine will tell the GPU to render and then go through the game loop and tell the GPU to render again. If there is nothing going on in the game this is very fast the GPU spends most of it's time doing a ...


2

I don't recommend using code like this for terrain generation, for several reasons: You don't want to be re-processing your terrain in every Update. If your terrain parameters can change on the fly, call a Regenerate() function to do this work only when such a change occurs. You don't want a separate object for every cell of your world. I know it looks ...


2

Ok... At first I was expecting there is some inner attribute on VisualEffect, which will tell me if it's playing or not... But didn't find anything in that way. So then I was thinking it is not so much big problem, when you just call play... But apparently is... As @DMGregory suggests, I just call the Play() once, added custom variable to hold state, if it ...


2

Presumably you considered just converting your angle and distance from polar coordinates into a Cartesian vector in the usual way? public static Vector3 FromPolar(float angleRadians, float radius) { return new Vector3(Mathf.Sin(angleRadians), 0 Mathf.Cos(angleRadians)) * radius; } You can now add that offset to the player position to spawn the building ...


2

Final code is easier than the one I posted. I just didn't think about doing it this way. public Vector3 rotation; public void RotateDegreesFast () { Quaternion newRotation = transform.localRotation * Quaternion.Euler(rotation); transform.DOLocalRotateQuaternion(newRotation, rotationTime); }


2

In essence, You need to make sure that the style that is used has wordwrap set to true. The problem with what you tried is that the style you edited isn't used by the TextArea. Which style is used is actually a weird one, as I expected EditorStyles.textArea but for some reason, unity uses EditorStyles.textField as the default style instead...(which has ...


2

A slightly different approach to the one given by Michael, which I am using on my project, if you are using crossplatform input axis and convert it to vector3 for movement; public float h; //input axix x public float v; //input axis y private void FixedUpdate() { h = ...


2

As I mentioned in the comments, a simple way to investigate execution order in Unity is to add a debug log to each method you're interested in, something like... Debug.LogFormat("{0}: [Event] called on {1}'s {2} at {3}", Time.frameCount, name, this.GetType().Name, Time.time); Then ...


2

My usual approach is to minimize managers by creating smarter gameObjects which have their own controllers. "coins picked up" and "score" are properties of the player, so they should be on the player game object. Instead of an UI Manager, I create controllers for individual UI elements. When they show information about a gameObject (like ...


2

This code tries to scale objects to the visible size they'd have if they were 1 unit away from the camera. That's awfully close to use as a benchmark size! A sprite that's 1m from your camera would be quite large, so a sprite that's just 1 unit farther away would need to double in size to match it. That means you saturate your upper scaling limit almost ...


2

Set line breaks inside tags UPTEXT<size=14> CENTERTEXT </size>DOWNTEXT or UPTEXT<size=14>\nCENTERTEXT\n</size>DOWNTEXT


2

Save in your playerprefs the version you are using. Have some migration function in case your current version is higher than the saved version. Always migrate only one version, in case saved version is 1.0 and current game uses 4.0, you should migrate from 1.0 to 2.0 to 3.0 to 4.0 without skipping a version. You can either adjust the score on migrating or ...


2

the answer seems to be IPanel.Pick var picked = rootVisualElement.panel.Pick(evt.mousePosition);


1

If you want to go from screen space (mouse position) into world space, then you're using the wrong method. If you want to place an object at a particular depth from the camera, you can use this: Vector3 GetMousePositionInWorld(float depth) { Vector3 position = Input.mousePosition; position.z = depth; return Camera.main.ScreenToWorldPoint(position)...


1

Solution was the edit the Canvas 'Order in Layer' option, with the pause menu having a higher order. Presumably this means the pause menu UI, is now properly layered over the top of the game scene UI, and so the mouse corrects hovers over the pause menu buttons, not the game buttons.


1

One of my favourite tricks in Unity is the OnRenderImage method. Put this on a script on your camera, and you get to intercept the image the camera has just rendered, and change it any way you like before it gets passed along to the screen or the next rendering pass. We'll use this to make a script that takes a material and applies it as a post-process ...


1

You seem to be under the impression that this line of code asks to do multiple things: Invoke("bulletplus", 0.5f); Schedule the method named "bulletplus" to be called 0.5 seconds from now. Pause the execution of the current method we called it from, and let the rest of the engine continue running its updates and rendering for the frame. ...


1

Thanks to @DMGregory, the problem is resolved. The root cause of the problem is the Pivot configured in the Sprite Editor is incorrect. After setting the Pivot to "Center", regenerate the tiles and reset the Tilemap pivot points to X=0.5, Y=0.5, Z=0, the selection in the Tile Palette panel becomes normal again.


1

Your solution might work with a world-space canvas, but I don't recommend using world-space canvases for most situations because it's difficult to make them adapt to different screen aspect ratios/resolutions. If you're using a Screen Space canvas, you need to convert the world coordinates of the sprite renderers into screen space, then convert that into ...


1

Scene Loading Basic Tested this in a new project. The idea is to start scene preloading when Return button is pressed and save AsyncOperation that is doing it, disallow scene activation for it and allow it when you need the scene to be loaded as soon as it's ready. I would expect it to be loaded into memory, be careful if you are RAM-bounded because both ...


1

From the error message, it looks like Tiled2Unity is trying to do something illegal in this version of Unity, and so its operation is aborted. The website for the tool reads: Warning: Tiled2Unity is old software! Tiled2Unity was created before Unity had support for tiles and tilemaps. It is still available for download but is outdated and unsupported. ...


Only top voted, non community-wiki answers of a minimum length are eligible