【持剑勇士】原型设计部分小结

时间:2022-05-17 00:01:28

【1】学习中的使用的类

1.cursor:用于设置光标.

本例中使用的方法:Cursor.SetCursor(Texture2D,CursorMode):为光标设置纹理。

2.Phisics:全局物理属性和方法。

本例中使用的方法:public static bool Raycast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction ):

投射一条Ray射线检测碰撞,返回Bool值。

3. RaycastHit:用于从光线投射中获取信息的结构。

本例中使用的属性:

public Collider collider:返回一个Collider,如果未碰撞则为Null。

public  Vector3  point;射线撞击对撞机时在世界空间中的撞击点(本例中地面已经设置了一个BoxCollider)。

4.Camera:相机是玩家通过其观看世界的设备。

本例中使用的属性:public static Camera Main:返回主摄像机。

本例中使用的方法:public Ray ScreenPointToRayVector3 pos):返回从相机通过屏幕点的光线。

5.Input:使用此类读取常规游戏设置中的轴,访问移动设备的多点触控和加速度。

本例中使用的属性:public static Vector3 mousePosition:返回当前鼠标在像素坐标中的位置。

本例使用到的方法:public static bool GetMouseButtonDown(int button);在用户按下给定鼠标按钮的帧期间返回true。

6.GameObject:Unity场景中所有实体的基类。

本例使用到的方法:GameObject FindGameObjectWithTag(string tag):返回对应标签的游戏对象。

7.MonoBehaviour:是每个Unity脚本派生的基类。

本例使用到的方法:public void InvokeRepeating(string methodName,float time,float repeatRate);

time秒为单位调用方法,然后每秒重复一次repeatRate。如果time=0;则立即调用方法。

8.:NavMeshAgent:导航网格代理。

本例使用的属性:public Vector3 destination:获取或尝试以世界空间单位设置的代理的目标。

【2】学习中用到的组件

1.Nav Mesh Surface:它的作用Navigation窗口的作用类似,用来渲染出NavMesh,需要参与渲染的物体不需要勾选Navigation Static,只需要是在Collect Objects中操作,包括的物体以及该物体所在层。 

2.Nav Mesh Agent:添加到寻路物体上。

3.Nav Mesh Obstacle:添加到障碍物上。

4.Nav Mesh Modifier:基于层次结构影响NavMesh区域类型的NavMesh生成。

【3】碰到的问题

1.代表位置的对象不能挂在寻路物体下面,否则无法读取正确的位置数据(原因未知)。

 

public class NPCController : MonoBehaviour
{
    public float patrolTime = 10f;
    public float aggroRange = 10f;
    public Transform[] waypoints;
    private int index;
    private float speed, agentSpeed;
    private Transform player;
    private int x = 0;
    private NavMeshAgent agent;

    private void Awake()
    {
        Debug.Log(waypoints[0].position);
        Debug.Log(waypoints[1].position);
        agent = GetComponent<NavMeshAgent>();
        if (agent != null)
        {
            agentSpeed = agent.speed;
        }
        player = GameObject.FindGameObjectWithTag("Player").transform;
        
        //index = 1;
        //index = Random.Range(0, waypoints.Length);

        InvokeRepeating("Tick", 0, 0.5f);
        if (waypoints.Length > 0)
        {
            InvokeRepeating("Patrol", 0, patrolTime);
        }
    }
    void Patrol()
    {
        index = (index == waypoints.Length - 1 ? 0 : index + 1);
  }
    void Tick()
    {
        //bool s = agent.SetDestination(waypoints[index].position);
        //Debug.Log(s);
        agent.destination = waypoints[index].position;
        Debug.Log(waypoints[index].position);
        //agent.destination = waypoints[index].transform.position;
        //Debug.Log(waypoints[index].transform.position);
        agent.speed = agentSpeed / 2;

        if (player != null && Vector3.Distance(transform.position, player.position) < aggroRange)
        {
            agent.destination = player.position;
            agent.speed = agentSpeed;
            Debug.Log(123);
        }
    }

}
public class MourseManager : MonoBehaviour
{
    public LayerMask clickableLayer;
    public Texture2D pointer;
    public Texture2D target;
    public Texture2D doorway;
    public Texture2D combat;
    public EventVector3 OnClickEnvironmrnt;
    void Update()
    {
        RaycastHit hit;
        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 50, clickableLayer.value))
        {
            bool door = false;
            bool item = false;
            if (hit.collider.gameObject.tag == "Doorway")
            {
                Cursor.SetCursor(doorway, new Vector2(16, 16), CursorMode.Auto);
                door = true;
            }
            else if (hit.collider.gameObject.tag == "Item")
            {
                Cursor.SetCursor(combat, new Vector2(16, 16), CursorMode.Auto);
                item = true;
            }
            else
            {
                Cursor.SetCursor(target, new Vector2(16, 16), CursorMode.Auto);
            }          
            if (Input.GetMouseButtonDown(0))
            {
                if (door)
                {
                    Transform doorway1 = hit.collider.gameObject.transform;
                    OnClickEnvironmrnt.Invoke(doorway1.position);
                    Debug.Log("DOOR");

                }
                else if (item)
                {
                    Transform itemPos = hit.collider.gameObject.transform;
                    OnClickEnvironmrnt.Invoke(itemPos.position);
                    Debug.Log("Item");
                }
                else
                {
                    OnClickEnvironmrnt.Invoke(hit.point);
                }
            }          
        }
        else
        {
            Cursor.SetCursor(pointer, Vector2.zero, CursorMode.Auto);
        }
    }
}
[System.Serializable]
public class EventVector3 : UnityEvent<Vector3>{}