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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by erda123 · Jul 08, 2013 at 10:06 AM · enemyfollowshoot

How To Make Enemy Shoot At You?

Hello, i want to make enemy shoot at me when it starts to follow me, im not sure but i think i should put the enemy shooting script in enemy AI script. What i am looking for is enemy shooting script, and where do i put it in enemy AI script. Enemy AI:

 var distance;
 
     var target : Transform;    
 
     var lookAtDistance = 15.0;
 
     var attackRange = 10.0;
 
     var moveSpeed = 5.0;
 
     var damping = 6.0;
 
     private var isItAttacking = false;
 
     function Update () 
     {
     distance = Vector3.Distance(target.position, transform.position);
     if(distance < lookAtDistance)
     {
     isItAttacking = false;
     renderer.material.color = Color.yellow;
     lookAt ();
     }   
     if(distance > lookAtDistance)
     {
     renderer.material.color = Color.green; 
     }
     if(distance < attackRange)
     {
     attack ();
     }
     if(isItAttacking)
     {
     renderer.material.color = Color.red;
     }
 }
 function lookAt ()
 {
 var rotation = Quaternion.LookRotation(target.position - transform.position);
 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
 }
 function attack ()
 {
     isItAttacking = true;
     renderer.material.color = Color.red;
     transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
 }
 
Comment
Add comment · Show 5
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 DavidDebnar · Jul 08, 2013 at 10:11 AM 1
Share

I don't want an enemy shooting at me! First format your code better and maybe then someone will help you.

avatar image Benproductions1 · Jul 08, 2013 at 11:02 AM 1
Share

Please format your code

avatar image erda123 · Jul 08, 2013 at 11:38 AM 0
Share

is it ok now?

avatar image DavidDebnar · Jul 08, 2013 at 11:45 AM 0
Share

It's better. You should also remove the unnecessary white-spaces and use proper indentation. Doing this will make us happy!

avatar image Benproductions1 · Jul 08, 2013 at 11:46 AM 0
Share

I was about to say, it's now formatted, but still unreadable XD

2 Replies

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

Answer by DavidDebnar · Jul 08, 2013 at 02:05 PM

//Enemy.js

 public var Target : Transform;    
 public var Projectile : Transform;
 
 public var MaximumLookDistance : float = 15;
 public var MaximumAttackDistance : float = 10;
 public var FollowSpeed : float = 5;
 public var MinimumDistanceFromPlayer : float = 2;
 
 public var RotationDamping : float = 6;
 
 function Update ()  {
 
     var distance = Vector3.Distance(Target.position, transform.position);
 
     if(distance <= MaximumLookDistance) {
         LookAtTarget ();
 
         if(distance <= MaximumAttackDistance)
             AttackAndFollowTarget (distance);
     }   
     else
         renderer.material.color = Color.green; 
 }
 
 
 function LookAtTarget () {
     renderer.material.color = Color.yellow;
     var dir = Target.position - transform.position;
     dir.y = 0;
     var rotation = Quaternion.LookRotation(dir);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
 }
 
 
 function AttackAndFollowTarget (distance : float) {
     renderer.material.color = Color.red;
     if(distance > MinimumDistanceFromPlayer)
         transform.Translate((Target.position - transform.position).normalized * MoveSpeed * Time.deltaTime);
 
     Instantiate(Projectile, transform.position + (Target.position - transform.position).normalized, Quaternion.LookRotation(Target.position - transform.position));
 }


//Projectile.js

 function Update () {
     transform.Translate(transform.forward);
 }
 
 function OnCollisionEnter (c : Collision) {
     //if(c.collider.CompareTag("Player"))
         //c.collider.gameObject.GetComponent(PlayerScript).Damage();
 
     Destroy(gameObject);
 }

--David--

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 Nk.Andrei · Jul 08, 2013 at 01:23 PM

In function attack() if isItAttacking is true then instantiate a prefab (ex. a sphere) from the enemy position and add forward speed to your sphere or use a raycast...if the ray intersects your player then use instantiate again.

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 erda123 · Jul 08, 2013 at 01:49 PM 0
Share

THAN$$anonymous$$S!!! IT WOR$$anonymous$$ED :D

avatar image erda123 · Jul 08, 2013 at 02:01 PM 0
Share

hey, i accidentaly messed something up, it still shoots but how do i make it shoot directly at me? this is the script now:

isItAttacking = true; clone = Instantiate(projectile, transform.position, transform.rotation);

avatar image boss.corp · Jul 09, 2013 at 07:43 AM 0
Share

i highly recommend you watch the brackys video on enemy AI.

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

19 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

Related Questions

Enemys following and shooting 1 Answer

Can someone Tell me how I can make the enemy shoot the player when the see them in 2D? 1 Answer

Enemy Pirate Ship AI 1 Answer

Problem with enemy shooting 4 Answers

Why is my enemy not doing his run animation? 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