- Home /
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;
}
}
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.
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;
}
}
Your answer
Follow this Question
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