Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Darckret · Mar 20 at 06:18 PM · 2d gameobstacle

problems with generation of obstacles help

Hello everyone, I have two questions to which I can't find a solution. When my obstacles are generated, it usually happens that sometimes they are generated one on top of the other, how do I make this not happen? The next question is that one of these obstacles is floating but as the map continues to be generated, the obstacle never appears floating again, it appears on the ground, how do I solve this?

This is my code

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour { public GameObject menuPrincipal; public GameObject menuGameOver;

 public float velocidad = 2;
 public GameObject col;
 public GameObject piedra1;
 public GameObject aleteo;
 public GameObject aleteo2;
 public Renderer fondo;
 public bool gameOver = false;
 public bool start = false;

 public List<GameObject> cols;
 public List<GameObject> obstaculos;
 // Start is called before the first frame update
 void Start()
 {
     // Crear Mapa
     for (int i = 0; i < 21; i++)
     {
         cols.Add(Instantiate(col, new Vector2(-8 + i, -5), Quaternion.identity));
     }

     // Crear Piedras
     obstaculos.Add(Instantiate(piedra1, new Vector2(-3, -3), Quaternion.identity));
     obstaculos.Add(Instantiate(aleteo, new Vector2(5, -3), Quaternion.identity));
     obstaculos.Add(Instantiate(aleteo2, new Vector2(14, -1.6f), Quaternion.identity));
 }

 // Update is called once per frame
 void Update()
 {
     if (start == false) 
     {
     if (Input.GetKeyDown(KeyCode.X)) 
         {
             start = true;
         }
     }

     if (start == true && gameOver == true)
     {
         menuGameOver.SetActive(true);
         if (Input.GetKeyDown(KeyCode.X))
         {
             SceneManager.LoadScene(SceneManager.GetActiveScene().name);
         }
     }

     if (start == true && gameOver == false) 
     {
         menuPrincipal.SetActive(false);
         fondo.material.mainTextureOffset = fondo.material.mainTextureOffset + new Vector2(0.1f, 0) * Time.deltaTime;

         // Mover Mapa
         for (int i = 0; i < cols.Count; i++)
         {
             if (cols[i].transform.position.x <= -10)
             {
                 cols[i].transform.position = new Vector3(10, -5, 0);
             }
             cols[i].transform.position = cols[i].transform.position + new Vector3(-1, 0, 0) * Time.deltaTime * velocidad;
         }
         // Mover Obstaculos
         for (int i = 0; i < obstaculos.Count; i++)
         {
             if (obstaculos[i].transform.position.x <= -10)
             {
                 float randomObs = Random.Range(11, 18);
                 obstaculos[i].transform.position = new Vector3(randomObs, -3, 0);
             }
             obstaculos[i].transform.position = obstaculos[i].transform.position + new Vector3(-1, 0, 0) * Time.deltaTime * velocidad;

         }
     }
 }  

}

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Caeser_21 · Mar 21 at 04:48 AM 0
Share

Hi mate, do you have a YouTube channel? If you do can you post a video of your game as it quite hard for me to understand...

avatar image Darckret Caeser_21 · Mar 21 at 12:53 PM 0
Share

hello friend, here is a small video of what happens thanks for taking the trouble, in the description of the video I explain what happens https://www.youtube.com/watch?v=MaY5XrcLfO4

avatar image Caeser_21 Darckret · Mar 21 at 01:10 PM 0
Share

Ok so I just saw the video mate... And there are a lot of things that can be changed such as the way the 'cols' is handled.
Give me some time and I will provide a full-depth answer on what can be changed and can be done better :)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Caeser_21 · Mar 22 at 09:16 AM

Ok, here is a detailed answer of what you can do better :


1.Don't move the 'piedra' and the 'aleteo', just move the Camera like :
(a) Create a new script called "CameraMove" and add it to the camera
(b) Add the following to that script

 private float Speed; //Add this to the top of the script

 void Start()
 {
      Speed = 5f;
 }
 
 void Update()
 {
      transform.position = new Vector2(transform.position.x * Speed * Time.deltaTime, transform.position.y)
 }

