Unity Disparo desde Camara

Lo que vamos hacer es  crear un crosshairs, un punto de mira y disparar, el punto de mira, ocupara el lugar del mouse durante el juego. así que vamos hacer lo siguiente

Vamos a  incorporar a la escena estos dos objetos, que serán nuestros enemigos

Aquí tengo al objeto de la derecha seleccionado, ves que tiene un script que pondré mas adelante, un Box Collider 2D, marcado con la Is Trigger, y un Rigibody2D con la gravedad 0. Ahora lo que vamos hacer es ir a nuestra cámara y le creamos un Script, en la Cámara del juego

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class camarabullet : MonoBehaviour
{
public GameObject crosshairs;

private Vector3 target;

private Vector3 mousePos;
private Vector3 objectPos;
public GameObject disparo;

// Use this for initialization
void Start()
{
Cursor.visible = false;
}

// Update is called once per frame
void Update()
{
target = transform.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
crosshairs.transform.position = new Vector2(target.x, target.y);

if (Input.GetButtonDown(«Fire1»))
{
mousePos = Input.mousePosition;
mousePos.z = 2.0f;
objectPos = Camera.main.ScreenToWorldPoint(mousePos);
Instantiate(disparo, objectPos, Quaternion.identity);
}
}
}

Este seria nuestro Script de la camara 

Un comentario

  1. For most recent news you have to visit world wide web and on internet
    I found this web site as a finest web page for most up-to-date updates.

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

En donde he puesto el Punto y la explosión seria el disparo, ahora mismo dispararíamos, pero no pasaría nada lo siguiente , cogeríamos nuestro disparo, y una vez creado lo incorporaríamos a la carpeta de prefab, con un Box Collider 2D el is Trigger activado, y con un Tag al que llamare dos, y con el script que dejare aquí abajo

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Disparo : MonoBehaviour
{
void Start()
{

}

// Update is called once per frame
void Update()
{
Destroy(gameObject, 1f);

}
}

 

 

Con este Script el disparo, se eliminaría en un segundo, ahora vamos a nuestros enemigos y pondríamos el siguiente script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemigo : MonoBehaviour
{
// Start is called before the first frame update
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.tag == «dos»)
{
Destroy(gameObject);
//or gameObject.SetActive(false);
}
}
}

Y ya podríamos probarlo