Unity 2D - How to place obstacle in Running game? - unity3d

I finish making running game. But I placed obstacle like this :
This way makes phone frozen often. So I make a prefeb is made of obstacle object like this :
I want to use function SetActive(bool) to appear/disappear obstacle. Finally, How it works is My runner is at a standstill and background, obstacle, coin, juwel is moved. But My creator script has a big problem.
My Prolem :
As you see, This code create blank(red box) between two obstacle prefebs. I can't find problem. Plz help me...
Creator Script :
using UnityEngine;
using System.Collections;
public class CsPartCreator : MonoBehaviour {
int count = 1;
float timer = 0.0f;
// Use this for initialization
void Start () {
for (int i = 1; i < 26; i++)//set Deactive all object excluding first object (Because This object is watched at first.)
transform.GetChild(i).gameObject.SetActive(false);
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
if (timer > 5f && count < 23)//every 5 seconds and count is smaller than the number of prefeb
{
transform.GetChild(count).gameObject.SetActive(true);//
count++;
timer = 0;
}
}
}
This is my Hierarchy :

I think , That missing prefab's Z value is more negative than background , Or you have used a sorting layer which is set to be behind background.
And Check if all obstacle gameobjects correctly orderered in Hirachy Part0, Part1, Part2, Part3 like that. Let me know if those are in correct order
Please check that :)
using UnityEngine;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
public class CsPartCreator : MonoBehaviour
{
private GameObject player;
float timer = 0.0f;
private List<GameObject> obstacles;
void Awake()
{
player = GameObject.FindWithTag("Player");
obstacles = new List<GameObject>();
}
// Use this for initialization
void Start()
{
for (int i = 0; i < 26; i++)//set Deactive all object excluding first object (Because This object is watched at first.)
{
obstacles.Add(transform.GetChild(i).gameObject);
transform.GetChild(i).gameObject.SetActive(false);
}
}
// Update is called once per frame
void Update()
{
if (obstacles == null || obstacles.Count() == 0)
return;
var toActivate = obstacles.Where(x => x.transform.position.x > transform.position.x && x.transform.position.x < transform.position.x + 10).ToList(); // +10 is the range
foreach (GameObject go in toActivate)
{
go.SetActive(true);
obstacles.Remove(go);
}
}
}
Set player gameobject to "Player" tag as in image below

Related

Unity using prefabs in child nodelist results in doubling the elements I want

I am new to Unity and have followed a tutorial to establish a way of building a path for a boardgame using this code:
Route.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Route : MonoBehaviour
{
Transform[] childObjects;
public List<Transform> childNodeList = new List<Transform>();
// Start is called before the first frame update
void Start()
{
FillNodes();
}
#if UNITY_EDITOR
void OnDrawGizmos()
{
Gizmos.color = Color.green;
FillNodes();
for (int i = 0; i < childNodeList.Count; i++)
{
Vector3 currentPos = childNodeList[i].position;
if (i > 0)
{
Vector3 prevPos = childNodeList[i - 1].position;
Gizmos.DrawLine(prevPos, currentPos);
}
}
}
#endif
void FillNodes()
{
childNodeList.Clear();
childObjects = GetComponentsInChildren<Transform>();
foreach(Transform child in childObjects)
{
if(child != this.transform)
{
childNodeList.Add(child);
}
}
}
}
The script is attached to my Board GameObject which itself contains a set of duplicate prefabs:
which results in 16 elements in my child node list:
Thing is, this means that when I roll the dice, my pawn moves only half the amount of spaces it should because each space has 2 elements in the same location.
I only want to see 8 elements in my child node list, one element per tile. In other words, I only want to list the parent named Tile (x) and ignore the Cube-visual.
I know enough to know that the FillNodes() function needs to be modified, but I don't know how or what to change.
thank you.
The easiest way is to add the Tile tag to the tile prefab, and add the corresponding condition. The condition in the FillNodes function will look like this:
if(child.tag == "Tile")
{
childNodeList.Add(child);
}
I don't have a chance to check the code right now, so tell me if the solution doesn't work.

Display 10 separate Meshes one by one with looping behavior to create "Animation" for empty GameObject?

I have 10 meshes that are each basically freeze-"frames" of an animation of a whale swimming.
If I were to loop through displaying these meshes then it would create a pseudo-animation, which is what I want.
How can this be achieved? Currently I have GameObject AnimTest, and I've placed the 10 mesh .obj files as children to it. I've started with this code on a script changeMeshes for the GameObject:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class changeMeshes : MonoBehaviour
{
//Create array of the meshes
public float[] currentMesh;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < 10; i++)
{
currentMesh[i].gameObject.GetComponent<MeshRenderer>().enabled = true;
}
}
}
This is obviously very wrong since I'm new and confused, so I'm getting errors with script like float does not contain a definition for gameObject. Can anyone help lead me down the correct path?
Edit: Here's my better try at it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class changeMeshes : MonoBehaviour
{
//Create array of the meshes
public GameObject[] whaleMeshes;
void FindWhales()
{
whaleMeshes = GameObject.FindGameObjectsWithTag("WhaleMesh");
print(whaleMeshes.Length);
}
void CycleWhales()
{
for (int i = 0; i < whaleMeshes.Length; i++)
{
if (i > 0)
{
whaleMeshes[i - 1].gameObject.GetComponentInChildren<MeshRenderer>().enabled = false;
}
whaleMeshes[i].gameObject.GetComponentInChildren<MeshRenderer>().enabled = true;
}
}
// Start is called before the first frame update
void Start()
{
FindWhales();
}
// Update is called once per frame
void Update()
{
CycleWhales();
}
}
The result now is that only the last Mesh is permanently rendered, rather than the script cycling through the meshes on a loop. How do I get the meshes to loop?
Well .. a float has no property .gameObject .. not even Mesh would
Since you say the objects are children of this component you can simply iterate through them.
And then I would simply activate and deactivate the entire GameObject.
Sounds like what you would want to do is something like
public class changeMeshes : MonoBehaviour
{
// How long should one mesh be visible before switching to the next one
[SerializeField] private float timePerMesh = 0.2f;
public UnityEvent whenDone;
private IEnumerator Start()
{
// Get amount of direct children of this object
var childCount = transform.childCount;
// Get the first child
var currentChild = transform.GetChild(0);
// Iterate through all direct children
foreach(Transform child in transform)
{
// Set all objects except the first child to inactive
child.gameObject.SetActive(child == currentChild);
}
// Iterate though the rest of direct children
for(var i = 1; i < childCount; i++)
{
// Wait for timePerMesh seconds
yield return new WaitForSeconds(timePerMesh);
// Set the current child to inactive
currentChild.gameObject.SetActive(false);
// Get the next child
currentChild = transform.GetChild(i);
// Set this one to active
currentChild.gameObject.SetActive(true);
}
// Optionally do something when done like e.g. destroy this GameObject etc
yield return new WaitForSeconds(timePerMesh);
whenDone.Invoke();
}
}

Destroying Prefabs Object After Spawn Using collision

I currently have some objects spawning from a prefab and am attempting to destroy only one of the spawned items of that prefab. I was searching online and I found tons of different examples but I have been unable to get any of them to work. I tried setting up an instance of the Instantiate and destroying that instance but I am unable to get it to work. The spawn/collision script is attached to the main camera if that matters. Collision with other items in my game work and the prefab does have a box collider set to isTrigger. Again, I know there are plenty of examples explaining this etc, but I can't get it to work and maybe I am not understanding what I actually should be doing.
Spawner code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class bloodVialSpawner : MonoBehaviour
{
public GameObject vialOfBlood;
private GameObject vialss;
private int hunger =10;
public int numOfVials;
public int minSpawnRange, maxSpawnRange;
public int minSpawnRange2, maxSpawnRange2;
// Start is called before the first frame update
float timeSpawns = 2;
List<GameObject> vialsInstantiated = new List<GameObject>();
void Start()
{
StartCoroutine(becomeHungry());
InvokeRepeating("SpawnVials", timeSpawns, timeSpawns);
}
private void Update()
{
if (hunger == -1)
{
Debug.Log("sigh");
}
}
void SpawnVials()
{
for (int i = 0; i < numOfVials; i++)
{
vialsInstantiated.Add(Instantiate(vialOfBlood, SpawnPosition(), Quaternion.identity) as GameObject);
}
}
Vector3 SpawnPosition()
{
int x, y, z;
y = 59;
x= UnityEngine.Random.Range(minSpawnRange, maxSpawnRange);
z = UnityEngine.Random.Range(minSpawnRange2, maxSpawnRange2);
return new Vector3(x, y, z);
}
IEnumerator becomeHungry()
{
while (true)
{
hunger -= 1;
yield return new WaitForSeconds(1);
Debug.Log(hunger);
}
}
}
Spawner Script is on the Main Camera. Player used is the First Person Player Unity provides.
Code for destroying spawned object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class destroyVial : MonoBehaviour
{
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "vials")
{
Destroy(col.gameObject);
Debug.Log("yell");
}
}
}
Destroy code is on prefab. Note prefab is not in hierarchy as it should not be.
Firstly,
I see that you're spawning things in a for-loop and overwriting the vialss variable every time:
for (int i = 0; i < numOfVials; i++)
{
vialss = Instantiate(vialOfBlood,
SpawnPosition(),
Quaternion.identity) as GameObject;
}
And then, on collision, you're Destroying vialss, which in this case will be the latest spawned object. And if you collide with anything after 1 collision, vialss will already be deleted and probably throw an exception. Maybe that's fine in your game, but the logic looks a bit flawed.
Also, I'm assuming you want to destroy the object you're colliding with? Does something like this not work?
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "vials")
{
Destroy(col.gameObject); // <== Remove colliding object
Debug.Log("yell");
}
}
If you, for some unrelated reason, need a list of all your spawned vials, maybe you'd like to convert that to a list instead:
List<GameObject> spawnedVials = new List<GameObject>();
void SpawnVials()
{
for (int i = 0; i < numOfVials; i++)
{
var vial = Instantiate<GameObject>(vialOfBlood,
SpawnPosition(),
Quaternion.identity)
spawnedVials.Add(vial);
}
}
Finally,
make sure that the collision detection is working. You're saying that the script is attached to your camera. please make sure you have a Collider on the camera. But you're saying that other colliders are working, so I'm guessing you have this under control.
I'd guess your issue lies in the flawed logic I initially described.
OnTriggerEnter needs to be on a script attached to the object it's colliding with, or the vial itself. It can't be on the main camera as OnTriggerEnter will never get called.
I would recommend you to keep scripts to one job, you should split that script into a Spawn script and a Collider script. And create a empty GameObject with the sole purpose of spawning prefabs.
Also there's some errors in your code:
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "vials")
{
Destroy(col.gameObject); // Destroy the gameObject you're colliding with
Debug.Log("yell");
}
}
Also the variable vialss isn't doing what you're expecting, vialss is only referencing to the last instantiated vial, so better save all vials in a List:
List<GameObject> vialsInstantiated = new List<GameObject>();
And then:
void SpawnVials()
{
for (int i = 0; i < numOfVials; i++)
{
vialsInstantiated.Add(Instantiate(vialOfBlood, SpawnPosition(), Quaternion.identity) as GameObject);
}
}

