- Home /
How do I generate obstacles in 2D side scroller?
I am building an endless 2D side scroller and I have created an object that will end the game when the player collides with it. The question I have, however, is how to have obstacles generate with a random frequency, and come at the player on one axis. Here is my code so far
using UnityEngine;
using System.Collections;
public class obstacleScript : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
float moveSpeed = 2.5f;
public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
// Update is called once per frame
void Update()
{
//makes obstacle move towards player
transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
}
//Detects if player hits an obstacle
void OnTriggerEnter2D(Collider2D other)
{
//If a gameObject with the tag "Player" enters this trigger, load a scene.
if (other.gameObject.CompareTag("Player"))
{
print("object hit");
Application.LoadLevel("Game Over Scene");
}
}
}
Currently, the error I am getting is that I have to attach a body to my instantiate line.
Answer by Happy-Zomby · Sep 23, 2016 at 06:08 PM
you can instantiate in the declaration part... what you want to do is start a coRoutine which will wait a random number of seconds and then instantiate the obstacle there...
For coroutine and waiting see here: https://docs.unity3d.com/ScriptReference/WaitForSeconds.html
for instantiate see here: https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
also you seem to be missing 2d and vector3... this may cause issues I believe - i work in 3d but i guess 2d should use vector2 not 3.
hope that helps,
I cannot seem to instantiate the obstacle to begin with. I am trying to get that down first, and I have already checked the instantiate doc before asking this question, and I have followed what it says, and it still does not work. I should add that this is my first game on Unity, so forgive me if I am missing something.
as Badeye mentioned, you need to have your instantiate in a void... not in the declaration. if you look at your code it is not in a void + I believe there are some issues in it If you are not sure what a void is - recommend you look at some tutorials out there to first grasp that concept.
Answer by Badeye · Sep 23, 2016 at 06:43 PM
Instantiate in a void and make sure the GameObject you try to instantiate is not null (create a public GameObject and select a Prefab in the Inspector, maybe perform a null check). Also make sure the wall-object is set to trigger, otherwise OnTriggerEnter will not get called.
Just to let you know, I am a beginner. Anyway, is this what you mean? I created a public GameObject called obstacle, and I put the instantiate event inside a void. I made a prefab, and for some reason, I cannot rename it. I also tried to use the null operator, but this is the first time I am using it. I selected "is trigger" for my obstacle already.
using UnityEngine;
using System.Collections;
public class obstacleScript : $$anonymous$$onoBehaviour
{
// Use this for initialization
void Start()
{
}
public GameObject obstacle;
public float obstacleSpeed = 4.5f;
if (GameObject.Obstacle == null)
{
print("obstacle is null");
}
public void createObstacle()
{
public static Object Instantiate(Object original, Vector2 position, Quaternion rotation);
}
// Update is called once per frame
void Update()
{
//makes obstacle move towards player
transform.Translate(Vector2.left * obstacleSpeed * Time.deltaTime);
}
//Detects if player hits an obstacle
void OnTriggerEnter2D(Collider2D other)
{
//If a gameObject with the tag "Player" enters this trigger, load a scene.
if (other.gameObject.CompareTag("Player"))
{
print("object hit");
Application.LoadLevel("Game Over Scene");
}
}
}
Your answer