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 nightbane30 · Mar 20, 2014 at 11:13 PM · c#collisionif-statementsgenerationrunner

Only detect collision once, if statement. C#

Hello everyone. I'm creating a simple endless runner game, and I'm having trouble creating a platform generator that generates platforms as the player goes further in the game. I wrote a script to randomly generate the y value of the next platform, and that works. The thing I'm having trouble with is generating the x value. I tried to do something like adding 77 units to the position of the next platform instantiate when it hits a previous platform. The problem is, when I do this, I get an endless amount of platforms generated as long as the player is in contact with the previous platform. This ends up in a mass of platforms and eventually crashing the game. How would I detect only one collision and not accept any more after that one?

By the way, I'm using a prefab as my platform.

Here's my script: using UnityEngine; using System.Collections;

 public class PlayerScript : MonoBehaviour {
 
     private Transform player;
     public float timer;
     public float playerSpeed;
     private bool touchingPlatform;
     public float acceleration;
     public Vector3 jumpVelocity;
     public float distanceTraveled;
     public GameObject Floor;
     private Vector3 platformPosition;
     int x,y;
     // Use this for initialization
     void Start () {
 
         player = transform;
         player.position = new Vector3 (0, 0, 0);
         timer = Time.time;
         y = Random.Range (-3, 3);
         platformPosition = new Vector3 (x,y, 0);
     
     }
     
     // Update is called once per frame
     void Update () {
 
         player.Translate (Vector3.right * Time.deltaTime * playerSpeed);
         if (touchingPlatform && Input.GetButtonDown("Jump")) {
             rigidbody.AddForce(jumpVelocity, ForceMode.Acceleration);
             Debug.Log("Space was pressed");
                 }
         distanceTraveled = player.localPosition.x;
 
     
     }
     void FixedUpdate() {
 
         if (touchingPlatform) {
             rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
                 }
 
 
         }
     void OnCollisionEnter (Collision playerCollide) {
         if (playerCollide.gameObject.tag == "Firstplat") {
             Instantiate(Floor, platformPosition, Quaternion.identity);
                 }
         if (playerCollide.gameObject.tag == "Floor") {
             x = x + 77;
             Instantiate(Floor, platformPosition, Quaternion.identity);
                 }
 
         touchingPlatform = true;
 
         }
     void OnCollisionExit () {
 
         touchingPlatform = false;
 
 
     }
 }
 
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 highpockets · Mar 21, 2014 at 06:11 AM 0
Share

Instantiate is a more CPU intensive way of doing the same thing by simply pooling your objects. What that means, is that those 'floors' are already in the scene (just out of site, at position (500,500,500) for example and you store however many you need to keep cycling through ) and you place them when you need them by simply changing the position. I think that would be a more optimized approach for what you're doing here (look up object pooling for more details).

Anyways, it looks like the player is constantly entering the collision and therefore continuously running that code. I think it may be better to have a trigger that starts at the beginning of the platforms. Since it is a continuos runner, that means that the player will only ever enter the trigger once, so OnTriggerEnter would probably do the trick.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sriram90 · Mar 21, 2014 at 06:34 AM

Because of you're Instantiating Objects as long as trigger enter with that Object, so obviously it'll generate multiple times.

 private string lastCollidedObjName = "";
 private bool generatePlatform = false;
 
 void OnCollisionEnter(Collision obj)
 {
 if(!generatePlatform )
 {
 //Instantiating whatever objects you want, it'll instantiate only one time
 lastCollidedObjName = obj.gameObject.name;
 generatePlatform = true;
 }
 }
 
 void OnCollisionExit(Collision obj)
 {
 if(generatePlatform && obj.gameObject.name == lastCollidedObjName )
 {
 generatePlatform = false;
 }
 }
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

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to give a component on collision? 1 Answer

Get Object that Instantiated this object 1 Answer

Move until collision sticks 0 Answers


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