Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 yyddaa · Oct 02, 2018 at 07:20 PM · 2d game2d platformerendless runnerendless

so how can I place the randomly generated obstacles and coins along the up and down hill of the ground am driving on thank you

Hey thank for your time am kind of new for unity but am touting my self unity2d so…here is the question I want to make a 2d endless car game like hill climb and so I have done with endless scrolling back ground and a car model but I want to create an randomly generated obstacle and coins with prefab but the ground has up hills and downhill so how can I place the randomly generated obstacles and coins along the up and down hill of the ground am driving on thank you. Can’t find any tutorials on endless 2d car games if you have recommendations and solution I’ll be happy

Comment
Add comment · Show 1
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 tormentoarmagedoom · Oct 02, 2018 at 08:51 PM 0
Share

For your text, I understand the question you want to be answered is not "how can I place"

The question you need to be answered is "How i detect the surface of a random generated ground".

bye!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by yyddaa · Oct 02, 2018 at 09:26 PM

ya your right @ tormentoarmagedoom i was trying to ask like that"How i detect the surface of a random generated ground" am kinda new for game development that is why i miss the technical words anyways thanks

Comment
Add comment · 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
0

Answer by Zay116 · Oct 02, 2018 at 09:27 PM

@yyddaa This is something I just tested and it should work for you. Attach this script to an object and place the object above the hills that will spawn and it will check the distance and spawn a coin randomly. Make sure to drag on the right components in the inspector. Like the coin prefab. I tested it with 3D objects but I think it will still work with 2D.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GroundDetector : MonoBehaviour
 {
     // Make sure to assign this in the inspector so it knows what the coin should be.
     public GameObject Coin;
     //Change this in the inspector to determine how often it should try to spawn the coin.
     public float CheckSpawnRate;
     float countDown;
     Vector3 coinPos;
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         
         countDown -= 1f * Time.deltaTime;
         if (countDown <= 0f)
         {
             RandomCoin();
             countDown = CheckSpawnRate;
         }
     }
     public void RandomCoin()
     {
         //Check the "1" From 1 to any other number and that will determine how likely it is to spawn a coin.
 
         int r = Random.Range(0, 1);
         if (r == 0)
         {
             RaycastHit hit;
             //This is where it checks the ground height.
             if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, Mathf.Infinity))
             {
                 coinPos = hit.point;
                 //The draw ray isn't required but it shows you a laser beam of where it is looking. Keep in mind it shoots from the bottom of the object the script is attatched to.
                 Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * hit.distance, Color.yellow);
                 Debug.Log("Did Hit");
             }
             Instantiate(Coin, coinPos, Quaternion.identity);
         }
     }
 }
 

Comment
Add comment · Show 1 · 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 toddisarockstar · Oct 03, 2018 at 04:18 AM 1
Share

if he is using sprites he might need to replace the raycast with raycast2d. but if he wanted a line of coins he could do something like this:

 RaycastHit hit;
 float spacing = 1.5f;
 int i = 50;
 while(i>-50){i--;
 Vector3 p = new Vector3(i*spacing,200,0);
 if (Physics.Raycast(p,Vector3.down, out hit))
              {GameObject g = GameObject.CreatePrimitive (PrimitiveType.Sphere);
         g.transform.position = hit.position;
 }}

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

107 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

Related Questions

How to keep a gameobject on the ground ? 1 Answer

Level design clone of King of Thieves 0 Answers

Should a physics material be added to the collider or the rigidbody? 2 Answers

Can I change the pivot point of animated character, not a single sprite? 1 Answer

In my 2D platformer game, how would I create a height marker?, 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