Randomly generating obstacles in Unity [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Currently I'm creating game in which whole level is generated when player moves. I have working script for that, but now I want to generate obstacles in front of the player. My idea is to add empty object to my terrain segment(these are dynamically generated) and then instantiate random obstacles at their position at runtime. The only thing i don't know is how this script should pick random empty object(empty object is child of the segment) to instantiate this obstacle? The script which generates level is attached to player.

This gives you an idea of how to do it.
Spawns a random game object to one of the empty gameobjects around your terrain.
//Add the random objects in the inspector
public List<GameObject> gameObjects;
//Spawn points around the terrain
public List<Transform> emptyTransforms;
foreach(var item in emptyTransforms){
var objToSpawn = gameObjects[Random.Range(0, gameObjects.Length)];
var objSpawned = Instantiate(objToSpawn, emptyTransforms[Random.Range(0, emptyTransforms.Length)].position, Quaternion.identity);
objSpawned.transform.SetParent(newParent);
}

Use the Random.Range function to calculate random values. Then make the values within bounds of where you want obstacles to spawn and create a Vector3 with those values. Now the obstacles are spawned in a random area.

If you have a fixed list of possible positions, just store them in a public variable in the script. If your empty gameobjects are the positions, create a public GameObject[] positions;, populate it and use something like Random.Range to pick one of it.
Also I suggest you have a look at item pooling and don't instantiate new objects all the time (and probably destroy "old" ones). That is better for your performance.

Related

How to add a score text in a unity game? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 days ago.
Improve this question
I am currently working on a Unity Game , so basically it is a school project ,but next level . I build an arena , and two balls on it . The balls have to push each other on the arena and the last one standing on the arena wins . But i am stuck with my code ... i want that the first ball who falls down , immediatly to respawn the both balls in the respawn point , and then the score to go higher with 1 point every time one of the balls falls down. It seems so difficult for me and i am struggling with it. If anybody cand save me i will be happy ! Thanks !! <3
I tried a lot of methods but the only bug i have is that if a ball falls down , the game automatically spawns a lot of balls in the place the ball has fallen . And after 1 minute my map is full of balls .... and i do not want that to happen. So please i am asking for your help !
Assuming you have two ball objects, you could make a Player.cs script, that is applied to the two balls. The script could have the following fields:
The other player object
The player's score object
The position on which the player is to respawn
Then, inside your Update method, you could check whether the ball has fallen below a certain Y value. If so, it can get the other player's score object, and increase the number on it. After that, just set the position of the current object to the specified position.
Currently, I can't provide any specific APIs or code, since I don't have Unity installed, but I'll be sure to edit the answer as soon as it installs.
For what you want to do, my advice is to create an empty GameObject, add a collider, and activate the isTriggered property. To make a previously prepared vector2 or vector3 the position of the players when one of the players is triggered and to set all the forces affecting the rigidbody of the players to 0 . You can also figure out which player to score according to the tag of the triggered GameObject every time it is triggered.
I'm not a professional but I hope it helped.

How do i make a child game object affect the parent? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm making a game like Super Mario. The player is a circle and the enemy is a square that goes left and right after hitting a wall. If the enemy touches the player on the sides the player dies so to survive the player has to jump on the head of the enemy to kill it.
I separated the enemy to two parts body and head (body is parent and the head is a child). Now killing the player is already done but I'm stuck at killing the enemy. How do I kill (destroy) the parent when the player touches the head (child)?
You should be able to add a collider to the child/head, BoxCollider2D for example. Then you can handle the Collision event and call a method on the parent object.
It would look something like:
// In the child/head
void OnCollisionEnter2D(Collision2D col)
{
var goomba = transform.Parent.GetComponent<Goomba>();
goomba.Kill();
}
This is probably the simplest way, but I think there is a way to propagate collisions to the parent, can't remember though.
Edit:
Apparently,
If you're using a Rigidbody with the character, you can get this out of the box. :)
Found that in this answer: https://gamedev.stackexchange.com/questions/151670/unity-how-to-detect-collision-occuring-on-child-object-from-a-parent-script

Save current state at run time [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
I have built a game in Unity3D where the player can place models, rotate them and scale them.
Now what I want is to add an option to save the current state.
What I need is to save for every model is simply the scale, position, rotation and the model's mesh.
I know how to save all the properties except for the Mesh. I planned to save the data in JSON file but it can't save the Mesh nor the GameObject.
I have reference to all the instanced models so there is no need to find them.
Edit:
To sum up, I am looking for a way to save the selected models so I would know which models to load when the user wants to load the file.
But I can't serialize the GameObject nor the mesh, and therefore using JSON or binary file doesn't work. All I have been able to save so far is the position, rotation and scale of each models.
How can I save the selected models¿
The mesh is not generated on runtime right? He has a collection of models that you have as prefabs that he can place. So just save as a string or enum which model each position has, then to load the saved scene you instantiate the correct prefab of the model according to the save at the saved position and rotation.
public class PrefabHolder : MonoBehaviour //assign all necessary prefabs here, also used for logging on mobileDebugText
{
public static PrefabHolder instance;
public GameObject prefabXYZ;
private void Awake()
{
instance = this;
}
Acess any model you want from any script with Prefabholder.instance.prefabXYZ

Best way to attach a string variable onto a moving texture, and have it drawn? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Currently working on a game (similar to a space defender game with random enemy spawns) and need the enemies to have a random.next attached to label the enemy.
However I'm having trouble figuring a way to do so, since the enemy is also moving on its Y axis at a speed of 4; "position.Y = position.Y + 4. Also whats making this difficult is the fact that i need my bullets that collide with the enemy to somehow also delete the label, and refreshing the label with a new random number on the next spawned enemy.
Sadly I'm currently away from my work station, so i can't present any code, but as of this moment, the way I'm managing my enemy spawning is through a list, it loops to check if the list has < 5 in its .count, and if so, add another enemy with a random Y, and X position. My method of reseting an enemy after it reaches the bottom of the screen, is that once it hits Y == 950, the enemy is set to "isvisible = false", the list is then looped in a check ,where if an enemy is found with isvisible = false, it is removed from the list.
You can say that the list is slightly useless, but I'm currently using it to limit the amount of spawns on screen.
see Drawing Text with a Sprite, I would just create a Label string property in your enemy class set it to whatever you need to in its constructor, and then when you loop over your list it to draw it also draw the text that you assign when you create the enemy at whatever offset from the sprites location.

Moving the player tile by tile smoothly in Unity [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to make the player move by a specific amount (1 tile/unit in this case) and then stop him. Just like in this video:
http://www.youtube.com/watch?v=DotOAbNngc4
By pressing a key the player should move smoothly to the next tile, than stops and so on.
How can I archieve this? (in C#)
You could take a look at GridMove which is from UnifyWiki.
You would probably get more of a response if you posted this question to answers.unity3d.com or forum.unity3d.com as these forums are specifically target towards Unity3d.
Here's what I would do: you give your player class a target field, e.g. a 2d vector. This will either contain the current position of the player (when initializing or when the target has been reached) or the target that you want to move the player to.
Now in your update you do this:
if target equals your current position then check user input for movement.
if user requests movement set target to position adjacent to current position.
else slowly move player towards target.
You may want to create a field for the current position of the player besides the normal position in GameObject. You could then update GameObject.position in the update-cylce by a small delta until the positions of GameObject.position and target have the (roughly! use epsilon when comparing) same value.
Some hints
Vector2.MoveTowards can help you
To check if the target and position are "close enough" you could subtract one from the other and look at the resulting vector's magnitude.
Beware of overshooting when you move towards the target. If your movement distance is more then or equal to the distance between position and target, just set the position to the target.

Categories