当前位置:网站首页>碰撞检测 Unity实验代码
碰撞检测 Unity实验代码
2022-06-10 14:16:00 【CTGU_narcissistic_zh】
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 initialization
void 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 frame
void 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 initialization
void 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 frame
void 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 initialization
void Start () {
}
// Update is called once per frame
void 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);
}
}
边栏推荐
- Textinputlayout usage details
- [original] poi 5 X xssf and HSSF use custom font colors
- 【離散數學期複習系列】二、一階邏輯(謂詞邏輯)
- 北京/上海内推 | 微软亚洲研究院系统与网络组招聘全职实习生
- 为doc2vec生成训练向量的数据集
- 【笔记】74HC573的一些记录
- QT 基于QScrollArea的界面嵌套移动
- Flutter Icon Stack LIsttitle... Learning summary 3
- D:\setup Exe could not find the problem
- NC|王军/宋默识结合三代测序解析肠道菌群结构变异和功能
猜你喜欢

IIC总线的主要特点/通信过程/读写过程

Docker部署一个Redis集群

大厂必备的40个方法论

【笔记】74HC573的一些记录

Ue5 how to convert screen coordinates to world coordinates and World Directions

二叉树和图1

Microsoft Word 教程,如何在 Word 中更改页边距、创建新闻稿栏?
![[note] about the problem of insufficient compilation mapping memory in keil](/img/0d/789c7629823600dbc77e62744ef5a1.png)
[note] about the problem of insufficient compilation mapping memory in keil

2022年危险化学品生产单位安全生产管理人员考试模拟100题及在线模拟考试

【大咖秀】博睿数据眼中的AIOps,选择正确的赛道正确的人
随机推荐
How to solve the problem that vmware tools are grayed out when VMware Workstation is installed
[discrete mathematics review series] VI. tree
Ue5 how to convert screen coordinates to world coordinates and World Directions
[special introduction] round table forum -- the integration of AI and audio and video technology
erp odoo 权限管理5年系统设置经验小结 真经验分享
What is CAS and ABA in CAS
For the first time, the Pentagon admitted to funding 46 biological facilities in Ukraine. Russia once revealed that only three were safe
大厂必备的40个方法论
自适应功能简略
Docker部署一个Redis集群
微信小程序 关闭当前页面
【離散數學期複習系列】二、一階邏輯(謂詞邏輯)
[discrete mathematics review series] v. some special charts
Leetcode 2293. Minimax game (yes. One pass)
[Chongqing University] information sharing of preliminary and second examinations (with postgraduate entrance examination group)
Textinputlayout usage details
Adding, deleting, modifying and querying databases with JDBC
2022年危险化学品生产单位安全生产管理人员考试模拟100题及在线模拟考试
Operation of simulated examination platform for theoretical question bank of refrigeration and air conditioning equipment operation in 2022
技术分享| 快对讲,全球对讲