Spawn Random Objects

Spawn Random Objects only by a horizontal line

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeteoriteMove : MonoBehaviour
{
Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent();
rb.velocity = new Vector2(rb.velocity.x, -0.9f);
}

// Update is called once per frame
void Update()
{
transform.Rotate(Vector3.back * 20f * Time.deltaTime);
rb.velocity = new Vector2(rb.velocity.x, -0.9f);

}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "ResetTag")
{
Destroy(gameObject);
}
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnMeteorite : MonoBehaviour
{
public GameObject[] enemy;
[SerializeField] public float interval = 10f;
private float timer = 0;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
timer += Time.deltaTime;
if(timer >= interval)
{
int randomEnemy = Random.Range(0, enemy.Length);
var pos = new Vector3(Random.Range(-4, 4), Random.Range(8,8), Random.Range(10,10));
Instantiate(enemy[randomEnemy], pos, Quaternion.identity);
timer = 0;
}
}
}

 


Saw Vertical

Saw Animation Vertical with Unity 2D

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SawV : MonoBehaviour
{
Rigidbody2D rb;
bool top = true;
bool down = false;
// Start is called before the first frame update
void Start()
{
rb = GetComponent();
}

// Update is called once per frame
void Update()
{
transform.Rotate(Vector3.back * 200 * Time.deltaTime);
if (top)
{
rb.velocity = new Vector2(rb.velocity.x, -4);
}
if (down)
{
rb.velocity = new Vector2(rb.velocity.x, 4);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "TopTag")
{
top = true;
down = false;
}
if (collision.tag == "DownTag")
{
top = false;
down = true;
}
}
}


Saw Horizontal

Saw Animation Horizontal with Unity 2D

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SawH : MonoBehaviour
{
Rigidbody2D rb;
bool left = true;
bool right = false;
// Start is called before the first frame update
void Start()
{
rb = GetComponent();
}

// Update is called once per frame
void Update()
{
transform.Rotate (Vector3.back * 200 * Time.deltaTime);
if(left)
{
rb.velocity = new Vector2(4, rb.velocity.y);
}
if (right)
{
rb.velocity = new Vector2(-4, rb.velocity.y);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "LeftTag")
{
left = true;
right = false;
}
if (collision.tag == "RightTag")
{
left = false;
right = true;
}
}
}


Database Sqlite with javaFX

How to create database and table sqlite with javaFX

package application;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;

public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
GridPane grid = new GridPane();
Scene scene = new Scene(grid,800,600);
createNewDatabase();
createNewTable();
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Tutorial");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}

public static void createNewDatabase() {
String url = "jdbc:sqlite:src/application/database/test.db";

try (Connection conn = DriverManager.getConnection(url)) {
if (conn != null) {
DatabaseMetaData meta = conn.getMetaData();
System.out.println("A new database has been created.");
}

} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public static void createNewTable() {
// SQLite connection string
String url = "jdbc:sqlite:src/application/database/test.db";

// SQL statement for creating a new table
String sql = "CREATE TABLE IF NOT EXISTS table1 (\n"
+ " id integer PRIMARY KEY,\n"
+ " first_name text NOT NULL,\n"
+ " last_name text NOT NULL,\n"
+ " city text NOT NULL\n"
+ ");";

try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
// create a new table
stmt.execute(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
launch(args);
}
}


Unity 2D Controller Jet

VIDEO PART ONE

VIDEO PART TWO

Code C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioLazer : MonoBehaviour
{
public AudioSource Audio;
// Start is called before the first frame update
void Start()
{
Audio = GetComponent();
}

// Update is called once per frame
void Update()
{

}
public void LazerAudio()
{
Audio.Play();
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletPlayer : MonoBehaviour
{
Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent();
}

// Update is called once per frame
void Update()
{
rb.velocity = new Vector2(rb.velocity.x, 20f);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "WallTag")
{
Destroy(gameObject);
}
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
bool drag;
Rigidbody2D rb;
float firetime = 0;
public float firetiming;
public Transform firePos;
AudioLazer lazer;
public GameObject BulletPlayer;
// Start is called before the first frame update
void Start()
{
rb = GetComponent();
lazer = FindFirstObjectByType();
}

// Update is called once per frame
void Update()
{
if(drag)
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
transform.Translate(mousePos);
if(Time.time > firetime)
{
firetime = Time.time + firetiming;
Instantiate(BulletPlayer, firePos.position, firePos.rotation);
lazer.LazerAudio();
}
}
}
private void OnMouseDown()
{
drag = true;
}
private void OnMouseUp()
{
drag = false;
}
}


Unity 2D Spawn Infinity Star

VIDEO PART NUMBER ONE

VIDEO PART NUMBER TWO

 

CODE SpawnStar 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnStar : MonoBehaviour
{
public GameObject starObj;
public int maxStars = 100;
// Start is called before the first frame update
void Start()
{
Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));
Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));
for(int i = 0; i < maxStars; ++i) { GameObject star = (GameObject)Instantiate(starObj); star.transform.position = new Vector2(Random.Range(min.x, max.x), Random.Range(min.y, 40)); star.GetComponent().speed = -(1f * Random.value + 0.5f);
star.transform.parent = transform;
}
}

// Update is called once per frame
void Update()
{

}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StarController1 : MonoBehaviour
{
public float speed = -0.2f;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
Vector2 pos = transform.position;
pos = new Vector2(pos.x, pos.y + speed * Time.deltaTime);
transform.position = pos;
Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));
Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));
if(transform.position.y < min.y) { transform.position = new Vector2(Random.Range(min.x, max.x), max.y); } } }

 


 

Drag and Drop with Unity 2D

Code of Drag Objects

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragObj : MonoBehaviour
{
bool drag;
public GameObject obj, objBlack;
Vector3 currentPos;
// Start is called before the first frame update
void Start()
{
currentPos = obj.transform.position;
}

// Update is called once per frame
void Update()
{
if(drag)
{
Vector2 MousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
transform.Translate(MousePos);
}
if(!drag)
{
float distance = Vector3.Distance(obj.transform.position, objBlack.transform.position);
if(distance < 0.5f) { obj.transform.position = objBlack.transform.position; } else { obj.transform.position = currentPos; } } } private void OnMouseDown() { drag = true; } private void OnMouseUp() { drag = false; } }