【问题标题】:How to change a nav mesh goal based on based on a transform如何基于变换更改导航网格目标
【发布时间】: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。
  • 排除错误,现在我必须处理实际的运动。

标签: c# unity3d


【解决方案1】:

错误在这里

 void Start()
{
    GetComponent<Collider>();
    //Dice.gameObject.SetActive(false);
    //work
}

您没有正确引用对撞机。

替换

GetComponent<Collider>();

Collider col  = GetComponent<Collider>();

另外我认为这个脚本中不需要碰撞器。

希望这会有所帮助。

【讨论】:

  • 错误已得到修复,但无论如何感谢。这条评论让我意识到我不需要 Collider。
【解决方案2】:

我只是为不同的路径制作了不同的脚本。我认为这将是我做到这一点的最佳方式。谢谢。 例如,这是其中之一。

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

public class Pad1 : MonoBehaviour {
// Use this for initialization
void Start () {
    //Collider padcollider = GetComponent<SphereCollider>();
}

private void OnTriggerEnter(Collider other)
{
    if (gameObject.CompareTag("Player"))
    {
        gameObject.GetComponent<PlayerController>();
        PlayerController.AtSpawn = false;
        PlayerController.AtPad1 = true;

    }
}
public static void MoveFromPad1 ()
{
    if (PlayerController.MoveSpaces != 0)
    {
        PlayerController.MoveSpaces -= 1;
        if (PlayerController.ThisPlayersTurn == true)
        {
            if (PlayerController.MoveSpaces == 1)
            {
                PlayerController.Playeragent.SetDestination(PadManager.IntPad2.transform.position);
                PlayerController.MoveSpaces -= 1;
            }
            if (PlayerController.MoveSpaces == 2)
            {
                PlayerController.Playeragent.SetDestination(PadManager.IntPad3.transform.position);
                PlayerController.MoveSpaces -= 1;
            }
            if (PlayerController.MoveSpaces == 3)
            {
                PlayerController.Playeragent.SetDestination(PadManager.IntPad4.transform.position);
                PlayerController.MoveSpaces -= 1;
            }
            if (PlayerController.MoveSpaces == 4)
            {
                PlayerController.Playeragent.SetDestination(PadManager.IntPad5.transform.position);
                PlayerController.MoveSpaces -= 1;
            }
            if (PlayerController.MoveSpaces == 5)
            {
                PlayerController.Playeragent.SetDestination(PadManager.IntPad6.transform.position);
                PlayerController.MoveSpaces -= 1;
            }
            if (PlayerController.MoveSpaces == 6)
            {
                PlayerController.Playeragent.SetDestination(PadManager.IntGoal.transform.position);
                PlayerController.MoveSpaces -= 1;
            }
        }
    }
}

}

下一步。试图弄清楚如何在踩到 Pad 时从播放器中获取脚本组件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多