Counter is not decremented in Unity 3D

Basically this game is a brick breaker in which I will be giving a quick basic background about it. The game will spawn 10 bricks at random locations, and if the all hits the brick
once, the brick is destroyed. If all 10 bricks are destroyed then the user will be taken to the other scene (gamewon).
My main problem is that when I destroy all the bricks, I am not being redirected to the scene. However if I set the numberOfBricks to 1, it will work.. I can't really understand what's happening.
Thanks alot guys!
-HurpaDurpa
using UnityEngine;
using System.Collections;
public class BrickScript : MonoBehaviour {
public int position_x = 0;
public int position_y = 0;
public GameObject Brick;
public int brickDamage = 0;
public int numberOfBricks = 10;
// Use this for initialization
void Start () {
Brick = GameObject.Find ("Brick");
position_x = Random.Range (-6, 6);
position_y = Random.Range(-1, 4);
transform.position = new Vector3 (position_x, position_y, transform.position.z);
}
// Update is called once per frame
void Update () {
if (brickDamage == 1) { //destroy brick on 1 hit
numberOfBricks--;
Object.Destroy (gameObject); //so it will destroy the hit object
}
if (numberOfBricks == 0) {
Application.LoadLevel (1);
numberOfBricks = 10;//Variable resetted
}
}
void OnCollisionEnter(Collision collision)
{
brickDamage++;
}
}
This is happening because you seem to have attached the script above to each brick.
So what is really happening is when any one of your bricks get hit, the numberOfBricks reduces by 1, resulting the value going to 9. This will happen to each brick.
Rather, what you need to do is to keep this numberOfBricks counter in another script. In the OnCollisionEnter portion of your code, you should reduce the numberOfBricks variable in the other script by 1.
Do that and you'll see the result you expect.

How can I get my trampoline script to work?

So I'm making a puzzle based game in which the player must use Trampolines which detect when the player jumps on top of it and bounces them up x amount of height.
However currently at the moment my code isn't working and stating error that
"Cannot modify the return value of 'UnityEngine.Rigidbody.velocity' because it is not a variable."
Wondering if any you guys know what could be done to fix this code and allow it to work to the purpose its needed for.
using UnityEngine;
using System.Collections;
public class small_trampoline_bounce : MonoBehaviour {
bool willBounce = false;
float bounceHeight = 10;
public Transform Player;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (willBounce)
{
Player.rigidbody.velocity.y = 0;
Player.rigidbody.AddForce (0, bounceHeight, 0, ForceMode.Impulse);
willBounce = false;
}
}
void OnCollisionEnter (Collision other)
{
if (other.gameObject.name == "Player")
{
willBounce = true;
}
}
}
To address the specific error you are getting - since velocity is a vector, you can't modify the returned vector and expect the stored vector to change. If you really want to set the velocity directly, you need to set the whole vector, not just a portion:
Player.rigidbody.velocity = new Vector3(0, 0, 0);
If you want to maintain previous x and z velocity, you can do this:
vector3 velocity = Player.rigidbody.velocity;
Player.rigidbody.velocity = new Vector3(velocity.x, 0, velocity.z);