金币游戏"/>
金币游戏
发生碰撞的关系
墙–游戏玩家
-
目标:碰撞在游戏玩家上,碰撞发生方法里,出现墙,游戏结束
-
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);}}
}
更多推荐
金币游戏
发布评论