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 Albano.conte · May 27, 2012 at 09:55 PM · enemyfollowing

MainCharacter Enemy Following Script Problem

So, I'm making a FPS, in this game it's supposed that my enemies should look, follow and shoot me, however, none of this happens, here's my Enemy's Script's Update:

 void Update () 
 {
     TimingAtacar++;
     if (Vector3.Distance(this.transform.position, Jugador.transform.position) < DistanciaParaAtacar)
     {    
         transform.LookAt(Camera.main.transform.position);
         transform.position += transform.forward * Time.deltaTime * Velocidad;
         if(TimingAtacar<=120)
         {
             Instantiate (BalaEnemigo, new Vector3 ( Enemigo.transform.position.x,Enemigo.transform.position.y,Enemigo.transform.position.z), Quaternion.identity);
             TimingAtacar=0;
         }
     }
 }
 public float Vida = 200;
 public void TakeDamage(float damage)
 {
     Vida -= damage;
     if (Vida <= 0)
     {
         GameObject.Destroy(Enemigo.gameObject);
     }
 }
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 whydoidoit · May 27, 2012 at 10:18 PM 0
Share

So some of that looks just fine, I'll make a couple of comments and see where we get to.

Velocidad is not 0 right? $$anonymous$$ake sure in the inspector.

DistanciaParaAtacar is a reasonable size, as in they should start moving at some reasonable distance (not 0 again of course)?

I'm guessing BalaEnemigo is the enemies bullet - you could just instantiate that without making the new Vector3 and use Enemigo.transform.position ins$$anonymous$$d by the way.

I'm presu$$anonymous$$g bullets know how to move? That they can't collide with the enemy as they start right on top of him...

One note - you should really make Ti$$anonymous$$gAtacar a float and do

 Ti$$anonymous$$gAtacar += Time.deltaTime;

Then treat it as a value in seconds - that way your game plays the same logic irrespective of frame rate.

avatar image Albano.conte · May 27, 2012 at 10:43 PM 0
Share

Thanks for the tip of deltaTime, Velocidad is not 0, DistanciaParaAtacar is 20, the bullets and the enemies are in different layers, and they should move, I have another question open about the fact that they dont.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Albano.conte · May 27, 2012 at 11:49 PM

Okay, I solved it,

transform.LookAt(Camera.main.transform.position); transform.position += transform.forward * Time.deltaTime * Velocidad;

This should go Outside of the if that deals with the distance.

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 whydoidoit · May 27, 2012 at 11:50 PM 0
Share

Well there you go! Best of luck with it :)

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

Bug with my enemyAI 2 Answers

MonoDevelop doesn't open script and keeps restarting? 0 Answers

Enemy following Player on uneven surface 1 Answer

Elevator script problem (Java) 1 Answer

Enemy is not stopping following the player 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