2014-10-30 179 views
0

我正在製作RTS風格的遊戲,並有一些錯誤請幫助。C#聲明錯誤

資產/腳本/ CameraOperator.cs(64,38):錯誤CS0023:該!' operator cannot be applied to operand of type UnityEngine.Vector3'

資產/腳本/ CameraOperator.cs(74,38):錯誤CS0165:未分配使用本地變量`hit'

這是腳本。

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

public class CameraOperator : MonoBehaviour 
{ 
    public Texture2D selectionHighlight = null; 
    public static Rect Selection = new Rect (0, 0, 0, 0); 
    private Vector3 StartClick = -Vector3.one; 
    private static Vector3 moveToDestination = Vector3.zero; 
    private static List<string> passables = new List<string>() {"Floor"}; 

    private void Update() 
    { 
      CheckCamera(); 
      CleanUp(); 
    } 

    private void CheckCamera() 
    { 
      if (Input.GetMouseButtonDown (0)) { 
        StartClick = Input.mousePosition; 
      } 
      if (Input.GetMouseButtonUp (0)) { 
        StartClick = -Vector3.one; 
      } 
      if (Input.GetMouseButton (0)) { 
        Selection = new Rect (StartClick.x, InvertMouseY (StartClick.y), Input.mousePosition.x - StartClick.x, InvertMouseY (Input.mousePosition.y) - InvertMouseY (StartClick.y)); 
        if (Selection.width < 0) { 
          Selection.x += Selection.width; 
          Selection.width = -Selection.width; 
        } 
        if (Selection.height < 0) { 
          Selection.y += Selection.height; 
          Selection.height = -Selection.height; 
        } 
      } 
    } 

    private void OnGUI() 
    { 
      if (StartClick == -Vector3.one) { 
        GUI.color = new Color (1, 1, 1, 0.5f); 
        GUI.DrawTexture (Selection, selectionHighlight); 
      } 
    } 

    float InvertMouseY (float y) 
    { 
      return Screen.height - y; 
    } 

    private void CleanUp() 
    { 
      if (!Input.GetMouseButtonUp (1)) { 
        moveToDestination = Vector3.zero; 
      } 
    } 

    public static Vector3 getDestination() 
    { 
      RaycastHit hit; 
      Ray r = Camera.main.ScreenPointToRay (Input.mousePosition); 
      if (moveToDestination = Vector3.zero) { 


        if (Physics.Raycast (r, out hit)) { 
          while (!passables.Contains(hit.transform.gameObject.name)) { 
            if (!Physics.Raycast (hit.transform.position, r.direction, out hit)) 
              break; 
          } 
        } 
      } 
      if (!hit.transform == null) { 
        moveToDestination = hit.point; 
      } 
      return moveToDestination; 
    } 

}

+0

也許你有一個額外的關閉花括號? – JleruOHeP 2014-10-30 06:31:26

回答

1

您在功能public static Vector3 GetDestination

public static Vector3 GetDestination() 
{ 
     if (moveToDestination == Vector3.zero) 
     { 
      RaycastHit hit; 
      Ray r = Camera.mainCamera.ScreenPointToRay (Input.mousePosition); 

      if (Physics.Raycast (r, out hit)) 
      { 
       while (!passables.Contains(hit.transform.gameobject.name)) 
       { 
        if (!Physics.Raycast (hit.transform.position, r.direction, out hit)) 
         break; 
        } // This seems like an unneeded } 

       } 
      } 
     } 
     if(!hit.transform = null) 
     { 
      moveToDestination = hit.point; 
     } 
    } 
    return moveToDestination; 
} 

有一個額外的}編譯器看到你的代碼爲:

public static Vector3 GetDestination() 
{ 
    if (moveToDestination == Vector3.zero) 
    { 
     RaycastHit hit; 
     Ray r = Camera.mainCamera.ScreenPointToRay (Input.mousePosition); 

     if (Physics.Raycast (r, out hit)) 
     { 
     while (!passables.Contains(hit.transform.gameobject.name)) 
     { 
      if (!Physics.Raycast (hit.transform.position, r.direction, out hit)) 
       break; 
     } 
     } 
    } 
} 

if(!hit.transform = null) 
{ 
    moveToDestination = hit.point; 
} 
} 
return moveToDestination; 

這應該說明的編譯器錯誤。