Unity2D学习笔记三(Bilibili视频参考)——秋梦汐

实现陷阱(困难)、移动木棍和游戏音效

一、实现陷阱(困难)

(一)、设计陷阱

和之前一样,添加Saw,但是其他三个Saws、Point1、Point2为EmptyGameObject

让Point1、2更容易清楚地被观察到

设置Saw  Tag标签,目的是当角色碰到这个标志地物品会触发死亡

给Saw添加图片

为了区分开图层,新建一个Layer1分配给Tilemap,而Default0分配给Backgorund,剩下的角色,物品都分配为Default1

(二)、编写代码

需要修改的:

1.PlayerMovement的C# Script

新建的:

1.WayMovePoint的C# Script

 [SerializeField] private GameObject[] waypoints;
 private int currentWayPointIndex = 0;
 [SerializeField] private float speed = 2f;

 private void Update()
 {
     if (Vector2.Distance(waypoints[currentWayPointIndex].transform.position, transform.position) < .1f)
     {
         currentWayPointIndex++;
         if (currentWayPointIndex >= waypoints.Length)
         {
             currentWayPointIndex = 0;
         }
     }
     transform.position = Vector2.MoveTowards(transform.position, waypoints[currentWayPointIndex].transform.position, Time.deltaTime * speed);
 }

2.Rotation的C# Script 

[SerializeField] private float speed = 2f;

private void Update()
{
    transform.Rotate(0,0,360*speed*Time.deltaTime);
}

(三)、添加组件

额外添加两个新建脚本与Box Collider2D

对Way Point Move(Script)进行编辑(WayPoints添加Point)

二、移动木棍

(一)、设计木棍

新建的GameObject与Saw一样都是一个Platforms包括着Platform、Point1、2

Platform的Tag不需要更改,因为脚本没有调用Platform的Tag标志

Sprite Renderer中的Sprite和之前一样将素材图片托移到这里

(所有的角色,物品图层都为Default1)

(二)、编写代码

只要使用之前创建的Way Point Move脚本就行(不需要重新建,调用之前的就行)

(三)、添加组件

Platform添加两个Box Collider 2D,第一个作为实体,第二个作为标签

目的是我也不清楚,反正更好观赏?

第二个设置标签需要勾选

(如果第二个的Edit Collider和我一样没有,就先编辑好第一个然后在添加第二个)

三、游戏音效

(一)、导入素材

(二)、编写代码

添加角色跳跃声音:

 [SerializeField] private AudioSource jumpSoundEffect;//在PlayerMovement全局变量添加

/*将PlayerMovement中的Update全部替换为下面的代码*/
 void Update()
 {
    transform.rotation = Quaternion.Euler(0,0,0);
     dirX = Input.GetAxisRaw("Horizontal");
     rb.velocity = new Vector3(dirX * moveSpeed, rb.velocity.y,0);
     if (Input.GetButtonDown("Jump") )
     {
        jumpSoundEffect.Play();
         if(IsGrounded())
         rb.velocity = new Vector3(rb.velocity.x,jumpForce,0);
     }
     UpdateAnimationState();
     
 }

添加收集东西声音:

[SerializeField] private AudioSource collectSoundEffect;//添加的全局变量

/*将Item_Collect脚本中的OnTriggerEnter2D函数替换成下面的*/

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.gameObject.CompareTag("cherry"))
    {
        collectSoundEffect.Play();
        Destroy(collision.gameObject);
        cherries++;
        cherriesText.text = "Cherries:" + cherries;
        GameManage.Instance.score = cherries;
    }
}

添加角色死亡声音:

[SerializeField] private AudioSource deathSoundEffect;/添加全局变量/


/*将PlayerLife的OnCollisionEnter2D函数替换成下面的代码*/

 private void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.CompareTag("saw")) 
     {
         deathSoundEffect.Play();
         Die();
     }
     if (collision.gameObject.CompareTag("Trap"))
     {
         deathSoundEffect.Play();
         Die();
     }
 }

(三)、添加组件

在Player属性处添加组件Audio Source

1.寻找合适音频

2.将音频拖入组件Audio Source

3.将组件托移到脚本(任意比如跳Jump Sound Effect)的特定属性

添加三个音频,分别分给Jump、Collect、Death

这是我推荐用的音效,如果实在懒得找可以用这三个

另外推荐BGM(背景音乐)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值