【Unity】My First Game_07

这篇博客介绍了在Unity中实现死亡线检测、场景切换、代码优化和视觉差效果的方法。通过创建DeadLine对象并设置触发器,实现玩家碰撞死亡及播放相应音效。同时,利用Input.GetKeyDown切换场景,优化了PlayerController和Animator代码,解决了跳跃和下蹲的手感问题。此外,实现了Parallax效果,增强了背景视觉体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

场景控制-死亡线

  1. 创建Empty,命名为DeadLine,添加Box Collider 2D组件,并调整好位置大小,选中Is Trigger,并设置tag为DeadLine

  2. 给Player添加一个dead音效

  3. 在PlayerController.cs中,修改代码:

    private void OnTriggerEnter2D(Collider2D collision)
    {
        //收集物品
        if (collision.tag == "Collection")
        {
            cherryAudio.Play();
            Destroy(collision.gameObject);
            cherry += 1;
            CherryNum.text = cherry.ToString();
        }
        
        //碰撞DeadLine
        if (collision.tag == "DeadLine")
    		{
        		GetComponent<AudioSource>().enabled = false;//关闭第一个音频
          	deadAudio.Play();//播放死亡音效
        		Invoke("Restart",1.5f);//延迟触发Restart方法
    		}
    }
    
    void Restart()
    {
        //碰撞DeadLine
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
    

场景控制-场景切换

  1. 创建“EnterHouse.cs”,添加代码:

    void Update()
    {
      	//按下E切换场景
    		if (Input.GetKeyDown(KeyCode.E))//key按下,bottom按住
       	{
       			SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+1);
       			//buildIndex编号
       	}
    }
    

    场景编号可在File -> Build Settings中将场景拖进Scenes in Build中查看

    将脚本拖拽进EnterDialog对象中,即可实现场景切换

代码优化

  1. 删除idle有关代码以及Animator窗口中相关属性

  2. 贴图有蓝线:Grid的Inspector窗口中的Grid中的Cell Size的XY改为0.99

  3. 跳跃下蹲手感问题:

    void FixedUpdate()
    {
        //有关Time.FixedDeltaTime的代码放在FixedUpdate中
        if(!isHurt)
        {
            Movement();//Movement中都使用Time.FixedDeltaTime
        }
        SwitchAnim();
    }
    
    private void Update()
    {
    		//在Update中调用这两个方法,在FixedUpdate中调用Movement方法
    		//在Movement方法中删除跳跃和下蹲代码
        Jump();
        Crouch();
    }
    
    void Jump()
    {
    		//角色跳跃
        if (Input.GetButton("Jump") && coll.IsTouchingLayers(ground))
        {
        		//注意使用GetButtom
    				rb.velocity = new Vector2(rb.velocity.x, jumpForce * Time.fixedDeltaTime);
            jumpAudio.Play();
            anim.SetBool("jumping", true);
        }
    }
    

视觉差Parallax

  1. 将BackGround的Collider移动至新的Object,命名为Area

  2. 新建Script,命名为Parallax.cs,添加代码:

    public Transform cam;
    public float moveRate;
    private float startPointX,startPointY;
    public bool lockY;
    
    // Start is called before the first frame update
    void Start()
    {
        startPointX = transform.position.x;
        startPointY = transform.position.y;
    }
    
    // Update is called once per frame
    void Update()
    {
        if (lockY)
        {
            transform.position = new Vector2(startPointX + cam.position.x * moveRate, transform.position.y);
        }
        else
        {
            transform.position = new Vector2(startPointX + cam.position.x * moveRate, startPointY + cam.position.y * moveRate);
        }
    }
    
  3. Parallax.cs拖拽进BackGround,将MainCamera拖拽进Cam,并修改Move Rate,并选择是否lockY

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值