Saludos amigos, en esta ocasión os traigo un ejemplo de como crear unos prismáticos o mira telescópica para un arma.
Parte 1
Parte 2
#pragma strict var camaraPrincipal : Camera; var player : GameObject; var marco : GameObject; var activaPrismaticos : boolean = false; var valorZoomInicial : int; var valorZoom : int = 12; var valorZoomMax : int = 15; var valorZoomMin : int = 2; var mensage : String = ""; var mensage2 : String = ""; function Start () { camaraPrincipal = Camera.main; valorZoomInicial = camaraPrincipal.camera.fieldOfView; DesactivaMarco (); rayoOn = false; } function Update () { if(Input.GetKeyDown (KeyCode.P)){ SwitchPrismaticos (); } if(rayoOn){ Rayo (); camaraPrincipal.camera.fieldOfView = valorZoom; }else{ camaraPrincipal.camera.fieldOfView = valorZoomInicial; } LimitesZoom (); if(Input.GetButton ("Fire1") && rayoOn){ MasZoom (); } if(Input.GetButton ("Fire2") && rayoOn){ MenosZoom (); } BuscaTipoEnemigo (); BuscaTipoArma (); CreaArmaGUI (); } function OnGUI () { if(rayoOn){ GUI.color = Color.green; GUI.Label (Rect (20, Screen.height - 40, 600, 20),"DISTANCIA: " + distancia + " m"); } if(armaGUI != null){ GUI.color = Color.green; GUI.Label (Rect (Screen.width - 60, Screen.height - 100, 100, 20),"" + mensage); GUI.Label (Rect (Screen.width - 60, Screen.height - 80, 100, 20),"" + mensage2); } } var rayoOn : boolean = false; var longitudRayo : int = 100; var col : Collider; var tagObj : String; var objeto : Transform; var distancia : float; function Rayo () { var ray = new Ray (camaraPrincipal.transform.position, camaraPrincipal.transform.forward); var hit : RaycastHit = new RaycastHit(); if (Physics.Raycast (ray.origin, ray.direction, hit, longitudRayo)){ Debug.DrawRay (camaraPrincipal.transform.position, camaraPrincipal.transform.forward * longitudRayo, Color.red); col = hit.collider; tagObj = col.transform.root.tag; objeto = col.transform.root; distancia = Mathf.RoundToInt(Vector3.Distance(player.transform.position, hit.point)); }else{ col = null; tagObj = null; objeto = null; distancia = 0; } } function SwitchPrismaticos () { if(activaPrismaticos){ activaPrismaticos = false; MotorOn (); DesactivaMarco (); rayoOn = false; }else{ activaPrismaticos = true; MotorOff (); ActivaMarco (); rayoOn = true; } } function MotorOn () { if(player != null){ var script : CharacterMotor = player.GetComponent("CharacterMotor"); script.enabled = true; } } function MotorOff () { if(player != null){ var script : CharacterMotor = player.GetComponent("CharacterMotor"); script.enabled = false; } } function ActivaMarco () { if(marco != null){ marco.renderer.enabled = true; } } function DesactivaMarco () { if(marco != null){ marco.renderer.enabled = false; } } function MasZoom () { valorZoom += 1; } function MenosZoom () { valorZoom -= 1; } function LimitesZoom () { if(valorZoom < valorZoomMin){ valorZoom = valorZoomMin; } if(valorZoom > valorZoomMax){ valorZoom = valorZoomMax; } } enum TIPOENEMIGO { SinEnemigo = 0, Infanteria = 1, Artilleria = 2, Vehiculo = 3, } var _tipoEnemigo : TIPOENEMIGO; var esEnemigo : boolean = false; function BuscaTipoEnemigo () { if(objeto != null && objeto.GetComponent(Enemigo)){ _tipoEnemigo = objeto.GetComponent(Enemigo)._tipoEnemigo; esEnemigo = true; }else if(objeto != null && !objeto.GetComponent(Enemigo)){ _tipoEnemigo = TIPOENEMIGO.SinEnemigo; esEnemigo = false; }else if(objeto == null){ _tipoEnemigo = TIPOENEMIGO.SinEnemigo; esEnemigo = false; } } var tieneArma : boolean = false; var arma : Transform; function BuscaTipoArma () { if(esEnemigo){ if(_tipoEnemigo == TIPOENEMIGO.Infanteria){ for (var child : Transform in objeto.transform) { if (child.CompareTag ("Arma")) { tieneArma = true; arma = child; break; }else{ tieneArma = false; arma = null; } } escala = 0.006; mensage = "Arma: "; if(arma != null){ mensage2 = "" + arma.gameObject.name; } } if(_tipoEnemigo == TIPOENEMIGO.Artilleria){ arma = objeto.transform; tieneArma = true; escala = 0.002; mensage = "Artilleria: "; mensage2 = "" + arma.gameObject.name; } if(_tipoEnemigo == TIPOENEMIGO.Vehiculo){ arma = objeto.transform; tieneArma = true; escala = 0.002; mensage = "Vehiculo: "; mensage2 = "" + arma.gameObject.name; } }else{ tieneArma = false; arma = null; } } var armaGUI : Transform; var posArmaGUI : Vector3; // X = 0.04; Y = -0.025; Z = -0.01 var creadaArmaGUI : boolean = false; var capaGUI : int; var gradosRotacion : int = 45; var escala : float = 0.006; function CreaArmaGUI () { if(arma != null && !creadaArmaGUI){ armaGUI = Instantiate (arma,marco.transform.position + posArmaGUI, Quaternion.identity); armaGUI.gameObject.layer = capaGUI; if(armaGUI.gameObject.collider){ armaGUI.gameObject.collider.enabled = false; } for (var child : Transform in armaGUI) { child.gameObject.layer = capaGUI; if(child.gameObject.collider){ child.gameObject.collider.enabled = false; } } armaGUI.parent = transform; armaGUI.localScale = Vector3(escala,escala,escala); creadaArmaGUI = true; } if(arma == null && armaGUI != null){ Destroy (armaGUI.gameObject); armaGUI = null; creadaArmaGUI = false; } if(armaGUI != null){ armaGUI.Rotate(Vector3.up * Time.deltaTime * gradosRotacion, Space.World); } }
No hay comentarios:
Publicar un comentario