金币游戏

编程入门 行业动态 更新时间:2024-10-09 20:24:45

<a href=https://www.elefans.com/category/jswz/34/1752570.html style=金币游戏"/>

金币游戏

发生碰撞的关系

墙–游戏玩家

  • 目标:碰撞在游戏玩家上,碰撞发生方法里,出现墙,游戏结束

  • tag

    • 游戏玩家–Player
    • 墙–Enermy
  • 组件

    • Player

      • Capsule Collider
      • Rigiidbody(旋转冻结)

游戏玩家–金币

碰撞在金币上,触发方法里,出现游戏玩家,金币增加,玩家可以通过金币

  • tag

    • 金币–Coin
  • 组件

    • Coin

      • Sphere Collider(勾选触发)

代码实现部分

PlayerControl

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerControl : MonoBehaviour
{// Start is called before the first frame update//刚体private Rigidbody rbody;//吃到的金币个数private int CoinCount;void Start(){//获取刚体组件rbody = GetComponent<Rigidbody>();}// Update is called once per framevoid Update(){//获取水平轴float horizontal = Input.GetAxis("Horizontal");//获取垂直轴数值float vertical = Input.GetAxis("vertical");//移动方向向量Vector3 dir = new Vector3(horizontal,0,vertical);//如果向量存在,证明按下了方向键if (dir!=Vector3.zero){//转向该向量transform.rotation = Quaternion.LookRotation(dir);//向前方移动rbody.velocity = 3 * dir.normalized;}else{//停止移动rbody.velocity = Vector3.zero;}}//发生碰撞private void OnCollisionEnter(Collision collision){//如果碰到敌人,这里是墙壁if (collision.collider.tag=="Enemy"){//游戏结束Debug.Log("游戏失败,游戏结束");//游戏停止Time.timeScale = 0;}}//增加金币的方法public void AddCoin(){//增加金币个数CoinCount++;//如果吃够5个金币if (CoinCount >= 5){//游戏结束Debug.Log("游戏成功,游戏结束");}//游戏停止Time.timeScale = 0;}}

CoinControl

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CoinControl : MonoBehaviour
{//发生碰撞private void OnTriggerEnter(Collider other){if (other.tag =="Player"){//如果触发执行,证明玩家角色碰到金币,增加玩家现有金币other.GetComponent<PlayerControl>().AddCoin();//删除自己Destroy(gameObject);}}
}

更多推荐

金币游戏

本文发布于:2024-02-07 01:30:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1752110.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:金币   游戏

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!