Hot answers tagged

5

The best way to do this depends on a few things. I'm going to assume the following - if any of these are incorrect, please let me know and I'll update my answer: You want the closest enemy that is anywhere to the right of the player within a certain range, even if it is at a different height You want true distance, not just the one that is closest in ...


4

It has been a while since I've touched Unity, but this should get you in the right direction. Assuming A, B, and C are objects, and we want to see if B or C are closer to A: var BtoA = (B.position - A.position).magnitude; var CtoA = (C.position - A.position).magnitude; // Assume B should return if distances are equal if(BtoA <= CtoA) { // B is ...


4

Although I wholeheartedly agree with @Jon on this one I'm still going to answer your question. The following class will write to a file in the Update method. Have fun with it. using UnityEngine; using System.IO; public class VeryBadIdea : MonoBehaviour { private string filename = "WeWLad.txt"; private float some_variable; private string ...


3

You do not want to write to a file inside of Update(). That is bad design, and ridiculous. Instead, you may want to consider writing to memory in your Update() function -- and then dumping this data to file at a reasonable opportunity.


3

You actually do not need C++/CLI for this, although you could use it if you wanted. I don't really recommend it. C#'s platform invoke facilities should be sufficient for you. You'll need to make your engine available as a DLL. Doing so is beyond the scope of this question, but there's plenty of information about making DLLs available on the web. Once you ...


2

So long as your 'Text' object contains a Text component, this will work as expected. You may have replaced the Text component with your TextController script, or it was removed some other way. Alternatively, you could change your script as follows: using UnityEngine; using UnityEngine.UI; public class TextController : MonoBehaviour { public ...


2

Graph-based pathfinding calculates an entire path (multiple turns' worth) and you're using only the first step, throwing away the rest, and recalculating every turn. This is wasteful. Keep following the path already computed until you think you need to recalculate it. If you're far away from the target, recalculate infrequently; if you're near it, ...


2

The simple solution would be to just have each actor calculate the ideal path using A* and then stick to it. Implement each lane of a road as a first-in-first-out queue. The edges in your A* graph should be the lanes, not the tiles. When a car wants to move onto a lane but that lane is already occupied by the maximum number of cars, they simply wait until it ...


2

Note that you can supply the SpriteBatch.Begin with a matrix parameter. Use your ingame "virtual screen" dimensions to calculate the scale factor. This way you can draw your game in the native "virtual" resolution and have the matrix do the heavy lifting for you. For example: float xscale = (float)ScreenWidth / VirtualscreenWidth; float yscale = (float)...


2

All the left enemy are in lower value in x-axis position. Objects on right side are on higher value. Subtract main character's x value from enemy's. Positive results are for enemies at right side.


1

Assuming your enemies all have colliders, could you not simply fire a 2D Raycast https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html in a straight line to the right of your player and return the GameObject of the first collider it hits?


1

Well your code is very smart but Unity has already done those calculations for you and you can just use void Update () { transform.Translate(0, 0, -Input.acceleration.z); } as it will take the device rotation in the world and translate it into Unity same way it behaves with WASD movement.


1

The thing is that with your list you dont know if the element is a weapon or a apple. It's just a GameItem. To be able to use the weapon specific properties you need to retrieve the element and cast it back to a Weapon. For example the update in inventory manager will look like this. void Update() { if (Input.GetKeyDown("a")) { ...


1

While I don't know for sure what these two games do, this is how I would do it: Have a Canvas set up with a child GameObject (which can be a prefab) holding all Game Over UI elements. This starts as disabled In the UI GameObject have a script which listens to OnEnable and fills out the relevant info (stats etc) Have a Game Controller that controls the ...


1

If I would implement it in Unity I'd do a dedicated scene with a GUI to show whatever is needed. So in the main scene I would have some script not destroyed on load of a new scene (with DontDestryOnLoad(GameObject go) method) which have all the infos you want to display at the Game Over stage. You could also handle it in a whole scene with variables to ...


1

If I understand your question correctly this can be done using a bit of vector math. I've created an example in Javascript so please keep in mind that many of the mathematical operations I'm doing here can be done in Unity really easily using the built-in classes and methods. The ball will follow your mouse when you hover it over the window. I recommend you ...



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