Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 IlanaBrenner · Jul 22, 2015 at 04:56 AM · playerenemybulletdelayshoot

Problem with enemy shooting

Hey guys, I can`t make my enemy shoot with delay and to do it when it`s near my player... Nothing works. No errors, just nothing happens... Please help me...

public Rigidbody projectile; public Transform player; public float playerDistance;

 public float speed = 50;
 public float delayTime = 0.5f;
 
 private float counter = 0;
 
 
 
 void Start () {
     
 }

 void Update () {

     playerDistance = Vector3.Distance (player.position, transform.position);
     
     if (playerDistance < 1400f && counter > delayTime)
     {
         counter = 0;
         Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation) as Rigidbody;

         instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
         
     }
 }

}

Comment
Add comment · Show 6
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 NeverHopeless · Jul 22, 2015 at 05:59 AM 0
Share

How do you manipulate counter variable ? Did you log check there is a running case when distance is less than 1400 ?

avatar image IlanaBrenner · Jul 22, 2015 at 11:14 AM 0
Share

Thank you for your response. Still there is a problem with bullet instantiation :/ it`s not moving and it instantiate many bullets in one position .... :(

avatar image iamsidv · Jul 22, 2015 at 11:38 AM 0
Share

You need to change the value of speed variable.

avatar image IlanaBrenner · Jul 22, 2015 at 12:15 PM 0
Share

Thank you iamsidv, but unfortunately it`s not the case . Changed it many times to greater number...and nothing...

avatar image IlanaBrenner · Jul 22, 2015 at 12:40 PM 0
Share

NeverHopeless, I checked the distance, everything is fine. i have another scripts that working with this distance and they are fine =\

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by iamsidv · Jul 22, 2015 at 06:28 AM

Okay, I see where the problem is now and again, I think you should use Time.time for this functionality.

 public float bulletShootInterval = 0.5f;
 private float startTime;

 void Start()
 {
     //Get the current time.
     startTime = Time.time;
 }

 void Update()
 {
     playerDistance = Vector3.Distance (player.position, transform.position);

     if (playerDistance < 1400f && Time.time > startTime)
     {

         //Perform your stuff here.
         Rigidbody instantiatedProjectile =    Instantiate(projectile,transform.position,transform.rotation) as Rigidbody;
  
         instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));

         //Increment the time variable by the interval.
         startTime = Time.time + bulletShootInterval;
     }
 }




Hope this helps !!

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
3

Answer by NeverHopeless · Jul 22, 2015 at 01:48 PM

Perhaps this:

  public float speed = 50;
  public float delayTime = 0.5f;

  void Start () {
      InvokeRepeating("InstantiateProjectileIfNeeded", 0.0f, delayTime);
  }

  void InstantiateProjectileIfNeeded()
  {
      playerDistance = Vector3.Distance (player.position, transform.position);
      if (playerDistance < 1400f)
      {
          counter = 0;
          Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation) as Rigidbody;
  
          instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
          
      }
  }

Since you want to launch a projectile if a distance is less than 1400 but not too frequently there must be a delay of delayTime. The following script repeatedly invoke a function after delayTime and if the distance is less than 1400, your logic should execute and no need of counter variable i guess.

Hope it helps!

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 NeverHopeless · Jul 22, 2015 at 08:36 PM 0
Share

@IlanaBrenner, don't use this method in Update, the delay logic will then be useless. To make it move, first try to move in some other direction than z-axis. e.g., try : instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(speed, 0, 0));

avatar image
2

Answer by Veldars · Jul 22, 2015 at 06:11 AM

I think you just forget to increment your counter...

  void Update () {
  
      playerDistance = Vector3.Distance (player.position, transform.position);
      
      counter += Time.deltaTime;
 
      if (playerDistance < 1400f && counter > delayTime)
      {
          counter = 0;
          Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation) as Rigidbody;
  
          instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
          
      }
  }


I hope this help...

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
1

Answer by IlanaBrenner · Jul 22, 2015 at 03:22 PM

NeverHopeless , it worked after i added:

void Update(){ InstantiateProjectileIfNeeded (); }

but the bullet still has no speed. It`s makin instantiate and it works..but I need to make it move. Thank you for your help i really appreciate this .

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

No collision on trigger and character controller 1 Answer

Shoot Bullet Delay Enemy 0 Answers

C # script Enemy shoots left 2d platform. 2 Answers

make Enemy shoot at player 2 Answers

Shoot directions 2 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