日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

碰撞检测 Unity实验代码

發布時間:2023/12/29 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 碰撞检测 Unity实验代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

碰撞檢測 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实验代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。