C#声明错误

本文关键字:错误 声明 | 更新日期: 2023-09-27 18:28:27

我正在制作RTS风格的游戏,有一些错误,请帮忙。

Assets/Scripts/CameraOperator.cs(64,38):错误CS0023:!' operator cannot be applied to operand of type UnityEngine.Vector3'

Assets/Scripts/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;
    }

}

C#声明错误

函数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;

这应该解释编译器错误。