[unity learning] RPG Leaning(一)

[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中找到。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值