碰撞检测 Unity实验代码

  • CharacterCollision.cs
  • RayCharacterCollision.cs
  • TriggerCollision.cs
  • CharacterCollision.cs(加入开门展示粒子效果)
  • RayCharacterCollision.cs(加入开门展示粒子效果)

CharacterCollision.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CharacterCollision : MonoBehaviour {private bool doorIsOpen = false;private float doorTimer = 0.0f;public float doorOpenTime = 3.0f;// Use this for initializationvoid Start () {}void OnControllerColliderHit(ControllerColliderHit hit){if (hit.gameObject.tag == "houseDoor" && doorIsOpen == false)OpenDoor ();}void OpenDoor(){doorIsOpen = true;GameObject myHouse = GameObject.Find ("house");myHouse.GetComponent<Animation> ().Play ("dooropen");}void ShutDoor(){doorIsOpen = false;GameObject myHouse = GameObject.Find ("house");myHouse.GetComponent<Animation> ().Play ("doorshut");}// Update is called once per framevoid Update(){if (doorIsOpen) {doorTimer += Time.deltaTime;if (doorTimer > doorOpenTime) {ShutDoor ();doorTimer = 0.0f;}}}
}

RayCharacterCollision.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class RayCharacterCollision : MonoBehaviour {private bool doorIsOpen = false;private float doorTimer = 0.0f;public float doorOpenTime = 3.0f;// Use this for initializationvoid Start () {}void OpenDoor(){doorIsOpen = true;GameObject myHouse = GameObject.Find ("house");myHouse.GetComponent<Animation> ().Play ("dooropen");}void ShutDoor(){doorIsOpen = false;GameObject myHouse = GameObject.Find ("house");myHouse.GetComponent<Animation> ().Play ("doorshut");}// Update is called once per framevoid Update(){RaycastHit hit;if(Physics.Raycast(transform.position,transform.forward,out hit,5)){if(hit.collider.gameObject.tag=="houseDoor"&&doorIsOpen==false){OpenDoor();}}if (doorIsOpen) {doorTimer += Time.deltaTime;if (doorTimer > doorOpenTime) {ShutDoor ();doorTimer = 0.0f;}}}
}

TriggerCollision.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class TriggerCollision : MonoBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {transform.Translate (transform.right * Input.GetAxis ("Horizontal") * Time.deltaTime, Space.Self);}void OnTriggerEnter(Collider other){Debug.Log ("Come to triggerEnter");if (other.gameObject.tag == "tagSphere")Destroy (other.gameObject);}
}

CharacterCollision.cs(加入开门展示粒子效果)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CharacterCollision : MonoBehaviour {private bool doorIsOpen = false;private float doorTimer = 0.0f;public float doorOpenTime = 3.0f;// Use this for initializationvoid Start () {StopPaticleSystemDemo ();}void OnControllerColliderHit(ControllerColliderHit hit){if (hit.gameObject.tag == "houseDoor" && doorIsOpen == false)OpenDoor ();}void OpenDoor(){doorIsOpen = true;GameObject myHouse = GameObject.Find ("house");myHouse.GetComponent<Animation> ().Play ("dooropen");}void ShutDoor(){doorIsOpen = false;GameObject myHouse = GameObject.Find ("house");myHouse.GetComponent<Animation> ().Play ("doorshut");}void OpenPaticleSystemDemo(){string particle="particle";GameObject.Find (particle).GetComponent<ParticleSystem> ().Play ();}void StopPaticleSystemDemo(){string particle="particle";GameObject.Find (particle).GetComponent<ParticleSystem> ().Stop ();}// Update is called once per framevoid Update(){if (doorIsOpen) {doorTimer += Time.deltaTime;OpenPaticleSystemDemo ();if (doorTimer > doorOpenTime) {ShutDoor ();doorTimer = 0.0f;}}}
}

RayCharacterCollision.cs(加入开门展示粒子效果)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class RayCharacterCollision : MonoBehaviour {private bool doorIsOpen = false;private float doorTimer = 0.0f;public float doorOpenTime = 3.0f;// Use this for initializationvoid Start(){StopPaticleSystemDemo ();}void OpenDoor(){doorIsOpen = true;GameObject myHouse = GameObject.Find ("house");myHouse.GetComponent<Animation> ().Play ("dooropen");}void ShutDoor(){doorIsOpen = false;GameObject myHouse = GameObject.Find ("house");myHouse.GetComponent<Animation> ().Play ("doorshut");}void OpenPaticleSystemDemo(){string particle="particle";GameObject.Find (particle).GetComponent<ParticleSystem> ().Play ();}void StopPaticleSystemDemo(){string particle="particle";GameObject.Find (particle).GetComponent<ParticleSystem> ().Stop ();}// Update is called once per framevoid Update(){RaycastHit hit;if(Physics.Raycast(transform.position,transform.forward,out hit,5)){if(hit.collider.gameObject.tag=="houseDoor"&&doorIsOpen==false){OpenDoor();OpenPaticleSystemDemo ();}}if (doorIsOpen) {doorTimer += Time.deltaTime;if (doorTimer > doorOpenTime) {ShutDoor ();doorTimer = 0.0f;}}}
}

碰撞检测 Unity实验代码相关推荐

  1. unity 实验演示 教程_铁匠常见问题解答和Unity演示团队

    unity 实验演示 教程 Since showing our short film The Blacksmith at GDC 2015 in March, we've received many ...

  2. TSP问题由标准格式转成简单格式(我的实验代码需要的格式)

    为什么80%的码农都做不了架构师?>>>    最近从网上收集tsp问题实例,因为网上的格式较为复杂,于是进行了一些小的转换.从而成为简单的格式,用于我的实验代码输入.我的代码需要的 ...

  3. Ch2r_ood_understanding 本文档为论文限定领域口语对话系统中超出领域话语的对话行为识别的部分实验代码。代码基于Python,需要用到的外部库有: Keras(搭建神经网络) S

    Ch2r_ood_understanding 本文档为论文限定领域口语对话系统中超出领域话语的对话行为识别的部分实验代码.代码基于Python,需要用到的外部库有: Keras(搭建神经网络) Sci ...

  4. java数据结构实验一顺序表,java数据结构实验代码之升序顺序表

    java数据结构实验代码之升序顺序表 数据结构实验报告 学院:管理学院 班级:13电子商务(1)班 姓名:廖秋君 学号:3213004779 2014年 10月 23 日 目录 一.需求分析----- ...

  5. python程序开发正则表达式_python正则表达式的使用(实验代码)

    正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. re 模块使 Python ...

  6. java实验报告合肥工业大学_合肥工业大学数据结构上机实验代码与实验报告(全)github地址...

    C++实现链队类--合肥工业大学数据结构实验5:链式队列 实验5 5.1 实验目的 熟练掌握队列的顺序链式存储结构. 熟练掌握队列的有关算法设计,并在链队列上实现. 根据具体给定的需求,合理设计并实现 ...

  7. 贪心算法 0-1背包c语言,贪心算法0-1背包问题(算法实验代码).pdf

    . 实验三. 0-1 背包问题(贪心算法) 实验代码: #include int max(int a,int b) { if(a>b) return a; else return b; } vo ...

  8. python正则表达式代码_python正则表达式的使用(实验代码)

    正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. re 模块使 Python ...

  9. Unity 之 代码获取IOS设备型号 -- 做分辨率适配

    Unity 之 代码获取IOS设备型号 一,通过机器型号校验 1.1 逻辑解释 1.2 实现代码 二,通过长宽比校验 一,通过机器型号校验 1.1 逻辑解释 在https://www.theiphon ...

最新文章

  1. 关于tesseract 出错信息:read_params_file: Can't open chi_sim
  2. 4路组相连cache设计_浅谈 Cache
  3. python入门之控制结构循环结构_python入门教程04-05(python语法入门之流程控制)...
  4. ListView vs FlatList vs RecyclerListView性能对比
  5. 【天池竞赛系列】阿里移动推荐算法思路解析
  6. python中的字体英文名,CSS 中文字体的英文名称
  7. 如何快速制作漫画App?
  8. 2021阿里云开发者大会——时隔两年平头哥有哪些杰作?
  9. matlab编程求卫星轨道长度,GPS卫星轨道计算及其MATLAB仿真.pdf
  10. python elif是什么意思_python elif是什么意思
  11. 自定义可自由移动的浮窗
  12. 在Python应用中Telegram 机器人搭建消息提醒
  13. u盘文件不见但还占用容量文件办法?
  14. ARM裸机篇(二)——i.MX6ULL启动过程
  15. 想提高团队技术,来试试这个套路! (作为个人提高的套路也行呢)
  16. 理解下DSO/ADSO
  17. mybatis-plus 分页类型转换工具类
  18. 情人节,送女友一桶代码可否?
  19. 在xsl中插入有大于、小于符号JavaScript,CSS代码的方法
  20. 羊驼alpaca php,今天,我们来聊聊羊驼

热门文章

  1. 记一次对vue双向绑定的理解
  2. Qt5+STM32F407+步进电机 | 通过电脑控制步进电机实现:6+2通道、速度可变、运动精确步数的教程——程序开发(3/4)
  3. linux get_user,linux内核中的get_user和put_user
  4. dependencies与devDependencies区别,以及peerDependencies的作用,npm2和npm3区别
  5. (3)一文懂“芯片”设计、代工、封测
  6. catia V5 2020打开Feature Name:AF_CAT_AFPLUS_TWrong host license(-4)
  7. 判断两直线正交 平行问题
  8. 利用新浪API实现数据的抓取\微博数据爬取\微博爬虫 1
  9. 在校大学生(非计算机专业)自学Python,兼职也能月入5k?
  10. 瑞星2008免费版下载