DAY 3:
For today want to get the Input class started, so can start interacting with the Universe and the objects within it. I will likely want to get an initial UI class going as well, that way I can dynamically pull debug info during run time and display it on screen.
For the Input class, already have most of the code written to handle the Mouse/Tile stuff from the demo on Day 1. The basics of it are creating a Plane along the X, Z axis and raycasting against it to find the mouse world coordinate. From there can use the Hex/Quad class to convert from world space to Hex/Quad space.
Mouse Plane Raycasting
Plane mouse_plane = new Plane (Vector3.up, 0.0f);
Ray mouse_ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float mouse_ray_distance = 0.0f;
Vector3 mouse_point = Vector3.positiveInfinity;
if (click_plane.Raycast (mouse_ray, out mouse_ray_distance)) {
mouse_point = mouse_ray.GetPoint (mouse_ray_distance);
}
object obj = main.map.GetTileFromWorldPoint (mouse_point);
Would also like to be able to move/zoom the camera. Will be able to use WASD, Arrows, or Mouse Drag for movement and -. +, or scroll wheel for zoom. For the mouse dragging part, checking if the mouse has moved a minimum distance before the drag begins to be able to handle clicks as well. Also using some multipliers to scale how much is moved depending on the zoom level, being farther away should cause the camera to move/zoom more.
Camera Movement/Zooming
Vector3 camera_move_direction = Vector3.zero;
float camera_min_distance = 5.0f;
float camera_max_distance = 100.0f;
Vector3 mouse_drag_start = Vector3.zero;
Vector3 mouse_drag_current = Vector3.zero;
bool mouse_dragging = false;
float mouse_move_sensitivity = 10.0f;
float mouse_move_multiplier = 2.0f;
float mouse_scroll_multiplier = 2.0f;
if (Input.GetKey (KeyCode.UpArrow) || Input.GetKey (KeyCode.W)) { camera_move_direction.z = 1.0f; }
if (Input.GetKey (KeyCode.DownArrow) || Input.GetKey (KeyCode.S)) { camera_move_direction.z = -1.0f; }
if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A)) { camera_move_direction.x = -1.0f; }
if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D)) { camera_move_direction.x = 1.0f; }
// Check Mouse Down State
if (Input.GetMouseButtonDown (0)) {
mouse_drag_start = Input.mousePosition;
mouse_drag_current = Input.mousePosition;
}
// Check Mouse Down (Actively) State, Handle Mouse Movement
if (Input.GetMouseButton (0)) {
if (mouse_dragging) {
camera_move_direction.x = (mouse_drag_current.x - Input.mousePosition.x) / mouse_move_sensitivity;
camera_move_direction.z = (mouse_drag_current.y - Input.mousePosition.y) / mouse_move_sensitivity;
}
else {
if (Vector3.Distance (mouse_drag_start, mouse_drag_current) > 3.0f) {
mouse_dragging = true;
}
}
mouse_drag_current = Input.mousePosition;
}
// Check Mouse Up State, Handle Clicks and Ending Mouse Movement.
if (Input.GetMouseButtonUp (0)) {
if (mouse_dragging == false) {
if (mouse_ray_distance > 0.0f) {
Debug.Log ("Clicked - " + mouse_point);
Debug.DrawLine (mouse_point - Vector3.up, mouse_point + Vector3.up, Color.yellow, 5.0f);
}
}
mouse_drag_start = Vector3.zero;
mouse_dragging = false;
}
// Zooming Controls, Keyboard
if (Input.GetKey (KeyCode.Plus) || Input.GetKey (KeyCode.KeypadPlus)) {
camera_position.y = camera_position.y - (1.0f * (mouse_scroll_multiplier * camera_position.y) * Time.deltaTime);
if (camera_position.y < camera_min_distance) { camera_position.y = camera_min_distance; }
}
if (Input.GetKey (KeyCode.Minus) || Input.GetKey (KeyCode.KeypadMinus)) {
camera_position.y = camera_position.y - (-1.0f * (mouse_scroll_multiplier * camera_position.y) * Time.deltaTime);
if (camera_position.y > camera_max_distance) { camera_position.y = camera_max_distance; }
}
// Zooming Controls, Mouse
if (Input.mouseScrollDelta.y != 0.0f) {
camera_position.y = camera_position.y - (Input.mouseScrollDelta.y * (mouse_scroll_multiplier * camera_position.y) * Time.deltaTime);
if (camera_position.y < camera_min_distance) { camera_position.y = camera_min_distance; }
if (camera_position.y > camera_max_distance) { camera_position.y = camera_max_distance; }
}
// Update Camera Position
camera_position = camera_position + (camera_move_direction * (mouse_move_multiplier * camera_position.y) * Time.deltaTime);
// Check Escape / End
if (Input.GetKeyDown (KeyCode.Escape)) { Application.Quit (); }
At this point I would like to start adding the UI and getting some debug info of the tile the mouse is over. I have never been a big fan of Unity3D’s newer UI system, but for the sake of Devember will attempt to learn to use it better, or at the very least not dislike it as much.
Decided to spice up the background a little with some distant stars/galaxies before finishing up for today. Can make this more efficient later by building a single mesh, but for now it makes the background a little more interesting.
Stars Background
public class StarsBackgroundScript : MonoBehaviour {
public Material[] StarMaterial;
List<GameObject> Stars;
void Start() {
Stars = new List<GameObject> ();
for (int i = 0; i < 500; i++) {
GameObject go = GameObject.CreatePrimitive (PrimitiveType.Quad);
go.transform.parent = this.transform;
go.transform.position = new Vector3 (Random.Range (-200.0f, 200.0f), Random.Range (-50.0f, -10.0f), Random.Range (-200.0f, 200.0f));
go.transform.rotation = Quaternion.Euler (90.0f, 0.0f, 0.0f);
go.transform.localScale = new Vector3 (0.5f, 0.5f, 0.5f);
go.GetComponent<MeshRenderer> ().sharedMaterial = StarMaterial[Random.Range (0, StarMaterial.Length)];
Destroy (go.GetComponent<MeshCollider> ());
Stars.Add (go);
}
}
}