Going to make this one simple and too the point.
I have this on enemy that is not set trigger or the moment the enemy is present it destroys everything it comes in contact with.
using UnityEngine;
public class DestroyByContact : MonoBehaviour
{
public GameObject explosion;
public GameObject playerExplosion;
public int scoreValue;
private GameController gameController;
void Start()
{
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent();
}
if (gameController == null)
{
Debug.Log("Cannot find 'GameController' script");
}
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Barrier") || other.CompareTag("Enemy"))
{
return;
}
if (explosion != null)
{
Instantiate(explosion, transform.position, transform.rotation);
}
if (other.tag == "Player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
}
gameController.AddScore(scoreValue);
Destroy(other.gameObject);
Destroy(gameObject);
}
I can shoot and kill the enemy using this shooting code. It works most of the time but still the enemy does not always get destroyed, and it does not matter how fast or slow i make the bullet spawn and the bullet IS set to Trigger. (Note i have it set to shoot with the right mouse click)
using UnityEngine;
public class Shoot : MonoBehaviour
{
public GameObject bullet;
public float delayTime = 1;
private float counter = 0;
void Start()
{
}
void Update()
{
if (Input.GetKey(KeyCode.Mouse1) && counter > delayTime)
{
Instantiate(bullet, transform.position, transform.rotation);
GetComponent().Play();
counter = 0;
}
counter += Time.deltaTime;
}
}
This is what i have on the player for movement and using a Character Controller because i need the player to not pass through other meshes and detect and use things like steps. Note I have it where Jump uses the right control with the Engine;'s input setting.
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
//Variables
public float speed = 3.0F;
public float rotateSpeed = 3.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update()
{
CharacterController controller = GetComponent();
//Feed moveDirection with input.
{
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
Vector3 forward = transform.TransformDirection(Vector3.forward);
float curSpeed = speed * Input.GetAxis("Vertical");
controller.SimpleMove(forward * curSpeed);
//Jumping
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
//Applying gravity to the controller
moveDirection.y -= gravity * Time.deltaTime;
//Making the character move
controller.Move(moveDirection * Time.deltaTime);
}
}
Now i do not have
using System.Collections;
using System;
with the enemy destruction or player movement because it is not necessary in these codes. Its only needed for the shoot script.
So how do i get the enemy to actually destroy the player and get the bullets to actually destroy the enemy with the first detected collision?
↧