So I’ve got a lot of the mechanics already done, however some weird things are at hand currently in my progression… Also, there’s some things I’d like explained as well.
Much of this will be described in the attachments below so check them out. (I confuse myself sometimes when explaining things like this)
Main elements of this game include:
- Items that fall on a randomized horizontal range
- Score
- Life
- Layered sprites
- Horizontal movement
- 10+ different Items
Basically, you’re just trying to catch Items as they fall.
There is a 2D collider on every Item, as well as the player for physics.
There is a copy of the collider on a gameobject parented to the item. This is a trigger so if they pass the red line or a player catches them, they will be destroyed. Yes They were both set to origin first.
The players box collider inside is for the items when they are caught. This is also a trigger.
Here’s my scripts:
Movement
using UnityEngine;
using System.Collections;
public class BucketMovement : MonoBehaviour
{
public float minX = -4.5f;
public float maxX = 4.5f;
public float speed = 5.0f;
private Rigidbody2D playerRigid;
void Start ()
{
playerRigid = gameObject.GetComponent<Rigidbody2D> ();
}
void FixedUpdate ()
{
if (Input.GetKey (KeyCode.LeftArrow)) {
//transform.position -= transform.right * speed * Time.deltaTime;
playerRigid.velocity = new Vector2 (-speed, 0) * speed * Time.deltaTime;
}
else
{
playerRigid.velocity = new Vector2 (0, 0) * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.RightArrow))
{
//transform.position += transform.right * speed * Time.deltaTime;
playerRigid.velocity = new Vector2 (speed, 0) * speed * Time.deltaTime;
}
// Constraints player movement to 'minX' and 'maxX'
if (playerRigid.position.x <= minX)
{
playerRigid.position = new Vector2 (minX, playerRigid.position.y);
}
if (playerRigid.position.x >= maxX)
{
playerRigid.position = new Vector2 (maxX, playerRigid.position.y);
}
}
}
Item Spawner
using UnityEngine;
using System.Collections;
public class ItemSpawner : MonoBehaviour
{
public float rateOfSpawn = 2.0f;
public float minSpawnX = -5.11f;
public float maxSpawnX = 5.11f;
public float spawnHeight = 4.5f;
public GameObject[] Item_List;
void Start ()
{
}
void Update ()
{
Invoke ("SpawnItemsIn", rateOfSpawn);
}
void SpawnItemsIn ()
{
int Items_In_List = Random.Range (0, 9);
Vector2 spawn_Pos = new Vector2 (Random.Range (minSpawnX, maxSpawnX), spawnHeight);
Instantiate (Item_List [Items_In_List], spawn_Pos, Quaternion.identity);
}
}
I’m getting this error, I have no Idea how to fix this? This is the first time I’ve used arrays in unity/C# so that may be a contributor…
And last but not least, it spawns like crazy! It’s actually pretty hilarious to see every prefab item just fall from the sky an extreme number of times. It lags after like 30 seconds because they arent destroying below the player (Haven’t gotten to that yet.)
Any ideas? How would I make a spawn rate time for the items to fall?
Any improvement to my code that can be explained? Thanks a lot guys.
Here’s some poorly drawn examples.