1, Light projection collision: First person sight in preset range (e.g. 3 m) and objects seen collide
① script to detect ray-casting collisions is added on first-person Fpscontroller
#pragma strictprivatevarCurrentdoor:gameobject;varHit:raycasthit;functionUpdate () {varFWD =transform. Transformdirection (Vector3.forward); //true when Ray casts intersect with any collider, otherwise false if(Physics.raycast (transform.position,fwd,hit,18)){ //colliding objects of lightCurrentdoor=Hit.collider.gameObject; Print (Currentdoor); if(currentdoor.tag== "Playerdoor") {print ("Collider"); Currentdoor. SendMessage ("Doorcheck"); } } }
View Code
② Control Door Open script added on Door, first person sight and door collision by SendMessage Trigger collision Object (Door) Open Door method:
#pragma strictvarDoor_open_time:float=3.0;varDoor_open_sound:audioclip;vardoor_shut_sound:audioclip;privatevarDoorisopen:Boolean=false;p rivatevarDoortimer:float=0.0; functionUpdate () {//If the door is open, start timing more than 3 seconds and then eat the animation and reset the opening time to 0 if(doorisopen==true) {Doortimer+=Time.deltatime; Print (Doortimer); if(doortimer>door_open_time) {Doortimer=0; //Shutdoor (Currentdoor);Doorfalse, Door_shut_sound, "Closedoor"); } } }functionDoorcheck () {if(!Doorisopen) {Door (true, Door_open_sound, "Opendoor"); } } //The object that executes, the state of the door, the sound that plays, the object that plays functionDoor (dooropenorclose:Boolean, audio_clip:audioclip,ani_name:string) {Doorisopen=Dooropenorclose; //thisdoor.getcomponent (Audiosource). Playoneshot (audio_clip); This. Transform.parent.GetComponent (Animation). Play ();//play animations distinguish between opening and closing doors}
View Code
Tis: If the ray projection does not collide, you can adjust the height of the Fpscontroller or jump up and try.
2,transform, which represents an object that attaches the current script
3, add the Box Collider component to the door to achieve the forward/backward to the door attachment when the collision triggered, the first person into the door of the Collider within the range can collide.
① Door Box Collider is trigger tick
② setting the range size of the Boxcollider appropriately
③ Real-time collision detection script, added to the door
function Ontriggerenter (col:collider) { if(col.gameobject.tag== "Player") { transform. SendMessage ("Doorcheck");} }
View Code
④ when the first person enters the collision range of the Boxcollider, it can trigger the opening script: Doorcheck
A previous script on the door implements the open door:
#pragma strictvarDoor_open_time:float=3.0;varDoor_open_sound:audioclip;vardoor_shut_sound:audioclip;privatevarDoorisopen:Boolean=false;p rivatevarDoortimer:float=0.0; functionUpdate () {//If the door is open, start the animation again after more than 3 seconds and reset the opening time to 0 if(doorisopen==true) {Doortimer+=Time.deltatime; Print (Doortimer); if(doortimer>door_open_time) {Doortimer=0; //Shutdoor (Currentdoor);Doorfalse, Door_shut_sound, "Closedoor"); } } }functionDoorcheck () {print ("Doorcheck"); if(!Doorisopen) {Door (true, Door_open_sound, "Opendoor"); } } //The object that executes, the state of the door, the sound that plays, the object that plays functionDoor (dooropenorclose:Boolean, audio_clip:audioclip,ani_name:string) {Doorisopen=Dooropenorclose; //thisdoor.getcomponent (Audiosource). Playoneshot (audio_clip); This. Transform.parent.GetComponent (Animation). Play ();//play animations distinguish between opening and closing doors}
View Code
4, three different collision methods for simple applications:
①oncontrollercolliderhit, attached to the first person controller
#pragma import Unityengine.ui; // collision script added in first person, first person and object launch collision when trigger function Oncontrollercolliderhit (hit:controllercolliderhit) { if(hit.gameobject.tag!=" Plane") { print (hit.gameObject.tag); Gameobject.find ("canvas/text"). Getcomponent (text). Text =Hit.gameObject.tag;
View Code
②physics.raycast, a ray-casting collision script is attached to the first-person controller. Trigger when first-person sight is colliding with an object (aiming action in the game):
#pragmaStrictimport Unityengine.ui;Private varObjcollider:gameobject;varhit:raycasthit;function Update () {varFWD =transform. Transformdirection (Vector3.forward); //true when Ray casts intersect with any collider, otherwise false if(Physics.raycast (Transform.position,fwd,hit, the)){ //colliding objects of lightObjcollider=Hit.collider.gameObject; Print (Objcollider); if(objcollider.tag!="Plane") {Gameobject.find ("Canvas/text"). Getcomponent (text). Text =Objcollider.tag; } } }
View Code
③ontriggerenter, increase the size of box Collider tick is Trigger, to add script to objects, to achieve the collision of objects into the scope of the trigger collision script (close to the door in the game, Door effect):
#pragma// script is attached to the object, and the function Ontriggerenter (hit:collider) is triggered within the collision range of the object. Gameobject.find ("canvas/text"). Getcomponent (text). Text =
View Code
Unity3d 5.x interactivity-light projection, collision settings