Skip to content

Instantly share code, notes, and snippets.

@FinnT730
Created July 15, 2019 14:03
Show Gist options
  • Save FinnT730/57192e13e8765368afeb47353737dc13 to your computer and use it in GitHub Desktop.
Save FinnT730/57192e13e8765368afeb47353737dc13 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Scripting;
public class Generator : MonoBehaviour
{
public GameObject prefab;
public GameObject collisionBlock;
public List<GameObject> collisionBlocks = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
float x = 0,y = -3.5f,z = 0;
for(int i = 0; i < 50; i++) { // Generate random placement.
GameObject obj = collisionBlock.transform.gameObject;
// obj.transform.position.Set(Random.Range(-6,6),-3.5f, i + 5);
GameObject spanwed = Instantiate(obj,new Vector3(Random.Range(-6,6),-3.5f, i + 5),Quaternion.identity);
collisionBlocks.Add(spanwed);
}
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public GameObject playerObj;
public Transform trans;
public float speed;
public GameObject collideObj;
private Generator generator = new Generator();
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 pos = playerObj.transform.position;
if (Input.GetKey(KeyCode.A) && !(pos.x < -5.9))
{
pos.Set(pos.x - 0.1f,pos.y,pos.z);
}
if (Input.GetKey(KeyCode.D) && (pos.x < 5.9))
{
pos.Set(pos.x + 0.1f,pos.y,pos.z);
}
if(Input.GetKeyDown(KeyCode.Space)) {
pos.Set(pos.x,pos.y + 2, pos.z);
}
if(pos.y > -3.5) {
pos.y -= 0.1f;
}
// if(!(collideObj.transform.position.x <= pos.x - 0.1f) &&
// !(collideObj.transform.position.z <= pos.y - 0.1f) &&
// !(collideObj.transform.position.z <= pos.y - 0.1f)) {
// pos.Set(pos.x, pos.y, pos.z + 0.1f);
// }
foreach (var obj in generator.collisionBlocks)
{ // Here is the collisionBlock being accesed, mcbabac (dave)
if(!(obj.transform.position.x + 0.1f > pos.x - 0.1f) ||
!(obj.transform.position.x - 1 < pos.x + 0.1f) ||
!(obj.transform.position.z + 1 > pos.z - 0.1f) ||
!(obj.transform.position.z - 1 < pos.z + 0.1f)) {
pos.Set(pos.x, pos.y, pos.z + 0.1f);
}
}
if(!(collideObj.transform.position.x + 0.1f > pos.x - 0.1f) ||
!(collideObj.transform.position.x - 1 < pos.x + 0.1f) ||
!(collideObj.transform.position.z + 1 > pos.z - 0.1f) ||
!(collideObj.transform.position.z - 1 < pos.z + 0.1f)) {
pos.Set(pos.x, pos.y, pos.z + 0.1f);
}
// if (Input.GetKey(KeyCode.W))
// {
// pos.Set(pos.x,pos.y + 0.1f,pos.z);
// }
// if (Input.GetKey(KeyCode.S) && !(pos.y < -3.4f))
// {
// pos.Set(pos.x,pos.y - 0.1f,pos.z);
// }
playerObj.transform.position = pos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment