[unity learning] RPG Leaning(一)
写这个文章的目的就是为了初学unity,然后更好的掌握unity中的内容【主要是代码】
学习unity的途径是 Brackeys 的教程;
Bake 烘焙
一开始只知道模型需要烘焙,没有想到unity中人物可以移动的路径也需要烘焙。
1、给Player添加Nav Mesh Agent 组件
给player【玩家】添加Nav Mesh Agent 组件
2、设置所有的环境为Static
3、Window -> AI -> Navigation
设置物体的是否可以行走?
最后就是bake了
4、如果需要设置一个可以移动的障碍:
设置该物体,不是Static,以及添加Nav Mesh Obstacle ,在下面勾选Carve;
完成了
设置Player的移动的代码
设置了两个类「playerController 和 playerMotor 」
playerController:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//引入playerMotor模块
[RequireComponent(typeof(playerMotor))]
public class playerCotroller : MonoBehaviour
{
Camera cam;
playerMotor motor;
public LayerMask movementMask;
void Start()
{
//cam = Camera.main来拿到mainCamera
cam = Camera.main;
motor = GetComponent<playerMotor>();
}
void Update()
{
//点击鼠标左键
if (Input.GetMouseButtonDown(0))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
//这里是一个ray的概念,【A ray is an infinite line starting at origin and going in some direction.】
// https://en.wikipedia.org/wiki/Ray_casting [raycast的概念]
RaycastHit hit;
//Structure used to get information back from a raycast.
//https://docs.unity3d.com/ScriptReference/RaycastHit.html
if (Physics.Raycast(ray, out hit, 100, movementMask))
{
motor.MoveToPoint(hit.point);
//point The impact point in world space where the ray hit the collider.
}
}
//点击鼠标右键
if (Input.GetMouseButtonDown(1))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
Debug.Log("We hit" + hit.collider.name + hit.point);
}
}
}
}
playerMotor
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI; //主要是这个模块来移动物体
[RequireComponent(typeof(NavMeshAgent))]
public class playerMotor : MonoBehaviour
{
NavMeshAgent agent
void Start()
{
agent = GetComponent<NavMeshAgent>();
}
public void MoveToPoint(Vector3 point)
{
agent.SetDestination(point); //到hit的点处
}
}
设置Main Camera的移动的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControll : MonoBehaviour
{
// 这里的target就是player
public Transform target;
//offset设置相机与player的距离
public Vector3 offset;
private float currentZoom = 10f;
private float pitch = 2f;
private float zoomSpeed = 4f;
private float zoomMin = 5f;
private float zoomMax = 15f;
private float currentYaw = 0f;
private float yawSpeed = 100f;
void Update()
{
//鼠标滚轮的滑动
currentZoom -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
currentZoom = Mathf.Clamp(currentZoom, zoomMin, zoomMax);
//按键盘"a" 和"d" 左右移动视角
currentYaw += Input.GetAxis("Horizontal") * yawSpeed * Time.deltaTime;
}
void LateUpdate()
{
transform.position = target.position - offset * currentZoom;
transform.LookAt(target.position + Vector3.up * pitch);
///哈哈哈 LookAt我会!
transform.RotateAround(target.position, Vector3.up, currentYaw);
//Rotates the transform about axis passing through point in world coordinates by angle degrees.
//https://docs.unity3d.com/ScriptReference/Transform.html
}
}
总结
这里由于涉及到很多关于3d方面的事情:
ray ;ray cast以及LookAt这些专用的3d模型渲染需要专业知识,大家如果不了解的可以去看看;
这里有许多API,可以在: Unity API中找到。