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 MachCUBED · Dec 03, 2012 at 07:22 PM · javascriptmovementcolliders

Bullet fired at enemy only applying damage when the enemy moves

Hi guys,

I have the following script on some bullets that automatically home in on the nearest enemy. Each bullet has a sphere trigger

Bullet.js:

 #pragma strict
 
 var damageValue : int = 2;
 
 var lifetime : float = 15.0;
 
 var speed : float = 10;
 
 private var enemies : GameObject[];
 
 private var target : GameObject;
 
 private var elapsedTime : float = 0;
 
 ...
 
 function FixedUpdate () {
 
     if (target)
     {
         transform.LookAt(target.transform);
         Debug.Log("Targeting an enemy");
     }
     else
     {
         Debug.Log("Target not found, proceeding straight from player");
     }
     
     transform.Translate(Vector3.forward * speed * Time.deltaTime);
     
     elapsedTime += Time.deltaTime;
     if (elapsedTime >= lifetime)
          Destroy(gameObject);
 }
 
 function OnTriggerEnter(other : Collider)
 {
     if (other.tag == "Enemy")
         other.SendMessage("ApplyDamage", damageValue);
     
     if (other.tag != "Player")
         Destroy(gameObject);
 }

The bullets home in on enemies as predicted. Each enemy has the following code.

EnemyDamage.js:

 var hitPoints = 3;
 
 var explosionPrefab : Transform;
 var deadModelPrefab : Transform;
 var healthPrefab : DroppableMover;
 var dropMin = 0;
 var dropMax = 0;
 
 var scoreValue : int = 100;
 
 ...
 
 var isBoss : boolean = false;
 
 private var dead = false;
 
 function ApplyDamage (damage : int)
 {
     Debug.Log("Enemy Taking Damage");
     // we've been hit, so play the 'struck' sound. This should be a metallic 'clang'.
     if (audio && struckSound)
         audio.PlayOneShot(struckSound);
 
     if (hitPoints <= 0)
         return;
 
     hitPoints -= damage;
     if (!dead && hitPoints <= 0)
     {
         Die();
         dead = true;
     }
 }
 
 function Die ()
 {    
     ...
 }
 
 ...

If an enemy is not moving (e.g. the enemy is just standing there (e.g. in the idle state defined in the Lerpz 3rd Person Tutorial's EnemyDamage.js script), the bullet just goes to the enemy's position and doesn't apply any damage until the enemy starts moving. I want the bullet to apply damage regardless of state.

Also, there is a boss with a custom flying script. That boss does not take any damage upon being shot. Here is the boss's script in its entirety, showing that the boss is in constant motion.

FlyingBoss.js:

 #pragma strict
 
 var speed : float = 1.0;
 
 var attackIntervalMin : float = 1.0;
 
 var attackIntervalMax : float = 5.0;
 
 var bullet : GameObject;
 
 private var waypoints : GameObject[];
 
 private var target : Transform;
 
 private var elapsedTime : float = 0;
 private var attackInterval : float = 0;
 
 function SelectWaypoint()
 {
 
     var numValues = waypoints.Length;
     
     var wayPointIndex = Random.Range(0, numValues - 1);
     
     if (waypoints[wayPointIndex].transform != transform)
         target = waypoints[wayPointIndex].transform;
     else
     {
         if (waypoints[wayPointIndex].transform == waypoints[numValues-1].transform)
             target = waypoints[wayPointIndex -1].transform;
         else
             target = waypoints[wayPointIndex + 1].transform;
     }
     
 }
 
 function Start () 
 {
 
     waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
     
     SelectWaypoint();
     
     animation["fly"].wrapMode = WrapMode.Loop;
     animation.Play("fly");
     
     GetRandomInterval();
 
 }
 
 function GetRandomInterval()
 {
     attackInterval = Random.Range(attackIntervalMin, attackIntervalMax);
 }
 
 function LaunchFireball()
 {
     // Instantiate the projectile at the position and rotation of this transform
     var clone : Transform;
     clone = Instantiate(bullet.transform, transform.position, transform.rotation);
 }
 
 function FixedUpdate () 
 {
     // Timer code for attacks
     
     elapsedTime += Time.deltaTime;
     
     if (elapsedTime >= attackInterval)
     {
         LaunchFireball();
         
         elapsedTime = 0;
         
         GetRandomInterval();
     }
     
     // Movement code
     
     if (target)
     {
         transform.LookAt(target); // TODO: replace this with smoother rotation code
         
         transform.Translate(Time.deltaTime * speed * Vector3.forward, Space.Self);
     }
     
     Debug.Log("Boss position x:" + transform.position.x + "y:" + transform.position.y + "z" + transform.position.z );
     
     // Once at target location, find a new one
     
     var targetDistance = Vector3.Distance(transform.position, target.position);
     
     Debug.Log("Distance to target:" + targetDistance);
     if (targetDistance < 0.25)
         SelectWaypoint();
 }

All help will be appreciated.

MachCUBED

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by sirival · Dec 03, 2012 at 10:34 PM

Try adding a kinematic rigid body to your enemy collider and see if that helps. Perhaps the collision message is not triggered when the enemy remains still.

Comment
Add comment · Show 4 · Share
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 MachCUBED · Dec 04, 2012 at 02:01 AM 0
Share

The normal enemies have kinematic rigidbodies already, so adding one to the enemies won't solve their problem. Adding a kinematic rigidbody to the flying boss didn't fix the problem either, even though the flying boss is always in motion. Adding a kinematic rigidbody to my player's bullets merely makes them disappear after being fired, so I removed them from the bullets.

avatar image sirival · Dec 04, 2012 at 07:12 AM 0
Share

Take a look at the table on the bottom of this link. $$anonymous$$aybe it will help you solve your problem and it is also the motivation of my response. http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html

avatar image MachCUBED · Dec 04, 2012 at 11:15 PM 0
Share

I added the player's bullets to their own layer, added a rigidbody component to the bullets, and adjusted the collision matrix so that the player's bullets only collide with the enemies layer (e.g. the bullets layer only collides with the enemies layer). This fixed the problem for my normal enemies, who can be shot and killed at any time. However, the flying boss was missing its collider component, so adding a capsule collider solved the problem.

avatar image sirival · Dec 04, 2012 at 11:39 PM 0
Share

Nice one :)

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

I have a movement script, but how can i make it so i can move half the speed while in the air. 0 Answers

how to interpolate between 3 position in a infinite runner game (scripts) 0 Answers


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