一些之前东西(2D关卡的练习代码摘要)

这篇博客分享了作者在2D关卡设计中进行的编程实践,通过代码摘要展示了如何构建和管理游戏关卡。内容涵盖了关卡布局、物体交互以及简单的游戏逻辑实现。

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

theLevel = FindObjectOfType<LevelManager>();//另一种写法:public LevelManager theLevel
isGround = Physics2D.OverlapCircle(checkPoint.position, radius, layerGround);//地面检测

//角色移动(2D)
   void FixedUpdate()
    {
        if (Input.GetAxisRaw("Horizontal") > 0)
        {
            rb.velocity = new Vector2(speed, rb.velocity.y);
            this.transform.localScale = new Vector3(1, 1, 1);
        }
        else if (Input.GetAxisRaw("Horizontal") < 0)
        {
            rb.velocity = new Vector2(-speed, rb.velocity.y);
            this.transform.localScale = new Vector3(-1, 1, 1);
        }
        else
        {
            rb.velocity = new Vector2(0, rb.velocity.y);
        }

        if (Input.GetAxisRaw("Jump") > 0 && isGround)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
        }
    }


        anim.SetFloat("speed",Mathf.Abs(rb.velocity.x)); //设定animation speed值,大于0.1f后动画切换为跑
        anim.SetBool("grounded",isGround);

	    if (isInvincible)
	    {
	        timeSpentInvincible += Time.deltaTime; //计时
	        if (timeSpentInvincible < 3f)  //小于3秒闪烁
	        {
	            float remainder = timeSpentInvincible % 0.3f;
	            GetComponent<Renderer>().enabled = remainder > 0.15f;
	        }
	        else
	        {
	            GetComponent<Renderer>().enabled = true;
	            isInvincible = false;
	            timeSpentInvincible = 0;

	        }

 
    public void Respawn()  //死亡协程
    {
        thePlayer.isInvincible = false;
        thePlayer.GetComponent<Renderer>().enabled = true;
        StartCoroutine("RespawnCo");
    }

    public IEnumerator RespawnCo()
    {
        thePlayer.gameObject.SetActive(false);
        Instantiate(deadEffect,thePlayer.transform.position,thePlayer.transform.rotation);
        yield return new WaitForSeconds(respawnDelay);
        thePlayer.gameObject.transform.position = thePlayer.nowRebirthVector2;
        healthCount = healthMax;
        UpdataHealth();
        
        thePlayer.gameObject.SetActive(true);
    }

	void Update () {
        //镜头跟随
        targetPos= new Vector3(player.transform.position.x,transform.position.y,transform.position.z);
	    if (player.transform.localScale.x>0)
	    {
	        targetPos = new Vector3(player.transform.position.x+ahead, transform.position.y, transform.position.z);
        }
	    else if (player.transform.localScale.x <0)
	    {
	        targetPos = new Vector3(player.transform.position.x-ahead, transform.position.y, transform.position.z);
        }
	  
		transform.position=Vector3.Lerp(transform.position,targetPos,smooth*Time.deltaTime);//顺滑过渡
	}

	void Update (){ //平台往复
	    moveThings.transform.position = Vector2.MoveTowards(moveThings.transform.position, targetPoint, moveSpeed * Time.deltaTime);

        if (moveThings.transform.position == startPoint.position)
        {
            targetPoint = endPoint.position;
        }
        else if (moveThings.transform.position == endPoint.position)
        {
            targetPoint = startPoint.position;
        }

    }





                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值