Hot answers tagged

16

In the decision on what tile prefab to choose, you can think of it this way: If (xCoord + yCoord) % 2 == 0, then select prefab1 If (xCoord + yCoord) % 2 != 0, then select prefab2 This ensures the tiles form a "chess-like" board. Note that you will probably want to pass xCoord and yCoord as integers instead of floats.


5

It looks like all these gestures only care about which points you hit around the outside of the stick's range, and the order you hit them in, so we can take a very simplified approach. Let's label each sector in the outer ring with a letter: Each time the stick lands in a new one of these sectors, we'll add that letter to a string. We won't repeat a letter ...


5

The standard Unity .gitignore file shows what files and folders should not be committed to git and can therefore be deleted. # This .gitignore file should be placed at the root of your Unity project directory # # Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore # /[Ll]ibrary/ /[Tt]emp/ /[Oo]bj/ /[Bb]uild/ /[Bb]uilds/ /[Ll]ogs/ ...


3

Unity follows a principle called Composition over Inheritance. When you have multiple entities in the game which have some behaviours in common and other behaviours which are unique to them, then there are three possible options: One stand-alone class for each entity type. But if you have multiple entity types in your game which all move n pixels forward ...


3

You can do this more simply by having one coroutine that expresses the sequence of actions you want to perform: IEnumerator MovePauseAndReturn() { yield return MoveTo(targetPos); yield return new WaitForSeconds(1); yield return MoveTo(origPos); } And a workhorse that performs the move: IEnumerator MoveTo(Transform destination) { while (...


3

Your DontDestroyOnLoad is exactly how I did it in Cognizer. I have hooks in the first script of each scene that will create the Game Manager object if it doesn't already exist. So I can start any scene, and everything works fine. Cognizer doesn't work unless the player has selected game settings through MainMenu, so each scene has BootToMainMenu.cs attached:...


3

I can't see how you have your scripts, but each event A, B and C needs to extend from a common base class. Assuming your CustomEvent class looks like this //base class public class CustomEvent : MonoBehaviour { //whatever you have in here } You would make your A, B and C events inherit from this class //extends base public class CustomEventA : ...


3

This occurs because you're displaying your sprite at a size on screen such that there's a non-integer ratio between the number of screen pixels it occupies, and the number of source texels in the sprite. That means that somewhere, some source texels need to be displayed more times than others to fill the space, and which ones get repeated/skipped depends on ...


2

The current setup I'm using is to have a scene dedicated to GameManager objects (input, audio, game services, etc). Obviously that means I'm using a multi-screen setup. It looks like this: Loading scene Game Manager scene Main menu scene UI scene Base level scene (used by all levels) Unique scene per level At runtime, the Loading scene scene is set as the ...


2

One way to handle functions that don't logically belong to any one scene is to put them into an object that doesn't live in a scene at all, but rather as an asset in your assets folder. For instance, we could make our manager a ScriptableObject: [CreateAssetMenu(fileName = "GameManager.asset", menuName = "Managers/Game Manager")] public class GameManager : ...


2

The Unity 2d and 3d physics engines are completely separated from each other and work in very different ways. So you can not expect to see every feature being mirrored 1 to 1 between 2d and 3d. The effectors happen to be among the features exclusive to the 2d engine. But if you want to simulate them, then that's often not difficult to do. Create a game ...


2

I assume you already created a Perlin noise which is supposed to represent the tree density and now you wonder how to feed this noise map into your tree spawning system. The simplest approach is to place a tree on every position where the amplitude of the Perlin noise exceeds a specific level. That will give you islands of dense grids of trees separated by ...


2

Many Unity tutorials start with just attaching the camera as a child to the player. While this is a quick and easy way to get going (while also demonstrating pretty well how transform hierarchies work), it's usually not how most real games end up controlling their camera. If you want your camera to follow the position of an object but not its rotation, then ...


2

First of all, you should not be doing this. The Unity scene graph exists for a reason. It's the very core concept which the engine architecture is built around. By rolling your own you are locking yourself out of approximately 99% of the features Unity provides to you. You won't really gain much over just keeping your codebase in C++. If this is just about ...


2

When this question was just about drawing a plain grid of lines, I said... I'd be tempted to draw this with a shader on a single quad, using the world coordinates of the vertices to snap the lines to your desired intervals, no matter if the parent quad is moving in sub-grid increments. Here's a quick shader that does this, with parameters to control the ...


2

I think it might be a typo. You have a method named onEnable. This should be OnEnable (Capital O). You can see all the methods that MonoBehaviour supports here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.html


2

You can programmatically set the aspect ratio. https://docs.unity3d.com/ScriptReference/Screen.SetResolution.html Here is some sample code from Unity: using UnityEngine; public class ExampleScript : MonoBehaviour { void Start() { // Switch to 640 x 480 full-screen Screen.SetResolution(640, 480, true); } } If you want to set ...


2

Be sure that you have set Spatial Blend to "3D". The slider is two entries above the 3D Sound Settings collapsible section (in my version of Unity). You can also enter a value from 0 to 1, with 1 for full 3D. The slider position defaults to 0 for 2D, which will override any 3D settings you configure in the lower section.


2

There are lots of ways to skin this cat. You could decide your axis first, then decide the direction along that axis: public static Vector2 GetRandomCardinalDirection() { Vector2 direction = Vector2.zero; int componentIndex = Random.Range(0, 2); direction[componentIndex] = Mathf.Sign(Random.value); } You could use a switch statement to pick ...


1

You can chain execution from one IEnumerator to another by yielding it. So we can have one master coroutine that expresses the eternal alternation between these two phases: IEnumerator AlternatingSpawnLoop() { // Keep looping forever. while(true) { // Run the enemy spawn loop until the enemy phase is over. yield return EnemySpawnLoop(...


1

Use WaitForSecondsRealtime on the place of WaitForSeconds. It will work.


1

Ah, the joy of creating resolution-responsive screen layouts in Unity. If I understand you correctly, your goal is to create an UI which always preserves its aspect ratio, but also fills as much of the screen as possible, letterboxing or pillarboxing as necessary. If that is the case: If your canvas itself has a Canvas Scaler component, remove it. Add a ...


1

I found what i did wrong , It was because I used transform to position a Object that had a rigid body. The RigidBody component causes the object to be subject to Physics Laws and trying to reposition it using transform didn't stop factor like gravity acting on it.


1

The simplest solution is to take your initialization code and put it into Awake instead of Start. Like Start, Awake is guaranteed to run just once for each instance of a script (unlike OnEnable which can fire multiple times if the component is disabled and re-enabled) But, like OnEnable (for already-enabled scripts), Awake runs before Instantiate or ...


1

You have stumbled onto a controversial topic in the programming world. Most people dislike singletons, but we game developers tend to be a bit more friendly towards them. There are pros and cons to singletons. My answer is specific to Unity, and covers maybe 1% of the topic. In Unity, Singletons are very useful. Let's say you have a manager class that ...


1

Rect is not a Drawable thing so you can't assign color to it. The Rect does not contain information for color, Unity Docs ref. This is something you're going to change when you are drawing that Rect. Let suppose you want to draw button out of it then you do something like this: drawArea = new Rect(0, 0, Screen.width, Screen.height); //Changing Button ...


1

You can add one collider which covers the whole grid. You can then use a usual mouse-into-scene raycast. The resulting RaycastHit object has a member Vector3 point which is the point in world space where the collider was hit. Assuming your tiles are in a two-dimensional array, each tile is 1 unit in size and the origin of your tilemap is at world ...


1

You can use Sprite bounds to get the rough size of your sprite. In your enemy class, float spriteSize; void Start(){ Sprite Sprite = GetComponen<SpriteRenderer.().Sprite; spriteSize = Vector3.magnitude(sprite.bounds.extents);// get this only once for efficiency } public void TakeDamage(int damage) { int dist = 10; Vector3 spawnPos = ...


1

It looks like you're using this CStat type purely for serialization, and at runtime everything uses the versions you've decoded into your dictionary. With that in mind, we can set up a type that always serializes its value down to a string, but remembers its assigned type so it can be decoded to the appropriate type in your dictionary: [System.Serializable]...


1

public class Turn_Move : MonoBehaviour { public int TurnX; public int TurnY; public int TurnZ; public int MoveX; public int MoveY; public int MoveZ; public bool World; int randomNumber; // Use this for initialization void Start () { InvokeRepeating("GetRandomNumber",0,5); } // Update is called once per frame void Update () { if (World == true) { ...


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