碰撞检测 Unity实验代码
生活随笔
收集整理的這篇文章主要介紹了
碰撞检测 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实验代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浙商大会观后感!
- 下一篇: ISO20000度量体系指标