- Home /
I wrote a bot script and a bullet script but i keep getting an error
I wrote a bot script and a bullet script but i keep getting an error, its really annoying because it says "script:OnCollisionError", but the scripts are fine, also i am trying to make it so that the bullet doesn't leave a bullet hole when it hits the bot but it always does, here are my scripts: BOT Script:
private int health=100;
public GameObject walkOrigin;
private bool RandomMoving=false;
private float angle;
private float destroyTime;
private float rate = 8.0f;
private Material[] materials;
public GameObject muzzleFlashPrefab;
private double nextMuzzleFlashTime;
private GameObject newMuzzleFlash;
public static bool shooting;
public GameObject BulletPrefab;
public GameObject BarrelEnd;
private float ShotTimer = 0.25f;
public GameObject EnemyGun;
private bool Turn;
public GameObject AimFor;
public bool Dying=false;
//This Method is called On start up
void Awake()
{
Moving=false;
Cover = GameObject.FindGameObjectWithTag("Cover");
Soldier = GameObject.FindGameObjectWithTag("soldier");
destroyTime = Time.time + Random.Range(0.02f, 0.01f);
angle = 90 * Mathf.Round(Random.Range(0,3));
}
//Method That Checks for this every frame
void Update()
{
EnemyDistance=Vector3.Distance(Enemy.transform.position,Soldier.transform.position);
CoverDistance=Vector3.Distance(Enemy.transform.position,Cover.transform.position);
if (health>0)
{
if ((CoverDistance<5)&&(EnemyDistance<5)&&(Dying==false))
{
ShootAndCover();
}
if((CoverDistance<5)&&(EnemyDistance>5)&&(Dying==false))
{
WalkAround();
}
else
{
Turn = false;
}
if ((CoverDistance>10)&&(EnemyDistance<15)&&(Dying==false))
{
if ((CoverDistance>5)&&(EnemyDistance<5)&&(Dying==false))
{
StandAndShoot();
}
else
{
MoveAndShoot();
}
}
if ((CoverDistance>5)&&(EnemyDistance>5)&&(Dying==false))
{
RandomMove();
}
}
}
void OnCollisionEnter(Collision other)
{
if (other.collider.tag == "bullet")
{
BulletPrefab.BroadcastMessage("MakingHoleFalse",1.0);
Debug.Log("It Hit");
}
}
void DEAD(float dead)
{
if (dead==1.0)
{
Dying=true;
}
}
void ShootAndCover()
{
StandAndShoot();
}
void StandAndShoot()
{
Enemy.transform.LookAt(AimFor.transform);
ShotTimer = ShotTimer-Time.deltaTime;
if (ShotTimer <= 0)
{
Instantiate(BulletPrefab,BarrelEnd.transform.position,BarrelEnd.transform.rotation);
ShotTimer = 0.25f;
animation.Play("soldierFiring");
}
}
void WalkAround()
{
Enemy.transform.Rotate(0,Random.Range(90,180),0);
Turn = true;
if (Turn)
{
Enemy.transform.position=Vector3.Lerp(Enemy.transform.position,walkOrigin.transform.position,1*Time.deltaTime*3);
}
}
void RandomMove()
{
Enemy.transform.position=Vector3.Lerp(Enemy.transform.position,walkOrigin.transform.position,1*Time.deltaTime*3);
animation.Play("soldierRun");
MoveCount-=Time.deltaTime;
if (RandomMoving==false)
{
MoveCount=Random.Range(2f,5f);
RandomMoving=true;
}
if (MoveCount<=0)
{
Enemy.transform.Rotate(0,Random.Range(45,180),0);
MoveCount=Random.Range(2f,5f);
}
}
void MoveAndShoot()
{
Enemy.transform.LookAt(AimFor.transform);
ShotTimer = ShotTimer-Time.deltaTime;
if (ShotTimer <= 0)
{
Instantiate(BulletPrefab,BarrelEnd.transform.position,BarrelEnd.transform.rotation);
ShotTimer = 0.25f;
}
Enemy.transform.position=Vector3.Lerp(Enemy.transform.position,walkOrigin.transform.position,1*Time.deltaTime*3);
animation.Play("soldierRun");
}
}
Bullet Script:
var life : float = 0.5;
var bulletSpeed : float = 1.0;
var dustPrefab : GameObject;
var bulletHolePrefab : GameObject;
private var destroyTime : float;
private var velocity : Vector3;
private var gravity : float = 9.8;
public var Soldier : GameObject ;
var makeHole : boolean = true;
function Start(){
destroyTime = Time.time + life;
velocity += transform.forward * bulletSpeed;
Soldier=GameObject.FindWithTag("BOT");
}
function MakingHoleFalse(makeAHole : float)
{
if(makeAHole==1.0)
{
Debug.Log(makeAHole);
makeHole=false;
}
}
function Update () {
if(Time.time > destroyTime){
Destroy(gameObject);
}
var ray : Ray;
ray.origin = transform.position;
ray.direction = transform.forward;
var hit : RaycastHit;
if (Physics.Raycast(ray,hit,bulletSpeed*Time.deltaTime)){
triggerChildrenColliderScript = hit.transform.root.GetComponent(triggerChildrenCollider);
var reCheck : boolean; //Re-check if there's a hit for children collider.
var mainColliderHit : Collider = hit.collider; //Parent collider. (must be re enabled)
if(triggerChildrenColliderScript != null){ //Trigger children property. Enable children collider and disable root collider.
hit.collider.enabled = false;
var childrenColliderList : Collider[] = triggerChildrenColliderScript.childrenColliderList;
for (var i = 0; i < childrenColliderList.Length; i++){
childrenColliderList[i].enabled = true;
}
reCheck = Physics.Raycast(ray,hit,bulletSpeed*Time.deltaTime); //Recheck collision for children collider.
}
if(reCheck || triggerChildrenColliderScript == null){ //If children collider or root collider are hit, process the bullet hit.
Destroy(gameObject);
var makeDust : boolean = true;
//var makeHole : boolean = true;
var bulletHolePropertyScript : bulletHoleProperty = hit.transform.root.GetComponent(bulletHoleProperty);
var extraPrefab : GameObject;
var holeSize : float = 1.0;
var differentBulletMaterialArray : Material[];
if (bulletHolePropertyScript != null){ //Bullet hole property.
makeDust = bulletHolePropertyScript.dust;
makeHole = bulletHolePropertyScript.hole;
extraPrefab = bulletHolePropertyScript.extraPrefab;
holeSize = bulletHolePropertyScript.sizeModifier;
bulletMaterials = bulletHolePropertyScript.bulletMaterials;
}
if (extraPrefab != null){ //Extra prefab.
Instantiate(extraPrefab,transform.position,transform.rotation);
}
if (makeDust){ //Dust.
var newdustCloudGenerator : GameObject = Instantiate(dustPrefab,hit.point - transform.forward*0.1, Quaternion.identity);
newdustCloudGenerator.GetComponent(dustCloudGenerator).velocity = hit.normal * (2 + Random.value*10);
}
if (makeHole){ //Bullet hole.
var newBulletHole : GameObject = Instantiate(bulletHolePrefab, hit.point + hit.normal *0.01, Quaternion.identity);
newBulletHole.transform.LookAt(newBulletHole.transform.position + hit.normal);
newBulletHole.transform.parent = hit.collider.transform;
var bulletHoleScript : bulletHole = newBulletHole.GetComponent(bulletHole);
bulletHoleScript.size *= holeSize;
if (bulletHolePropertyScript != null && bulletMaterials.Length > 0){
bulletHoleScript.materials = bulletMaterials;
}
}
if(hit.rigidbody != null){ //Apply force.
hit.rigidbody.AddForceAtPosition(velocity* 1.5, hit.point);
}
var healthScript : health = hit.transform.root.GetComponent(health); //Health property.
if(healthScript != null){
healthScript.health -= 20;
healthScript.SetLastHitTime();
var hitPointNoHeight : Vector3 = hit.point;//hit.point;
hitPointNoHeight.y = hit.transform.position.y;
var hitDirection : Vector3 = hitPointNoHeight - hit.transform.position;
hitDirection.Normalize();
hitDirection = hit.transform.root.InverseTransformDirection(hitDirection);
healthScript.SetHitDirection(hitDirection);
var recoilDirection : Vector3 = hit.transform.position - hitPointNoHeight;
recoilDirection.Normalize();
healthScript.SetrecoilDirecion(recoilDirection);
}
}
if(triggerChildrenColliderScript != null){//Trigger children property. Disable children collider and enable root collider.
mainColliderHit.enabled = true;
for (var n = 0; n < childrenColliderList.Length; n++){
childrenColliderList[n].enabled = false;
}
}
}
velocity.y -= gravity * Time.deltaTime;
transform.position += velocity * Time.deltaTime;
transform.LookAt(transform.position + velocity);
}
please help
Your question is confusing. Runtime error or compile time error? Please edit your question and copy and past the error message from the console into your questions.
ok, forget the error, that seems to have disappeared, my problem now is how to make it so that when i hit the bot, who is tagged BOT, not to leave a bullet hole
Your answer
Follow this Question
Related Questions
Get animation work 1 Answer
Enemy health and bullet problem 2 Answers
Random Raycast inside crosshairs 1 Answer
when the bullet hits the bot it still makes the bullet hole 1 Answer
Troubles with 2D projectiles 1 Answer