死亡方法
public GameObject explosionprefab;
Instantiate(explosionprefab, transform.position, transform.rotation);
Destroy(动画特效)
collision.SendMessage('Die')
死亡方法
public GameObject explosionprefab;
Instantiate(explosionprefab, transform.position, transform.rotation);
Destroy(动画特效)
collision.SendMessage('Die')
private SpriteRenderer sr;
public Sprite[] tankSprite; (直接拖到数组里)
sr = GetComponent<SpriteRenderer>();
if (h<0)
{
sr.Sprite = tankSprite[3];
}
else if (h>0)
{
sr.Sprite = tankSprite[1];
}
public float moveSpeed = 3;
float h = Input.GetAxisRaw("Horizontal");
transform.Translate(Vector3.right*h*moveSpeed*Time.deltaTime,Space.World);
float v = Input.GetAxisRaw("Vertical");
transform.Translate(Vector3.up*v*moveSpeed*Time.deltaTime,Space.World);
2D UI要反过来
0 8 16 24
都要有碰撞器,一方要有刚体,最好是运动的
创建玩家,调整大小
2D素材,类型要设为2d
多张图片需要切割,sprite mode 需要调成multiple
切割使用sprite editor,
the truth that you leave
sb
Sprite
sorting layer层级
orderinlayer小层级
越大越靠后
2D游戏界面 Z轴旋转 度数是相反的
触发检测的必要条件:
两方都有碰撞器,其中一方is trigger是勾选状态,其中一方有刚体组件,并且是运动的
发生碰撞的条件:
两个发生碰撞的物体都有碰撞器;
其中一方有刚体组件,并且最好是运动的
一旦一个刚体以低于某一最小线性或旋转速度运动,物理引擎就会假定它已经停止。当这种情况发生时,对象将不会再次移动,直到它收到碰撞或力,所以它将被设置为睡眠模式。
Update受当前渲染的物体影响,帧与帧的时间间隔不固定
FixedUpdate帧与帧的时间间隔固定,因此些物理属性的更新操作应该放在FxiedUpdate中操作,比如Force,Collider,Rigidbody等。外设的操作也是,比如说键盘或者鼠标的输入输出Input,因为这样GameObject的物理表现的更平滑,更接近现实。
Animation:存放动画文件
AnimatorController:存放动画控制器
创建2D动画:
连续的图片拖到hierarchy,命名动画文件,保存
2D游戏素材Texture Type要选择为Sprite(2D and UI)
Sprite Mode:
单张图片选择single,图集选择multiple
在Sprite Editor中进行图片的切割
碰撞条件 :两个发生碰撞的物体身上都要有碰撞器,其中一方身上要有刚体(这一方最好是运动的一方 因为物体长时间不运动 刚体组件会产生休眠 休眠之后就不会发生碰撞)
# 项目问题
1. 2D建议Game面板5:4比例,摄像头距离为8.5。
2. 移动:
- 获取水平垂直输入:
```c#
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
```
- 移动:
```c#
transform.Translate(Vector3.right * h * moveSpeed * Time.deltaTime, Space.World);
transform.Translate(Vector3.up * v * moveSpeed * Time.deltaTime, Space.World);
```
- ==Time.deltaTime: 每一帧所花费的时间==
3. 获取组件GetComponent<>();
- 获取图片渲染:
```c#
private SpriteRenderer sr;
sr = GetComponent<SpriteRenderer>();
赋值 sr.sprite = sprite
```
4. 添加刚体之后的元素在移动时候,出现抖动现象应该从Update移动到FixUpdate中执行(deltaTime 要改为 fixedDeltaTime)。
5. 监听键盘鼠标输入:
```c#
Input.GetKeyDown(KeyCode.Space)
Input.GetKeyUp(KeyCode.A)
```
6. 实例化物体 Instantiate(物体, 位置, 角度(四元数))
```c#
public GameObject bulletPrefab;
GameObject bullet = Instantiate(bulletPrefab, transform.position, transform.rotation);
```
7. 欧拉角(x、y、z),四元数(向量)
- 欧拉角转换为四元数
```
Quaternion rotation = Quaternion.Euler(0, 30, 0);
```
- Vector3 是欧拉角
```
Vector3 rotationVector = new Vector3(0, 30, 0);
```
8. transform.up 和 Vector3.up (Space.World)
- transform.up 是相对于自身移动,移动方向不一定为(0,1,0),但是模长一定是1.
- Vector3.up 相对于世界坐标移动,移动方向一定是(0,1,0)
9. 2D碰撞检测(触发):OnTriggerEnter2D()
```
private void OnTriggerEnter2D(Collider2D collision)
{
print(collision.tag);
print(collision.name);
}
```
- collision 被碰撞物体信息
- ==collision.SendMessage("Die"); 触发被碰撞物体内部方法==
10. 设置物体激活状态: SetActive(状态)
```
public GameObject bulletPrefab;
bulletPrefab.SetActive(true);
bulletPrefab.SetActive(false);
```
- SetActive(true); 设置物体启用
- SetActive(false); 设置物体禁用
11. 延时调用方法:Invoke(方法名, 延时时间秒)
```
Invoke("PlayerBorn", 0.8f);
private void PlayerBorn()
{
Instantiate(playerPrefab, transform.position, transform.rotation);
}
```
12. 重复调用方法:InvokeRepeating(方法名,多少秒之后立即调用,每隔多少秒调用一次)
13. 随机数(包左不包右):Random.Range(0, 1)
14. Quaternion.identity: 无旋转即:Quaternion(0,0,0,0)
15. SetParent: 设置父物体
```
GameObject gb = Instantiate(createGameObject, position, rotation);
gb.transform.SetParent(gameObject.transform);
```
16. 单例模式:
在使用 Unity 开发游戏的时候,经常会需要各种 Manager 类用于管理一些全局的变量和方法,例如最常见的 GameManager 用于记录各种需要在整个游戏过程中持久化的数据。本文以 GameManager 为例,假设我们有以下几个需求:
- 整个游戏过程中必须有且只有一个 GameManager
- 在 GameManager 里会记录一个叫 Value 的整型变量
- 切换游戏场景的时候 GameManager 不能被销毁
- 有两个游戏场景分别叫 FirstScene 和 SecondScene
- 每一个场景中都会有一个按钮,点击后会跳转到另一场景,并且对 GameManager.Value 进行 +1 操作
- 每一个场景中都会显示当前 GameManager.Value 的值
```
public class GameManager : MonoBehaviour
{
public static GameManager Instance { get; private set; }
public int Value { get; set; } = 0;
private void Awake()
{
Instance = this;
}
}
```
17. 声音播放:
- AudioSource.PlayClipAtPoint()
```
public AudioClip barriarAudio; // 获取声音资源
AudioSource.PlayClipAtPoint(barriarAudio, transform.position); // 播放(静态方法)
```
- AudioSource 和 AudioClip
- AudioSource 是声音控制
- AudioClip 是声音资源
```
public AudioSource moveAudio;
public AudioClip[] tankAudio;
if (!moveAudio.isPlaying)
{
moveAudio.clip = tankAudio[1];
moveAudio.Play();
}
```
- moveAudio.Play() 播放
- moveAudio.clip 设置声音资源
- moveAudio.volume = 0.5f; 声音大小0-1
if(Input.GetkeyDown(KeyCode.Space))
{
Instantiate()
}
tank的攻击方法