function Update ()
{
if (Input.GetButtonDown("Fire1"))
{
mouseButton1DownPoint = Input.mousePosition;
var hit1 : RaycastHit;
var ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
//Debug.DrawRay (ray.origin, ray.direction * 100.0, Color.green);
if ( Physics.Raycast (ray1, hit1, raycastLength) ) // terrainLayerMask
{
if (hit1.collider.name == "Terrain")
{
print ("Mouse Down Hit Terrain " + hit1.point);
mouseButton1DownTerrainHitPoint = hit1.point;
leftRectStart = hit1.point;
lineRenderer.SetPosition(0, leftRectStart);
lineRenderer.SetPosition(1, leftRectEnd);
mouseLeftDrag = true;
}
else
{
print ("Mouse Down Hit something: " + hit1.collider.name);
//hit.collider.gameObject.SendMessage("SetSelected");
// Ray hit a unit, not the terrain. Deselect all units as the fire 1 up
// event will then select that just recently clicked unit!
ClearSelectedUnitsList();
}
//Debug.DrawRay (ray.origin, ray.direction * 100.0, Color.green);
}
}
if (Input.GetButtonUp("Fire1"))
{
mouseButton1UpPoint = Input.mousePosition;
print("units? " + unitManager.GetSelectedUnitsCount());
if (mouseButton1DownPoint == mouseButton1UpPoint) {
// user just did a click, no dragging. mouse 1 down and up pos are equal.
// if units are selected, move them. If not, select that unit.
if (unitManager.GetSelectedUnitsCount() == 0) {
// no units selected, select the one we clicked - if any.
ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
if ( Physics.Raycast (ray1, hit1, raycastLength, nonTerrainLayerMask) )
{
// Ray hit something. Try to select that hit target.
//print ("Hit something: " + hit.collider.name);
hit1.collider.gameObject.SendMessage("SetSelected");
}
}
} else {
// mouse is dragged
ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
if ( Physics.Raycast (ray1, hit1, raycastLength, terrainLayerMask) )
{
//if (hit.collider.name == "Terrain")
//{
print ("Hit Terrain 2 " + hit1.point);
//MoveSelectedUnitsToPoint(hit.point);
leftRectEnd = hit1.point;
ClearSelectedUnitsList();
SelectUnitsInArea(leftRectStart, leftRectEnd);
lineRenderer.SetPosition(1, leftRectEnd);
}
}
}
//if (mouseLeftDrag) {
//}
// Debug rays for selection rect
Debug.DrawRay(leftRectStart, Vector3.up * 10, Color.red);
Debug.DrawRay(leftRectEnd, Vector3.up * 10, Color.red);
//WorldToScreenPoint
if (Input.GetButtonDown("Fire2"))
{
mouseButton2DownPoint = Input.mousePosition;
mouseRightDrag = true;
}
if (Input.GetButtonUp("Fire2"))
{
mouseRightDrag = false;
mouseButton2UpPoint = Input.mousePosition;
//if (mouseButton2DownPoint == mouseButton2UpPoint) {
//ClearSelectedUnitsList();
//}
var hit2 : RaycastHit;
var ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);
//Debug.DrawRay (ray.origin, ray.direction * 100.0, Color.green);
if ( Physics.Raycast (ray2, hit2, raycastLength) ) // terrainLayerMask
{
if (hit2.collider.name == "Terrain")
{
print ("Mouse Down Hit Terrain " + hit2.point);
//MoveSelectedUnitsToPoint(hit.point);
mouseButton2DownTerrainHitPoint = hit2.point;
}
}
// untis are selected, move them. Unit Manager's unit count is > 0!
MoveSelectedUnitsToPoint(mouseButton2DownTerrainHitPoint);
}
if (mouseRightDrag)
{
var dragDifference : Vector2 = mouseButton2DownPoint - Input.mousePosition;
//print("Mouse drag distance: " + dragDifference.x + " " + dragDifference.y);
dragDifference /= 40.0;
moveDirection = new Vector3(-dragDifference.x, 0, -dragDifference.y);
// Translating so move relative to where the camera currently is located
transform.Translate(moveDirection);
var speed = 1.0;
moveDirection *= speed;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
controller.Move(moveDirection * Time.deltaTime);
}
var mouseWheelSpeed : float = 150.0;
var mouseWheel : float = Input.GetAxis ("Mouse ScrollWheel");
if (mouseWheel != 0)
{
//print("mouse wheel value: " + mouseWheel);
var currentHeight = transform.position.y;
currentHeight -= mouseWheel * mouseWheelSpeed * Time.deltaTime;
if (currentHeight > 150.0) {
currentHeight = 150.0;
}
if (currentHeight < 20.0) {
currentHeight = 20.0;
}
transform.position = Vector3(transform.position.x, currentHeight, transform.position.z);
}
}