敌人的寻路相关问题报错,用的是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)
具体的现象就是,敌人的模型站在那不动