文章目录
一、基础知识
1.准备
Unity安装: https://unity.cn
2.基础知识
1.层级(Layer)
值越大,越后渲染,值大的游戏物体会覆盖值小的游戏物体。(初始为0)
Inspector->Sprite Renderer->Addititional Settings->Order in Layer.
2.轴心点
游戏物体的"锚点",以此为支点进行旋转,坐标点位置则是指轴心点的位置。
在Project中选择图片->Inspector->点击Sprite Editor进入编辑->下图黄色箭头所指位置的蓝色圆圈就是轴心点->移动蓝色圆圈可改变轴心点位置。(默认0.5,0.5)
3.预制体(Prefab)
是一个或者一系列组件的集合体,可以使用预制体实例化克隆体,后续可对克隆属性进行统一修改。
选择Assets右键->Create->folder->将名改为perfabs->...
4.刚体组件(Rigidbody)
使游戏物体能获得重力,接受外界的受力和扭力功能的组件,可通过脚本或者物理引擎为游戏对象添加钢铁组件。
2D游戏Gravity Scale设置为0,否则游戏物品会往下掉。
5.碰撞器组件(BoxCollider)
使游戏物体具有跟挂载刚体组件的游戏物体发送碰撞能力的组件。

二、代码
1.移动

csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LunaController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//Application.targetFrameRate = 60;//帧率
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxis("Horizontal");//获取水平
float vertical = Input.GetAxis("Vertical");//垂直
Vector2 position = transform.position;
position.x = position.x + 2*horizontal*Time.deltaTime;
position.y = position.y + 2* vertical*Time.deltaTime;
transform.position = position;
}
}