YiXuCodeMonkey 2022-04-11 23:52 采纳率: 33.3%
浏览 135
已结题

unity脚本出现BUG,关于寻路的问题

敌人的寻路相关问题报错,用的是unity自带的navigation,目前只有一处错误,求解答
还有就是玩家的镜头问题,现在玩家的视角,只能左右移动,上下没有办法移动,这个是代码出现问题了么
再者就是玩家的前后左右移动问题,不能跟随视角移动,按W键只能前进,转移视角之后,就要按A或者D键前进,这是什么原因,怎么解决
敌人的寻路代码

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour 
{
    Transform m_transform;
    Player m_player;
    NavMeshAgent m_agent;
    float m_movSpeed = 2.5f;
    void Start()
    {
        m_transform = this.transform;
        m_player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
        m_agent = GetComponent<NavMeshAgent>();
        m_agent.speed = m_movSpeed;
        m_agent.SetDestination(m_player.m_transform.position);
    }

}


玩家代码

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour 
{
    public Transform m_transform;
    CharacterController m_ch;
    float m_movSpeed = 5.0f;
    float m_gravity = 6.0f;
    public int m_life = 5;
    public float m_jumpSpeed = 10.0f;
    private Vector3 m_movDirection = Vector3.zero;
    Transform m_camTransform;
    Vector3 m_camRot;
    float m_camHeight = 1.4f;
    void Start()
    {
        m_transform = this.transform;
        m_ch = this.GetComponent<CharacterController> ();
        m_camTransform = Camera.main.transform;
        Vector3 pos = m_transform.position;
        pos.y += m_camHeight;
        m_camTransform.position = pos;
        m_camTransform.rotation = m_transform.rotation;
        m_camRot = m_camTransform.eulerAngles;
        Screen.lockCursor = true;
    }
    void Update()
    {
        if (m_life <= 0)
            return;
        Control ();
    }
    void Control()
    {
        float rh = Input.GetAxis ("Mouse X");
        float rv = Input.GetAxis ("Mouse Y");
        m_camRot.x -= rv;
        m_camRot.y += rh;
        m_camTransform.eulerAngles = m_camRot; 
        Vector3 camRot = m_camTransform.eulerAngles;
        camRot.x = 0;
        camRot.z = 0;
        m_camTransform.eulerAngles = camRot;
        Vector3 pos = m_transform.position;
        pos.y += m_camHeight;
        m_camTransform.position = pos;
        float xm = 0, ym = 0, zm = 0;
        ym -= m_gravity * Time.deltaTime;
        if (Input.GetKey (KeyCode.W)) 
        {
            zm += m_movSpeed * Time.deltaTime;
        }
        else if (Input.GetKey (KeyCode.S)) 
        {
            zm -= m_movSpeed * Time.deltaTime;
        }
        if (Input.GetKey (KeyCode.A)) 
        {
            xm -= m_movSpeed * Time.deltaTime;
        }
        else if (Input.GetKey (KeyCode.D)) 
        {
            xm += m_movSpeed * Time.deltaTime;
        }
        if (Input.GetKeyDown (KeyCode.Space)) 
        {
            m_movDirection.y = m_jumpSpeed;
        }
        m_movDirection.y -= m_gravity * Time.deltaTime;
        m_ch.Move (m_transform.TransformDirection (new Vector3 (xm, ym, zm)));
        m_ch.Move (m_movDirection * Time.deltaTime);
    }


}


报错内容
UnassignedReferenceException: The variable m_transform of Player has not been assigned.
You probably need to assign the m_transform variable of the Player script in the inspector.
UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/EditorGenerated/UnityEngineTransform.cs:28)
Enemy.Start () (at Assets/Scripts/Enemy.cs:16)

具体的现象就是,敌人的模型站在那不动

  • 写回答

3条回答 默认 最新

  • 漫步云巅Kim 2022-04-12 15:59
    关注

    是不是时序问题?同个start上。你可以把player的Start改成Awake试试。
    还有其实直接用player.transform就可以了,不需要再用一个m_transform来初始化获取,然后把Player.cs挂到你要的模型预制件上。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月21日
  • 已采纳回答 4月13日
  • 赞助了问题酬金30元 4月11日
  • 修改了问题 4月11日
  • 展开全部

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题