您好我正在制作RTS风格的游戏并且难以选择并突出显示我的车辆。这是我有的错误。任何帮助将不胜感激。
Assets / Scripts / Unit2.cs(19,51):错误CS0120:访问非静态成员`CameraOperator.InvertMouseY(float)'
需要对象引用这是脚本。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Unit2 : MonoBehaviour
{
public bool selected = false;
public float floorOffset = 1;
public float speed = 5;
public float stopDistanceOffset = 0.5f;
private Vector3 moveToDest = Vector3.zero;
private void Update ()
{
if (renderer.isVisible && Input.GetMouseButtonDown (0))
{
Vector3 camPos = Camera.main.WorldToScreenPoint (transform.position);
camPos.y = CameraOperator.InvertMouseY(camPos.y); "This Line Error"
selected = CameraOperator.Selection.Contains (camPos);
if (selected)
{
renderer.material.color = Color.red;
}
else
{
renderer.material.color = Color.white;
}
if(selected && Input.GetMouseButtonUp(1))
{
Vector3 destination = CameraOperator.getDestination();
if(destination != Vector3.zero)
{
moveToDest = destination;
moveToDest.y += floorOffset;
}
}
}
UpdateMove();
}
private void UpdateMove()
{
if((moveToDest != Vector3.zero) && (transform.position != moveToDest))
{
Vector3 direction = (moveToDest - transform.position).normalized;
direction.y = 0;
transform.rigidbody.velocity = direction * speed;
if(Vector3.Distance(transform.position, moveToDest) < stopDistanceOffset)
{
moveToDest = Vector3.zero;
}
}
else
{
transform.rigidbody.velocity = Vector3.zero;
}
}
}
Here is the CameraOperator script.
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 ();
}
public 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;
}
}
}
public 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 :(得分:0)
您的InvertMouseY
函数未标记为static
,但您尝试将其称为静态函数,而不是实例成员。 CameraOperator.InvertMouseY(camPos.y)
是静态调用(您通过类型名称CameraOperator
调用函数,而不是通过CameraOperator
的实例调用。
由于InvertMouseY
函数似乎不依赖于任何类型的状态(只有它的单个输入参数),解决此问题的最简单方法是将static
添加到函数的声明中。
public static float InvertMouseY (float y)
{
return Screen.height - y;
}