Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tayyab43 · Apr 01, 2014 at 06:24 PM · bulletbots

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Apr 01, 2014 at 07:23 PM 1
Share

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.

avatar image tayyab43 · Apr 06, 2014 at 04:36 PM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges