I'm working on a version of Conway's Game of Life in Unity, and here is my setup for making the grid:
I've created a prefab of an individual cell responding to mouse click that will be the basis for creating the cell grid. I have a Empty GameObject to act as the controller to create the grid. I'm putting it in the code for the controller like so, pointing my prefab to the field:
[SerializeField]
private GameObject Cell;
private Camera _camera;
My idea was to get the dimensions of the Cell and instantiate it into a grid, with _camera pointing to the Main camera to get boundaries. However, I'm not sure how to get the height/width from GameObject. What's the best way to find this out?
I don't know if you found the answer, but the most common way is using Collider (if you have one, but mouse click needs it) or Renderer (if you have a mesh) by using:
GetComponent<Collider>().bounds.size
GetComponent<Renderer>().bounds.size
Game of life is very nice, I've written my paper for bachelor seminar about it. Have fun!
Related
I'm trying to make a game where you're a boy and you can transform into a deer.
I don't know how to make the player transform into a deer when pressing a button. Can someone please tell me how to make the player transform into a deer when pressing a button and how to make the deer transform back into the player when pressing the button again?
There are 2 ways depending on what you want exactly.
1.Transformation without an animation, you can do this by having 2 separate models
public GameObject boy;
public GameObject deer;
//Give both objects the same tag and don't add anything else under the
tag in order for this to work
private Transform currentPosition;
private void Start()
{
deer.SetActive(false);
}
public void TransformButton()
{
currentPosition = GameObject.FindObjectWithTag(LayerMask.NameToLayer("TransformationTag"));
//Unity doesn't pick up disabled GameObjects so it will pick up only the
//active state (the active object)
boy.SetActive(!activeInHierarchy);
deer.SetActive(!activeInHierarchy);
GameObject.FindObjectWithTag(LayerMask.NameToLayer("TransformationTag")).getComponent<Transform>() = currentPosition;
}
I recommend this way if your deer and boy have different scripts
you could also play an animation on click (apply this animation to the active character) and when it is done (below is how to check if it is done) you can trigger the code above
https://answers.unity.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html
I am pretty sure there are other better ways but this is the way I do it
The other way which is probably easier if you are good at animating is to just trigger an animation using the animation bools when the button is clicked but I haven't tried this way because I'm not that good at animation. Instead I use the first way (usually for switching skins in a shop for example) but I never did animations that completely change the size ratio so much so I don't know how well would it work (I usually do 1:1 transformations like humanoid to humanoid or tank to tank so you will have to experiment a bit)
I dont think that this is possible in unity. Maybe there are some Assets that offer something like this.
But you could create a similar effect with Blender.
Check out Morphing Shape Animations.
There are some cool tutorials out there Morphing Shape Animations in Blender
I have a prefab of a submarine I made in blender. In this submarine there is a "altitude" screen which is basically a cube I scaled and painted black.
I can easly get the altitude of my submarine by doing sub.transform.position.y.
Now I want to put a "Text" over the screen with the altitude I am getting.
My question is : What is the best way to do it ?
In an Ideal world I would have a function like
TextObject putTextOnTop(GameObject the_panel)
that would generate a textObject that I would then update each frame with my value.
Thanks
There are multiple solutions for this.
There is an old 3D Text component in unity. This can do what you want. The Problem with this is, that you have only limited styling options.
A Better way is to use a WorldSpaceCanvas.
If you use this option, you have all TextMeshPro styling options.
Here is a video:
https://www.youtube.com/watch?v=GuWEXBeHEy8
The video is not very good, but i can't find a better one.
If you have any Questions left, feel free to ask
You can put the Text GameObject to be the child of the screen (Cube GameObject), there are 2 ways for this:
Set directly in Hierarchy
Create a prefab TextObject and save it in Resources with path: Resources/UI/TextObject. Then in the code, instance and add it as a child of the Cube using the following code:
GameObject textObject = Instantiate(Resources.Load("/UI/TextObject") as GameObject);
textObject.transform.SetParent(cube.transform, false);
Then, in the Update function you add the following code:
void Update()
{
TextObject.GetComponent<Text>().text = sub.transform.position.y.ToString();
}
Hope it can help you!
I want to add Vertical scrolling to a Canvas using a script.
I'm adding N number of GameObject to the Canvas and placing them one after the other.
Now I want to add a vertical scroll to the canvas but I'm not sure how to proceed.
Please Help me out.
Thank You.
GameObject instance = Instantiate (Resources.Load ("Object", typeof (GameObject))) as GameObject;
instance.transform.SetParent (GameObject.FindGameObjectWithTag ("Canvas").transform, true);
I guess what you want is a ScrollView object which scrolls vertically.
So according to Unity's documentation the best way to create the UI elements runtime is by creating a Prefab of that UI Element, set the properties according to your need.
So in your case You need to create a prefab of ScrollView which has the Horizontal set as false and Vertical set as True in the ScrollRect component and then make a prefab of that object and use it in your script to instantiate it.
public GameObject scrollPrefab;
void SpawnVerticalScrollView()
{
GameObject instance = Instantiate (scrollPrefab,GameObject.FindGameObjectWithTag ("Canvas").transform) as GameObject;
}
And as you can see I have also assigned the parent to the instantiated object right away in the same line.
I recommend you don't use the function Resources.Load, because the Unity says: "Don't use it. Use of the Resources folder makes fine-grained memory management more difficult. So you need to have a created prefab with your scrolling component and add this prefab link to your main script that controlling UI objects in the Canvas by the editor. When you need to use the scrolling prefab just use "Instantiate" function. Attache your controlling script to the Canvas object. For example:
public GameObject scrollingPrefab;
public void CreatePrefab(){
GameObject currentPrefab = Instantiate(scrollingPrefab, gameObject.transform);
}
Also, the bad way of creating objects is using the function FindGameObjectWithTag and other "Find".
I have a 2D polygon that is defined by a edgeCollider. Is there a way to hide everything that is outside of the shape and show only what is inside?
I tried using skyboxes and lights. I thought about creating a mask(but i dont know how to create such a mask).
Is there a way to only show what is inside the shape defined by edge collider?
What about using the colliders trigger events?
Set this polygon as a trigger in the edgeCollider2D component in the inspector. Then you can use the collider OnEnterTrigger2D.
Your gameobjects are all disabled until this edgeCollider collides with in. Then disable the gameobject OnExitTrigger2D.
If you wanted to limit it to a certain number of object only. You would set a layer to only hide/show these object.
void OnTriggerEnter2D(Collider2D other) {
if(other.gameObject.layer == "hiddenObject"){
other.gameObject.enable = true;
}
}
Then the reverse on the OnTriggerExit2D.
I'm not sure the effect that you are aiming for. So another solution could be a postprocessing shader.
I can only give a high level description on this however.
You would take the final screen image texture, and the current position of the polygon and then add the pixels from the screen texture to the polygon texture and output this texture. (this shader has to exists already).
But you want the inverse of these.
https://docs.unity3d.com/Manual/shader-TransparentCutoutFamily.html
You're wanting to keep what's in the hole and get rid of what's outside of it?
You can download the built in shaders to edit them here
https://unity3d.com/get-unity/download/archive
Just find your version of unity and select builtin shaders from the drop down.
Edit: "SPOTLIGHT!", try this but with your custom shape
http://www.shaderslab.com/demo-49---spotlight.html
I am working on a 2D game and the text prefab that I instatiate doesn't position itself over the gameObject clicked (which is the goal). I've set the Canvas as parent of the prefab via script after spawning it and it doesnt change position.
// creating hit text
GameObject canvas = GameObject.Find("Canvas");
GameObject hit = (GameObject)Instantiate(hitText, transform.position,Quaternion.identity);
hit.transform.SetParent(canvas.transform, false);
hit.transform.position = transform.position;
P.S: this sample code worked with a text made with the Unity Text Editor. Does that mean TexhMesh Pro won't support this function?
For anyone who might have this problem please understand that I made sure that the instantiated text is a child object of the canvas. The problem was that my text has been changed on start by an attached animation. The script above was working fine but the animation was changing it.
You can easely fix this problem by setting the parent of the text to an empty GameObject, which then serves as a container for the text, allowing the text to change its position relatively to it's parent only.