New answers tagged

2

Its because you have declared int i two times. If you want to save new value change: int i = i + 1; to: i = i + 1;


0

I will trow my implementation too for future generations . void Awake() { if (instance == null) instance = this; else if (instance != this) Destroy(gameObject.GetComponent(instance.GetType())); DontDestroyOnLoad(gameObject); } For me this line Destroy(gameObject.GetComponent(instance.GetType())); is ...


1

You don't need to add the Guns via the inspector one by one. Just execute FindObjectsOfType at the start of your game: Gun[] guns; void Start () { guns = FindObjectsOfType<Gun> (); foreach (Gun gun in guns) { Debug.Log("Found " + gun.gameObject.name); //Now you can access their location with gun.gameObject.transform.position ...


0

You could maybe detect the orientation of the device and if its in landscape mode, it spins in the other direction. if (portraitMode) { rectT.localEulerAngles = Vector3.back * wheelAngle; } else { if (landscapeMode) { rectT.localEulerAngles = Vector3.forward * wheelAngle; } }


0

Capsule Colliders and Character Controllers are great for a TPG.


2

You should use a capsule collider, because it doesn't have edges and works great and also you could apply some constrains in the rigidbody, its common to apply. freeze position in: x and z freeze rotation in: x,y and z I hope that this solve your problem. Cheers!


-1

public void RoomMirror() { Vector3 temp = transform.rotation.eulerAngles; if (temp.y == 0f) { temp.y = 180.0f; } else { temp.y = 0.0f; } transform.rotation = Quaternion.Euler(temp); Mirror = !Mirror; RoomMirror(ref _east, ref _west); } This ...


1

The Start method is only executed once at start and the NavmeshAgent won't start moving the object before the Start method finished. The moving happens between calls to Update. So check if you reached the destination in your Update method, not your Start method. I would also recommend you to use agent.remainingDistance to check if the destination is ...


2

As a common rule you should always rebuild that kind of matrix instead of repeatedly applying transforms, or you get the kind of cumulative error you're seeing =) Every new transform and the error gets worse and worse, faster and faster. This is because floating point numbers are imprecise and a matrix has many multiplications applied to it at each new ...


2

To solve all of this problems you still have to write quite a lot of code. So this answer is just a general overview of what you need to do, not a complete implementation. When you need specific help with one of these points, feel free to ask it as a new question. When you want multiple goals, you need to change public Transform goal; into an array of ...


4

Identifiers in C# are case sensitive. You want iteminfo.text not iteminfo.Text You can verify in the docs that the UnityEngine.UI.Text.text property is all-lowercase.


1

float Timer = 2f; float TIMER = 2f; float StartVelocity = 10; float EndVelocity = 0; void Update() { float elapsed = (float)gameTime.elapsedSecondsOrWhatever; Timer -= elapsed; if (Timer < 0) Timer = 0; velocity.Y = MathHelper.Lerp(EndVelocity, StartVelocity, Timer/TIMER); }


0

Your question is vague (could you please elaborate?), so I'll assume that you don't know how lerping works and that you don't have access to the math function, then you can write your own like this: float lerp( float value1, float value2, float alpha, float range ){ alpha = alpha/range; if( alpha < 0 ) alpha = 0; if( alpha > 1 ) alpha ...


1

You'd do this in the opposite order. Rather than: Create Modify Attach to GameObject (there's no way provided to do this) the order is: Create and attach to GameObject (one fused step) Modify Like so: GameObject go = new GameObject(); MeshRenderer newmeshrenderer = go.AddComponent<MeshRenderer>(); // Do stuff with newmeshrenderer, eg... ...


1

It should work like the example on the Scripting API for AddComponent http://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html MeshRenderer mr = gameObject.AddComponent("MeshRenderer"); (...) //do stuff with mr


1

