【发布时间】:2018-04-07 04:15:00
【问题描述】:
我正在尝试制作马里奥派对/棋盘游戏类型的游戏,但我似乎无法正确移动。我尝试通过导航网格移动玩家,目标是每回合都会改变并且可以在任何情况下改变。这就是我目前所拥有的控制器。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PlayerController : MonoBehaviour
{
public static NavMeshAgent Playeragent;
public static float MoveSpaces;
public bool ThisPlayersTurn;
public Transform MoveTo;
public Transform Dice;
public Vector3 DicePosition;
public bool HasHitDice;
// Use this for initialization
void Start()
{
GetComponent<Collider>();
//Dice.gameObject.SetActive(false);
//work
}
// Update is called once per frame
void Update()
{
//Debugging the NavMesh
//Playeragent.SetDestination(MoveTo.transform.position);
//Start of the turn.
if (ThisPlayersTurn == true)
{
//Moveplayer();
//Put the Dice above the player.
Dice.gameObject.SetActive(true);
Dice.transform.position = transform.position + DicePosition;
//Playeragent.SetDestination(MoveTo.transform.position);
}
//if it is not the players turn, disable the dice.
if (ThisPlayersTurn != true)
{
Dice.gameObject.SetActive(false);
}
// If it is this players turn and the dice has not been hit.
if (ThisPlayersTurn == true & HasHitDice == false)
{
// If the player pressed Jump and the dice is rotating.
if (Input.GetButtonDown("Jump") & DiceMotion.canrotate == true)
{
// Call the function for us to know how far to move.
DiceMotion.MoveSpaceAmount();
}
}
}
public void Moveplayer()
{
if(UIController.player1ismoving == true)
Debug.Log("We are moving");
Playeragent.SetDestination(MoveTo.transform.position);
}
这就是我为运动而得到的。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class MovementScript : MonoBehaviour {
public static NavMeshAgent Playeragent;
public Transform Pad1;
public Transform Pad2;
public Transform Pad3;
public Transform Pad4;
public Transform Pad5;
public Transform Pad6;
//public Transform Pad7;
public Transform Goal;
public static Transform GoHere;
public void Update()
{
if(PlayerController.Playeragent.nextPosition == Pad1.transform.position)
{
Debug.Log("Testing");
}
}
public void MovePlayer()
{
if (UIController.player1ismoving == true)
{
if (DiceMotion.canrotate == false)
{
Playeragent.SetDestination(Goal.transform.position);
}
}
}
//Test which pad we are on
private void OnTriggerEnter(Collider other)
{
//Spawn
if (gameObject.CompareTag("Spawn"))
{
if(PlayerController.MoveSpaces == 1) {
if(DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad1.transform.position;
}
}
if (PlayerController.MoveSpaces == 2)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad2.transform.position;
}
}
if (PlayerController.MoveSpaces == 3)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad3.transform.position;
}
}
}
//Pad1
if (gameObject.CompareTag("Pad1"))
{
if (PlayerController.MoveSpaces == 1)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad2.transform.position;
}
}
if (PlayerController.MoveSpaces == 2)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad3.transform.position;
}
}
if (PlayerController.MoveSpaces == 3)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad4.transform.position;
}
}
}
//Pad2
if (gameObject.CompareTag("Pad2"))
{
if (PlayerController.MoveSpaces == 1)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad3.transform.position;
}
}
if (PlayerController.MoveSpaces == 2)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad4.transform.position;
}
}
if (PlayerController.MoveSpaces == 3)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad5.transform.position;
}
}
}
//Pad3
if (gameObject.CompareTag("Pad3"))
{
if (PlayerController.MoveSpaces == 1)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad4.transform.position;
}
}
if (PlayerController.MoveSpaces == 2)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad5.transform.position;
}
}
if (PlayerController.MoveSpaces == 3)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad6.transform.position;
}
}
}
//Pad4
if (gameObject.CompareTag("Pad4"))
{
if (PlayerController.MoveSpaces == 1)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad5.transform.position;
}
}
if (PlayerController.MoveSpaces == 2)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad6.transform.position;
}
}
if (PlayerController.MoveSpaces == 3)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Goal.transform.position;
}
}
}
//Pad5
if (gameObject.CompareTag("Pad5"))
{
if (PlayerController.MoveSpaces == 1)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Pad6.transform.position;
}
}
if (PlayerController.MoveSpaces == 2)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Goal.transform.position;
}
}
if (PlayerController.MoveSpaces == 3)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Goal.transform.position;
}
}
}
//Pad5
if (gameObject.CompareTag("Pad6"))
{
if (PlayerController.MoveSpaces == 1)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Goal.transform.position;
}
}
if (PlayerController.MoveSpaces == 2)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Goal.transform.position;
}
}
if (PlayerController.MoveSpaces == 3)
{
if (DiceMotion.canrotate == false)
{
GoHere.transform.position = Goal.transform.position;
}
}
}
}
}
当按下跳跃按钮时,我设法让骰子停止旋转,并给玩家一些移动空间。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DiceMotion : MonoBehaviour {
public static bool canrotate = true;
public static float movenumber;
private void LateUpdate()
{
//Rotate the Dice.
if (canrotate == true)
{
transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
canrotate = false;
}
}
public static void MoveSpaceAmount()
{
movenumber = Random.Range(1, 6);
PlayerController.MoveSpaces = movenumber;
Debug.Log("You can move " + PlayerController.MoveSpaces + " spaces");
canrotate = false;
}
}
我似乎不知道如何解决这个问题。 如果问题仍然不清楚,即使使用此 UI 脚本强制玩家移动,角色也不会移动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.AI;
public class UIController : MonoBehaviour {
public static bool player1ismoving = false;
public static GameObject Player1;
public static NavMeshAgent p1a;
public void StartMovement()
{
p1a.SetDestination(MovementScript.GoHere.transform.position);
}
}
这是我在使用这些脚本时遇到的错误。
NullReferenceException: Object reference not set to an instance of an object
MovementScript.Update () (at Assets/Scripts/MovementScript.cs:21)
NullReferenceException: Object reference not set to an instance of an object
UIController.StartMovement () (at Assets/Scripts/UIController.cs:14)
UnityEngine.Events.InvokableCall.Invoke () (at
C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:165)
UnityEngine.Events.UnityEvent.Invoke () (at
C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at
C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()
NullReferenceException: Object reference not set to an instance of an object
UIController.StartMovement () (at Assets/Scripts/UIController.cs:14)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:165)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnSubmit (UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute (ISubmitHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:127)
UnityEngine.EventSystems.ExecuteEvents.Execute[ISubmitHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()
我尝试搜索这些东西,但似乎进展不顺利。很抱歉发了这么长的帖子。
编辑:我在项目中安装了 NavMesh 编辑器或示例。名为 GoHere 的对象是您可以看到的物理对象,并且在给定随机数时它不会改变位置。 EDIT2:将标题更改为如何根据变换更改导航网格目标,以使问题更易于理解。
【问题讨论】:
-
您是否阅读了错误信息? motionscript.cs 的第 21 行抛出空引用异常,因此 PlayerController.Playeragent 或 Pad1 为空。
-
感谢 lockstock,发现我只需要在启动函数中使用 Get Component。
-
排除错误,现在我必须处理实际的运动。