2.Then make the following changes to your script :

  public GameObject menuPrincipal;
  public GameObject menuGameOver;
  public float velocidad = 2;
  public GameObject piedra1;
  public GameObject aleteo;
  public GameObject aleteo2;
  public bool gameOver = false;
  public bool start = false;
  public List<GameObject> obstaculos;
  public GameObject Camera; //Asign this in the inspector
  void Start()
  {
      // Crear Piedras
      obstaculos.Add(Instantiate(piedra1, new Vector2(-3, -3), Quaternion.identity));
      obstaculos.Add(Instantiate(aleteo, new Vector2(5, -3f), Quaternion.identity));
      obstaculos.Add(Instantiate(aleteo2, new Vector2(14, -1.6f), Quaternion.identity));
  }
  // Update is called once per frame
  void Update()
  {
      if (start == false) 
      {
      if (Input.GetKeyDown(KeyCode.X)) 
          {
              start = true;
          }
      }
      if (start == true && gameOver == true)
      {
          menuGameOver.SetActive(true);
          if (Input.GetKeyDown(KeyCode.X))
          {
               SceneManager.LoadScene(SceneManager.GetActiveScene().name);
          }
      }
      if (start == true && gameOver == false) 
      {
          menuPrincipal.SetActive(false);

      for (int i = 0; i < obstaculos.Count; i++)
      {
          if (Vector3.Distance(obstaculos[i].transform.position, Camera.transform.position) >= 10)
          {
              float randomObs = Random.Range(11, 18);
              obstaculos[i].transform.position = new Vector2(Camera.transform.position.x + randomObs, obstaculos[i].transform.position.y);
          }
       }
     }          
  }  

3.Drag the 'Background' image straight from the 'Sprites' folder to the 'Scene View'
- After that a new GameObject will get created with the name of "Background" (This GameObject should only have a 'SpriteRenderer')
- Then adjust it's 'Scale' to the appropriate size (For example : x = 15, y = 12) and make it a child of the 'Camera'
- That should make the 'Background' follow along with the 'Camera' and 'Player'


4.Make the 'Floor' and 'Player' also a child of the 'Camera'


Hope this works :)

Comment
Add comment · Show 19 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Darckret · Mar 22 at 02:41 PM 0
Share

Thanks friend I already did the steps you mentioned but I get these 2 errors

  • namespace cannot directly contain members such as fields, methods or statements

*The name "Speed" does not exist in the current context

and in the part of making "background" a secondary element, I don't know how to do it, do I drag it inside the camera?

avatar image Caeser_21 Darckret · Mar 22 at 02:52 PM 0
Share
  • For the speed I changed the script in my initial answer and now it should work

  • Make the background image an object by dragging the image into the inspector, them make it a child of the camera

avatar image Caeser_21 Caeser_21 · Mar 22 at 02:56 PM 0
Share

Also could you send the camera script that you typed, just wanna check if there are any errors I am missing out :)

Show more comments
avatar image Darckret · Mar 22 at 07:17 PM 0
Share

No problem I really appreciate your help it's okay

look what happens now

https://youtu.be/nW98iy7MbTg

avatar image Caeser_21 Darckret · Mar 23 at 03:55 AM 0
Share

Hey mate I found out the problem, I have again updated my initial answer... check out the 4th point, that should fix you problem!

avatar image Darckret Caeser_21 · Mar 23 at 01:25 PM 0
Share

Hello friend, yes indeed now everything comes together but last night I tried it and ran the camera but the dinosaur stayed out of the scene and now the button to start the game does not work when I did it last night and I did not move anything else since last night that I tried

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

180 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What logic should i implement in this 2D game? 1 Answer

2D repeating obstacles with a random Y value 0 Answers

How come setting sprite tint/alpha does not increase draw calls? 0 Answers

playclipatpoint set default 2d (default audio is 3d) 0 Answers

Android 2D Game screen artifacts and compatibility issues on certain devices 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges