воскресенье, 6 октября 2013 г.

RTS Mouse

Click and Run - система


function ShootRay() {
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    var hit : RaycastHit;
 
    if(Physics.Raycast(ray, hit, Mathf.Infinity)) {
        var moveScript : UnitMoveable = unit.GetComponent("UnitMoveable");
        if (moveScript) {
           moveScript.MoveTo(hit.point);
        }
    }
}



var moveSpeed : float = 2.0f;
private var vDestination : Vector3;
private var isMoving : boolean;
 
function Start () {
    isMoving = false;
}
 
function FixedUpdate () {
    if (isMoving) {
       if (transform.position.Equals(vDestination)) {
         isMoving = false;
       }
       else {
         // Vector will do a linear interpolation between our current position and our destination
         var v : Vector3;
         v = Vector3.Lerp(transform.position,vDestination,Time.fixedDeltaTime * moveSpeed);
 
         // Raycast to the surface below our next destination step to make sure we are standing on it
         // this is probably not efficient for large numbers
         var hit : RaycastHit;
         if (Physics.Raycast(v, Vector3.down, hit, 5.0f)) {
          // collider.bounds.extents.y will be half of the height of the bounding box
          // only useful if your pivot point is in the center, if it's at the feet you don't need this
          v.y = hit.point.y + collider.bounds.extents.y;
         }
         // set our position to the new destination
         transform.position = v;
       }
    }
}
 
function MoveTo (vPoint:Vector3) {
    vDestination = vPoint;
    isMoving = true;
}