记录学习Unity制作游戏时的一些基本代码。
准备工作
Unity - Scripting API:
API | 官方文档地址 |
---|---|
Input.GetAxis | https://docs.unity3d.com/ScriptReference/Input.GetAxis.html |
Mathf.Lerp | https://docs.unity3d.com/ScriptReference/Mathf.Lerp.html |
Mathf.Clamp | https://docs.unity3d.com/2018.2/Documentation/ScriptReference/Mathf.Clamp.html |
Vector3.Lerp | https://docs.unity3d.com/530/Documentation/ScriptReference/Vector3.Lerp.html |
Vector3.magnitude | https://docs.unity3d.com/ScriptReference/Vector3-magnitude.html |
Vector3.Normalize | https://docs.unity3d.com/ScriptReference/Vector3.Normalize.html |
导入模型增加组件
- 为模型增加刚体(Rigidboday)组件(并冻结x,y,z旋转)
- 为模型增加胶囊碰撞器(Capsule Collider)
- 如下
人物的移动
代码
创建2个脚本,一个用来获取输入(PlayerInput),一个用来更新数据(PlayerController)
using UnityEngine;
public class PlayerInput : MonoBehaviour
{
public PlayerController pc;
void Start()
{
pc = GetComponent<PlayerController>();//获取PlayerController
}