I currently have a script attached to a prefab for a bullet. The script work by looking at the collision with the target which is “Player 2”. However the bullet is a prefab and my Game Object is in the scene. I can not drag and drop the GameObject onto the prefab so i am unable to complete the script.
Anyone can you help?
Bellow is my script:
using UnityEngine;
using System.Collections;
public class BulletDestroyer : MonoBehaviour {
public int damage_amount;
public GameObject Player2;
// Update is called once per frame
void Update ()
{
Attack();
//if the AutoDestruct() method isn't already being invoked
if(!IsInvoking("AutoDestruct"))
{
//schedule the execution of the AutoDestruct() method to happen in the next 3 seconds
Invoke("AutoDestruct",3);
}
}
//destroys the game object
void AutoDestruct()
{
Destroy(gameObject);
}
private void Attack() {
float distance = Vector3.Distance(Player2.transform.position, transform.position);
//To fix .. looking if the charecter is infront or binde of you.
Vector3 dir = (Player2.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
//debug for above ...
Debug.Log(direction);
if(distance < 1.2f){
Player2Health eh = (Player2Health)Player2.GetComponent("Player2Health");
eh.addjustcurrenthealth(-damage_amount);
}
}
}