Acually you want to load resource from your project views there are 2 ways to do this.(which I know if somebody know more then surely update this answer) Resource.Load Asset Bundles 1 . Resource Example void Start() { GameObject go = GameObject.CreatePrimitive(PrimitiveType.Plane); Renderer rend = go.GetComponent<Renderer>(); ...


0

I do have some answers for you: how can I randomly move the robot asset through the house? Use a navmesh agent. (this tutorial will help you)The robot would have to set a new random target, once it reaches it's previous. This could be done by selecting a random one out of several predefined target points. Alternatively, you could attach a standart ...


0

To generate unique seed from non-boolean traits (small integers in some range), for purposes of random generation or other equivalent, you can treat all the possible player choices as dimensions, giving you a multi-dimensional coordinate when you consider them together (in the state space of all possible trait combinations a player may choose) So if you ...


1

Check broad-phase collision detection. Example link That would reduce the collision detection cost. Then figure out WHAT causes your performance drop. Virtual method calls themselves are not that expensive - 1000 of them in a frame will definitely NOT cause a slideshow. Perhaps the message system is slow? The message creation copies data by value? etc etc. ...


1

I finally succeeded to get rid of this behaviour. (Thanks @Jon !) "Don't let the snake change its direction if it heads backward from its current heading. " It actually works :) Example with 'Up' Direction : int X = _snake._parts.Last().X; int Y = _snake._parts.Last().Y; if (newDirection == Direction.Haut) { Rectangle test = new Rectangle(X, Y - ...


0

I think you might find the following function useful: public Texture2D getTexture2DFromRenderTexture(RenderTexture rTex) { Texture2D texture2D = new Texture2D(rTex.width, rTex.height); RenderTexture.active = rTex; texture2D.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0); texture2D.Apply(); return texture2D; } This would be ...


2

It might be useful to note that while it is not guaranteed to be unique, your original approach is still going to work in practice with small amounts of traits, and a collision would not even have disastrous consequences if it happened. The chance that some combination of hashes will match the hash some other trait is n!/2^32 (for a 32-bit hash), which is ...


0

Not sure if this will work but I did a few tests and it seems to be fine. Define your enum like this : enum Directions { Up = 1, Down = ~Up, Right = 2, Left = ~Right }; Now you can a check about the current direction and next direction like this if(currDit == ~nextDir) { //the snake will go into itself ...


0

I know, it's a bit late but try boundingSphere.Center += entityPosition; before you return your sphere in CreateBoundingSphereForModel


3

Your problem is that you're trying to compare floating-point numbers for equality. The matrix inversion and multiplication, however, will inevitable introduce slight rounding errors that will make the numbers in the two matrices not exactly equal. Still, let's compare your debug printouts side by side. I've deleted the M1 and TR rows, and left just the ...


4

As Phillipp said, HashCodes do not guarantee uniqueness. In fact they pretty much don't guarantee anything. What you seem to want to do is to generate a unique number from a unique string. How to do that generically is answered in this question: How can I generate a GUID for a string? If you just want a unique identifier which is allowed to differ even for ...


27

Hash codes are never guaranteed to be unique. Also, you do not get a guarantee that adding hash-codes gets you an unique value which does not collide with a different combination of hash-codes. The usual solution to identify a combination of on/off flags is to use a bitfield. Assign manual integer values to your enum which are all powers of two: [Flags] ...


0

I'm assuming guess variable will have a text from InputField, if so you could use this line to split the string from checking to NewLine string[] guessWords = guess.Split('\n'); and then retrieving the specific word from the guessWords array like if (string.Equals(guessWords[0], code)) { Debug.Log("code 1 correct"); } else { Debug.Log("code 1 ...


1

I don't really understand your question to be honest but I think you're trying to figure out how to find the world matrix of a child node. In other words, you essentially have a scene graph with parent nodes and child nodes and you want to know how to calculate the world matrix of one of the children. Essentially it's a two step process. First you need to ...


-1

GameObject bulletInstance = Instantiate(bullet, transform.position, player.transform.rotation) as GameObject; bulletInstance.GetComponent<Rigidbody2D> ().velocity = transform.right * bulletSpeed;


1

The main problem here is that you don't have some sort of game loop. You record the keypress, but not the continious keydown (thus, if the letter 'A' is pressed for 10 seconds, only 1 event is triggered). Now this could be expected behaviour (only move one square one keypress at a time). With a gameloop you can have the object moving independant of the ...


0

If you wanted to know if the user held CTRL, SHIFT or ALT down whilst pressing another button then you could check the Modifiers: if(e.KeyCode == Keys.Up && e.Modifiers == Keys.Control) Debug.WriteLine("Up and Ctrl"); But your question is about right and down, if the user presses both at the same time one will trigger ...


0

When you use else you are preventing two if blocks to be evaluated at the same time. For your code you should use else only for the opposing axes, for example: if (e.KeyCode == Keys.Right) x += 1; else if (e.KeyCode == Keys.Left) x -= 1; if (e.KeyCode == Keys.Up) y -= 1; else if (e.KeyCode == Keys.Down) y += 1; if (e.KeyCode == Keys.Escape) Close();


-1

Why are you changing the position of the camera using transform.position += newTargetOffset - targetOffset; ? While panning, you should only rotate the camera. Also, what is targetOffset?


1

The other approach I've seen is to use a generate & test (aka trial & error) approach. Basically, a rule is selected & if the result would be valid, it is applied. If the result is invalid (I.E. overlapping rooms) usually it is discarded & a new rule is attempted. Other times, it may be possible to modify the results. In a sense, both of ...


2

Most instances I've seen of graph rewriting for map generation solve the collisions problem (I.E overlapping rooms) by restricting the graph nodes to regular, modular components. For instance, this example, taken from Procedural generation of dungeons uses rooms that are laid out on regular intervals:


0

You could add a trigger to the plane and then write a script to check, if an object with a tag "Player" is inside it (don't forget to set the players tag to "Player") . Make the speed variable in the player movement script to public so that other scripts can access it. Then when the player is in trigger set the speed variable higher, and when ever the player ...


0

you can use System.Serializable using UnityEngine; using System.Collections; public class grouping: MonoBehaviour { [System.Serializable] public class class1{ public enum myEnum {one,two,three}; public myEnum[] row; public string name; public int price; } public class1[] array; }


7

Having a value deplete over time can be done a few different ways. The simplest is likely using the update method to deplete at a constant rate. public float myValue = 10; // the total public float subractPerSecond = 1; // remove this every second public void Update() { //subtract at a constant rate myValue -= subtractPerSecond * Time.deltaTime; } ...


2

You can do something like this. It looks monumentally stupid (Unity has an awesome editor, why not use it?), but just in case you do have a good reason: You can't create scenes programmatically, but you can have an empty scene and add whatever you need programmatically. Using prefabs is probably not the best idea: prefabs are usually loaded only when ...


0

Are you calling FixedUpdate() or defining it? I think you should delete the opening brace after FixedUpdate(), and add a ; directly after FixedUpdate(). In other words, if your intent is to call the method, you should write FixedUpdate (); If your intent is to define a new method called FixedUpdate (), you should write void FixedUpdate(){ //code// ...


-1

You just change [PUN] to [PunRPC].


0

You have several options: Create a variable public GameObject TreeGroup in your DisableTrees behavior. The variable will now appear as a field in the inspector of DisableTrees. You can then drag the TreeGroup game object to it. Use GameObject.Find to find the tree group by its name. But keep in mind that this method is very slow on larger scene graphs. Add ...


2

Based on your description, I would make each card effect verb a class & then use composition to combine them into cards as needed. E.G. the draw cards action would be an effect class, its parameters might be # of cards, source drawn from & type of draw. Damage player might be another effect class & its parameters might be amount & ...


0

It could be Constructive Solid Geometry indeed. But they said in the store page it is a voxel-based world (unless they lied of course). Which is basically pixels in 3D (or i could say cubes in the case of minecraft, and others). In this case, it seems to me they smooth out those voxels to achieve that organic looks. Something like Castle Story which you can ...


2

Solved by reversing the indices for the problematic bottom face. Indices are now: Indices = new int[] { // front face 0, 1, 2, 2, 3, 0, // top face 3, 2, 6, 6, 7, 3, // back face 7, 6, 5, 5, 4, 7, // left face 4, 0, 3, 3, 7, 4, // bottom face 0, 4, 5, 5, 1, 0, // Back to ...


0

I've been working on a similar project based in Zero-G in which I need all game objects to generate and react to both gravitational and electromagnetic force based on their volume, density, mass, energy and conductivity. I'm relatively new to scripting, but I've made a few modifications to the script above to make it work for me in a 3D space (as far as ...


2

In the data you send with the message, you include information about what the purpose of the message is. Not just the relevant data. For example, you'd create an enumerate of possible message types: enum MessageType { MovePlayer, AttackPlayer, ChatToPlayer, Disconnect, }; When you send some data related to moving a player, you don't just send the ...


1

Converting between a Quaternion (transform.rotation) and an Euler angle triplet is not possible the way you wrote it. See the API http://docs.unity3d.com/ScriptReference/Transform-rotation.html for the meaning of the transform.rotation property. If you want to double check or verify that your Euler angles are correct, you can convert them to a quaternion ...


0

I wiped up a script that does what you want I think. Check it out. public class OrientTowardsDirection : MonoBehaviour { Vector3 prevLoc = Vector3.zero; Vector3 moveDirection = Vector3.zero; public Transform cube; private float MaxTurnSpeed = 100; void Start() { //code for simply moving the camera was here but was removed as per ...



Top 50 recent answers are included