Unity 5 c#导航
我的问题是我的Unity 5 c#导航,c#,unity5,C#,Unity5,我的问题是我的 var tempTarget RtsManager.Current.ScreenPointToMapPosition(Input.mousePosition); 出于某种原因获取null值,即使我告诉它在mouseclick上获取值 我试图了解为什么它没有得到值,但似乎无法解决问题 public class RtsManager : MonoBehaviour { public static RtsManager Current = null; public
var tempTarget RtsManager.Current.ScreenPointToMapPosition(Input.mousePosition);
出于某种原因获取null值,即使我告诉它在mouseclick上获取值
我试图了解为什么它没有得到值,但似乎无法解决问题
public class RtsManager : MonoBehaviour
{
public static RtsManager Current = null;
public List<PlayerSetupDifinition> Players = new List<PlayerSetupDifinition>();
public TerrainCollider MapCollider;
public Vector3? ScreenPointToMapPosition(Vector2 point)
{
var ray = Camera.main.ScreenPointToRay(point);
RaycastHit hit;
if (!MapCollider.Raycast(ray, out hit, Mathf.Infinity))
return null;
return hit.point;
}
// Use this for initialization
void Start()
{
Current = null;
foreach (var p in Players)
{
foreach (var u in p.StartingUnits)
{
var go = (GameObject)GameObject.Instantiate(u, p.Location.position, p.Location.rotation);
if (!p.isAi)
{ go.AddComponent<RightClickNavigation>(); }
}
}
}
// Update is called once per frame
void Update()
{
}
}
public class RightClickNavigation : Interaction {
public float RelaxDistance = 5;
private NavMeshAgent agent;
private Vector3 target = Vector3.zero;
private bool selected = false;
private bool isActive = false;
public override void Deselect()
{
selected = false;
}
public override void Select()
{
selected = true;
}
public void SendToTarget()
{
agent.SetDestination(target);
agent.Resume();
isActive = true;
}
void Start () {
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update () {
if(selected && Input.GetMouseButtonDown (1))
{
var tempTarget = RtsManager.Current.ScreenPointToMapPosition(Input.mousePosition);
if (tempTarget.HasValue)
{
target = tempTarget.Value;
SendToTarget();
}
}
if(isActive && Vector3.Distance(target, transform.position) < RelaxDistance)
{
agent.Stop();
isActive = false;
}
}
}
公共类RtsManager:monobhavior
{
公共静态RtsManager Current=null;
公共列表玩家=新列表();
公共地面准直器地图对撞机;
公共向量3?屏幕指向对手(向量2点)
{
var ray=摄影机.main.ScreenPointRoay(点);
雷卡斯特击中;
if(!MapCollider.Raycast(光线,命中率,数学无穷大))
返回null;
返回命中点;
}
//用于初始化
void Start()
{
电流=零;
foreach(玩家中的var p)
{
foreach(以p.启动单位表示的var u)
{
var go=(GameObject)GameObject.Instantiate(u,p.Location.position,p.Location.rotation);
如果(!p.isAi)
{go.AddComponent();}
}
}
}
//每帧调用一次更新
无效更新()
{
}
}
公共类RightClickNavigation:交互{
公共浮动相对距离=5;
私人代理;
私有向量3目标=向量3.0;
选择的私有布尔=假;
private bool isActive=false;
公共覆盖无效取消选择()
{
所选=假;
}
公共覆盖无效选择()
{
所选=真;
}
公共无效发送目标()
{
设置目的地(目标);
agent.Resume();
isActive=true;
}
无效开始(){
agent=GetComponent();
}
//每帧调用一次更新
无效更新(){
如果(已选择&&Input.GetMouseButtonDown(1))
{
var testarget=RtsManager.Current.screenpointomposition(Input.mousePosition);
if(试探目标HasValue)
{
目标=目标值;
SendToTarget();
}
}
if(isActive&&Vector3.距离(目标、变换、位置)
在这里发布您的代码,而不是图像。我只是用上面的代码编辑了文章。请阅读我解决了问题,我必须更改Current=null;到电流=此;