Unity学习——跑酷游戏第三到四天

关卡的解锁设计

在游戏胜利页面这样写

PlayerPrefs.SetInt("levelReached", levelToUnlock);

关卡选择页面UI 按钮页面
通过存储公共数值的方式判断是不是可以进入这个关卡、第一关是可以进入的所以将值一开始赋值为1;
在每一关赋予相应的值(好像有一点点bug)

public class LevelSelect : MonoBehaviour
{
    public Button[] levelbuttons;
    // Start is called before the first frame update
    void Start()
    {
        int levelReached = PlayerPrefs.GetInt("levelReached", 1);
        for (int i = 0; i < levelbuttons.Length; i++)
        {
            if (i + 1 > levelReached)
            {
                levelbuttons[i].interactable = true;
            }

        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

使游戏物体可以左右移动

代码思路:
设置游戏物体的速度,在超过规定左右移动的范围的时候,将速度变成其相反值便可以了。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LeftRightMove : MonoBehaviour
{
    public float speed = 3;
    public float left = -5;
    public float right = 1;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.position += Vector3.right * speed * Time.deltaTime;
        if (transform.position.x >= right || transform.position.x <= left)
        {
            speed = -speed;
        }
    }
}

尖刺的设计

在阿里图标库找到图片,做成尖刺。如果游戏物体于其发生碰撞事件,调用游戏结束的函数。

发射子弹的炮台的设计

获取要发射的游戏物体,给予其向左的初速,作为子弹,子弹(rigid body组件重力可以去掉),
将子弹的预制体拖在代码公共部分。
在这里插入图片描述

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shoot : MonoBehaviour
{
    public int Speed = 20;
    public Rigidbody Bullet;
    public Transform FPonit;
    public float shootSpeed = 2;
    private float shootTimer = 0;
    private float shootTimerInterval = 0;
    // Start is called before the first frame update
    void Start()
    {
        shootTimerInterval = 1 / shootSpeed;
    }

    // Update is called once per frame
    void Update()
    {
        shootTimer += Time.deltaTime;
        if (shootTimer > shootTimerInterval)
        {
            shootTimer -= shootTimerInterval;
            shoot();
        }
    }
    void shoot()
    {
        Rigidbody clone;
        clone = (Rigidbody)Instantiate(Bullet, FPonit.position, FPonit.rotation);
        clone.velocity = transform.TransformDirection(Vector3.right * Speed);
    }
}

海绵的设计

给物体一个向上的速度。

public float movespeed = 5.0f;//角色踩在海面上之后的速度;
float blastmovespeed;
if (collision.gameObject.tag == "haimian")
        {
            Debug.Log("检测到海绵");
            blastmovespeed = movespeed * 2.0f;
            Vector3 sudu = new Vector3(0, blastmovespeed, 0);
            Playerrigidbody.velocity = sudu;
        }

关卡图片

第三关
在这里插入图片描述
第四关
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值