• 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 GesterX · Feb 08, 2011 at 10:24 PM · prefabtimespawnbulletsrate

Why are my bullets shooting at an unpredictable rate?

The desired behavior is for the enemy to spot and chase the player and shoot every 2 seconds. If the player escapes the enemy returns to his starting position.

In action the scripting all works as expected EXCEPT for instead of shooting every 2 seconds, the enemy shoots at a very unpredictable rate - seemingly independant from the timer (although a lot less than once every 2 seconds). Thankfully everything else works fine.

I have done a bit of debugging and the timer that controls when the bullets should be shot works as expected (counting down from 2 to 0 and resetting).

I am going to post my entire code for the enemy control just in case some other function is interfering. If anything needs clarifying please let me know!

var target : Transform; //the target var moveSpeed = 3; //move speed var rotationSpeed = 10; //speed of turning

var myTransform : Transform; //current transform data of this object

private var startPosition : Vector3; private var chasing = false;

private var rotLock = 0;

var chaseThreshold = 2; // distance within which to start chasing var giveUpThreshold = 5; // distance beyond which AI gives up

var distance; //distance from player var distance2; //distance from start position

private var variableScript; var theBullet : Transform;

public var health = 50;

var attackTimer : float; //a timer that controls frequency of attack var coolDown : float; //the cooldown for each attack

var fov = 60.0; private var hit : RaycastHit;

function Awake() { myTransform = transform; //cache transform data for easy access/preformance coolDown = 2; }

function Start() { target = GameObject.FindWithTag("Player").transform; //target the player startPosition = transform.position; //store the start position }

function Update() { // check distance to target every frame: distance = (target.position - myTransform.position).magnitude; distance2 = (startPosition - myTransform.position).magnitude; ChaseCheck(); CheckDeath(); FixRotation(); }

function ChaseCheck() { if (chasing) { ChasePlayer(); AttackTimer(); }

 else if (LineOfSight(target))
     {
         chasing = true;
     }

 else if(distance > giveUpThreshold && distance2 > 1)
     {
         ReturnToPosition();
     }

}

function ChasePlayer() {

  //rotate to look at the player
     myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
     Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

     //move towards the player
     myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

     // give up, if too far away from target:
     if (distance > giveUpThreshold) {
         chasing = false;
 }

}

function ReturnToPosition() { //rotate to look at the start position myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(startPosition - myTransform.position), rotationSpeed*Time.deltaTime);

         if(myTransform.position != startPosition)
         {
         //move back to start position
             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
         }

}

function AdjustHealth(adj) { health -= adj; }

function CheckDeath() { if (health <= 0) { Destroy(gameObject); } }

function AttackTimer() { //if the attack timer is above 0 if (attackTimer > 0) { attackTimer -= Time.deltaTime; //countdown to 0 } else if (attackTimer < 0) { attackTimer = 0; //error avoidance } //attack when the timer is at 0 else if (attackTimer == 0) { var script : TurretShoot; script = GetComponentInChildren(TurretShoot); script.AutoShoot (); attackTimer = coolDown; //reset timer to cooldown value } }

function LineOfSight (targt : Transform) : boolean { if (Vector3.Angle(targt.position - transform.position, transform.forward) <= fov && Physics.Linecast(transform.position, targt.position, hit) && hit.collider.transform == targt && (distance < chaseThreshold))

         {
             return true;
         }
 return false;

}

function FixRotation() { transform.rotation = Quaternion.Euler(rotLock, transform.rotation.eulerAngles.y, rotLock); }

And the TurretShoot.js

var myTransform : Transform; //current transform data of this object var theBullet : Transform;

function Awake() { myTransform = transform; //cache transform data for easy access/preformance

}

function AutoShoot() { var createdBullet : Transform; // create a bullet, and rotate it based on the vector createdBullet = Instantiate(theBullet, myTransform.position, myTransform.rotation); }

If anybody could help me spot why the bullets are not shot as expected I'd be very grateful.

Edit: The Update function in a script attached to the bullet to make the bullet move:

function Update ()
{
    existanceTime += Time.deltaTime;
    // if we have been traveling for more than one second remove the bullet
    if (existanceTime > 1) 
    {
        removeMe();
    }
    // move the bullet
    transform.Translate(0, 0, moveSpeed * Time.deltaTime);
    transform.position = new Vector3(transform.position.x, 0.5, transform.position.z);
}
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 T. · Feb 08, 2011 at 10:30 PM 0
Share

where are you performing the check & firing the bullets? i cant seem to find it.

avatar image GesterX · Feb 08, 2011 at 10:45 PM 0
Share

In the AttackTimer function I check to see the state of the attackTimer var. If it isabove zero then I count down. If it equals 0 then I call the AutoShoot $$anonymous$$ethod from the TurretShoot.js script.

3 Replies

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

Answer by GesterX · Feb 09, 2011 at 12:47 PM

It appears that this was an issue with my colliders. The bullets were sometimes colliding with the object shooting them and destroying themselves a split second after being shot!

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

Answer by tertle · Feb 08, 2011 at 10:34 PM

attackTimer is always 0 from what I can tell. Is this intentional?

attackTimer = coolDown;

Do you set coolDown in inspector or something? I can't see it set anywhere.

Comment
Add comment · Show 1 · 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 GesterX · Feb 08, 2011 at 10:43 PM 0
Share

$$anonymous$$y mistake - I have edited the awake function to set coolDown to 2 and the problem persists. I was previously setting coolDown to 2 in the inspector. The purpose of the line you highlighted is to set the attackTimer back to 2 and countdown to 0 again (when the attackTimer hut's zero the AutoShoot() function is called)

avatar image
0

Answer by T. · Feb 08, 2011 at 11:04 PM

Make attackTimer a float, and change this

    else if (attackTimer == 0)

to

else if (attackTime <= 0.0)

Comment
Add comment · Show 3 · 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 GesterX · Feb 08, 2011 at 11:13 PM 0
Share

Changed it and also changed the coolDown to 2.0f. The problem still occurs unfortunately.

avatar image T. · Feb 08, 2011 at 11:46 PM 1
Share

try chucking some print outs in and see what that does

avatar image GesterX · Feb 09, 2011 at 11:14 AM 0
Share

I put a print at the end of the AutoShoot() function after the instantiation and it successfully prints every two seconds.

I should also note that the movement of the bullet is controlled in a script attached to the bullet itself.

I have added this to the original question

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Spawn a prefab at different objects 2 Answers

Destroying Projectile Prefabs after Time 3 Answers

i need Spawn Object loop if find object tag ("Clone") in list ,when i delete find object tag ,cript is good but no find object, please help me 1 Answer

Game not working when I save, exit out of unity, then come back in. 1 Answer

choose 1 of multiple objects to spawn, respawn once its dead. 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges