Skip to content

Instantly share code, notes, and snippets.

@attashe
Created February 25, 2020 10:13
Show Gist options
  • Save attashe/d7ed36e53c58c0bc444d78f65bb67365 to your computer and use it in GitHub Desktop.
Save attashe/d7ed36e53c58c0bc444d78f65bb67365 to your computer and use it in GitHub Desktop.
Porting Unity Scripts
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BattleUnit : MonoBehaviour {
public GameObject fireCyrcle;
public GameObject moveCyrcle;
bool isSelected;
GameObject fireZone;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseDown()
{
Debug.Log("Mouse down");
Debug.Log(transform.position);
isSelected = !isSelected;
if (isSelected)
{
fireZone = Instantiate(fireCyrcle, transform.position, transform.rotation);
fireZone.transform.localScale = new Vector3(4, 4, 4);
fireZone.transform.parent = gameObject.transform;
}
else
{
GameState.isUnitSelected = false;
Destroy(fireZone);
}
}
void OnMouseOver()
{
if(Input.GetMouseButtonDown(1)){
Debug.Log("Right msouse down");
Debug.Log(transform.position);
isSelected = !isSelected;
if (isSelected)
{
GameState.isUnitSelected = true;
GameState.selectedUnit = gameObject;
fireZone = Instantiate(moveCyrcle, transform.position, transform.rotation);
fireZone.transform.localScale = new Vector3(10, 10, 10);
fireZone.transform.parent = gameObject.transform;
}
else
{
GameState.isUnitSelected = false;
Destroy(fireZone);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColorizeTile : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.Log("Tile start");
}
// Update is called once per frame
void Update () {
}
void OnMouseEnter()
{
Debug.Log("Mouse enter");
SpriteRenderer renderer = GetComponent<SpriteRenderer>();//Get the renderer via GetComponent or have it cached previously
renderer.color = new Color(0f, 1f, 1f, 1f);
}
void OnMouseExit()
{
Debug.Log("Mouse exit");
SpriteRenderer renderer = GetComponent<SpriteRenderer>();//Get the renderer via GetComponent or have it cached previously
renderer.color = new Color(1f, 1f, 1f, 1f);
}
//void OnMouseDrag()
//{
// Debug.Log("Mouse drag");
// // SpriteRenderer renderer = GetComponent<SpriteRenderer>();//Get the renderer via GetComponent or have it cached previously
// // renderer.color = new Color(0f, 1f, 1f, 1f);
//}
void OnMouseDown()
{
Debug.Log("Mouse down");
if(GameState.isUnitSelected)
{
Debug.Log("Transform obj");
Debug.Log(GameState.selectedUnit);
Vector3 selfPos = transform.position;
Vector3 objPos = GameState.selectedUnit.transform.position;
if (Vector3.Distance(selfPos, objPos) < GameState.moveDistance)
{
Transform tr = GameState.selectedUnit.transform;
tr.position = new Vector3(selfPos.x, selfPos.y, objPos.z);
objPos.x = selfPos.x;
objPos.y = selfPos.y;
}
}
// rigidbody.AddForce(-transform.forward * 500f);
// rigidbody.useGravity = true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreateGameField : MonoBehaviour
{
public GameObject groundTile;
// Use this for initialization
void Start()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Instantiate(groundTile, new Vector3(i, j, 0), new Quaternion());
}
}
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.Log("Name = " + hit.collider.name);
Debug.Log("Tag = " + hit.collider.tag);
Debug.Log("Hit Point = " + hit.point);
Debug.Log("Object position = " + hit.collider.gameObject.transform.position);
Debug.Log("--------------");
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class GameState {
public static GameObject selectedUnit;
public static bool isUnitSelected;
public static float moveDistance = 10;
}
// Just add this script to your camera. It doesn't need any configuration.
using UnityEngine;
public class TouchCamera : MonoBehaviour {
Vector2?[] oldTouchPositions = {
null,
null
};
Vector2 oldTouchVector;
float oldTouchDistance;
void Update() {
if (Input.touchCount == 0) {
oldTouchPositions[0] = null;
oldTouchPositions[1] = null;
}
else if (Input.touchCount == 1) {
if (oldTouchPositions[0] == null || oldTouchPositions[1] != null) {
oldTouchPositions[0] = Input.GetTouch(0).position;
oldTouchPositions[1] = null;
}
else {
Vector2 newTouchPosition = Input.GetTouch(0).position;
transform.position += transform.TransformDirection((Vector3)((oldTouchPositions[0] - newTouchPosition) * GetComponent<Camera>().orthographicSize / GetComponent<Camera>().pixelHeight * 2f));
oldTouchPositions[0] = newTouchPosition;
}
}
else {
if (oldTouchPositions[1] == null) {
oldTouchPositions[0] = Input.GetTouch(0).position;
oldTouchPositions[1] = Input.GetTouch(1).position;
oldTouchVector = (Vector2)(oldTouchPositions[0] - oldTouchPositions[1]);
oldTouchDistance = oldTouchVector.magnitude;
}
else {
Vector2 screen = new Vector2(GetComponent<Camera>().pixelWidth, GetComponent<Camera>().pixelHeight);
Vector2[] newTouchPositions = {
Input.GetTouch(0).position,
Input.GetTouch(1).position
};
Vector2 newTouchVector = newTouchPositions[0] - newTouchPositions[1];
float newTouchDistance = newTouchVector.magnitude;
transform.position += transform.TransformDirection((Vector3)((oldTouchPositions[0] + oldTouchPositions[1] - screen) * GetComponent<Camera>().orthographicSize / screen.y));
transform.localRotation *= Quaternion.Euler(new Vector3(0, 0, Mathf.Asin(Mathf.Clamp((oldTouchVector.y * newTouchVector.x - oldTouchVector.x * newTouchVector.y) / oldTouchDistance / newTouchDistance, -1f, 1f)) / 0.0174532924f));
GetComponent<Camera>().orthographicSize *= oldTouchDistance / newTouchDistance;
transform.position -= transform.TransformDirection((newTouchPositions[0] + newTouchPositions[1] - screen) * GetComponent<Camera>().orthographicSize / screen.y);
oldTouchPositions[0] = newTouchPositions[0];
oldTouchPositions[1] = newTouchPositions[1];
oldTouchVector = newTouchVector;
oldTouchDistance = newTouchDistance;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment