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 gs10484 · Aug 24, 2020 at 03:53 PM · beginnerenemy aiattack

Enemy AI i have this script for a ranged ai unit but i need it to use a melee (bite) attack when the player is close enough.

var runSpeed = 3; var biteRange = 3; var walkSpeed = 1; var rotationSpeed = 3; var attackRange = 3; var chaseRange = 10; var giveUpRange = 20; var dontComeCloserRange = 2; var attackRepeatTime = 0.9; var controller : CharacterController; public var target : Transform; var biteAnim : String; var idleAnim : String; var idleaimAnim : String; var idleaimhighAnim : String; var idleaimlowAnim : String; var runAnim : String; var walkAnim : String; var shootAnim : String; private var initialSpeed = 0; var chasing = false; private var attackTime = 1; var myTransform : Transform; var PauseRunning = false; var Anim : Animation; var lookStrength = 2; var option = false; var option2 = false; var Proj : Transform; var MuzzleFlash : Transform; var BulletSpeed = 20; var BulletPos : Transform; private var idleaimAnimReal; var AudSource : AudioSource; private var currentMuzzle;

function Awake() {

  myTransform = transform;

}

function Start() { Anim.Play(idleAnim);
target = GameObject.FindWithTag("Player").transform;

}

function Update () { Debug.DrawLine( target.position , BulletPos.position , Color.cyan); var distance = (target.position - BulletPos.position).magnitude;

      if ( target.position.y == transform.position.y ) {
      idleaimAnimReal = idleaimAnim;
      }
      if ( target.position.y > transform.position.y ) {
      idleaimAnimReal = idleaimhighAnim;
      }
      if ( target.position.y < transform.position.y ) {
      idleaimAnimReal = idleaimlowAnim;
      }
 
  BulletPos.LookAt(target);
 
 
      if (distance < chaseRange) {
 
          chasing = true;
 
      }
      
      if (chasing) {
      if ( PauseRunning == false ) {
  
      targetRot = new Vector3(target.position.x, this.transform.position.y, target.position.z);
      transform.LookAt(targetRot);
 
      controller.SimpleMove(runSpeed*transform.forward);
 
      Anim.Play(runAnim);
      }
         }
         else 
         { 
      Anim.Play(idleAnim);
 
      }  
 
      if (distance > giveUpRange) {
      
          chasing = false;
 
      }
     
             if (distance < biteRange) {
    
           Anim.Play(biteAnim);
        attackRepeatTime = 0;
  

}

      if (distance < dontComeCloserRange) {

// targetRotation = Quaternion.LookRotation (target.position - transform.position); //
// str = Mathf.Min (lookStrength * Time.deltaTime, 1);

      targetRot = new Vector3(target.position.x, this.transform.position.y, target.position.z);

// transform.rotation.z = Quaternion.Lerp (transform.rotation, targetRotation, str); transform.LookAt(targetRot); PauseRunning = true; if ( option == false ) { InvokeRepeating("Fire", 0, attackRepeatTime); option = true; dontComeCloserRange += 5; option2 = true; } } if (distance > dontComeCloserRange) { PauseRunning = false; CancelInvoke(); option = false; if ( option2 == true ) { dontComeCloserRange -= 5; option2 = false; } }

      if ( PauseRunning == true ) {
      Anim.Play(idleaimAnim);
      
      
      }                            

}

function Fire () {
Anim.Play(shootAnim); attackRepeatTime = 2; Debug.Log("Shoot1"); var Bullet : Transform; var Muzzle : GameObject; AudSource.Play(); Bullet = Instantiate(Proj, BulletPos.position, BulletPos.rotation); Muzzle = Instantiate(MuzzleFlash, BulletPos.position, BulletPos.rotation).gameObject; Bullet.GetComponent.().AddForce(Bullet.transform.forward * BulletSpeed); Debug.Log("Shoot");

  }
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
1
Best Answer

Answer by MerpysoupStudio · Aug 24, 2020 at 08:07 PM

@gs10484 You can put this stuff in the update method, but it is somewhat expensive so if your game needs to be optimized you might want to try something other than vector3.distance.

 distanceFromPlayer = Vector3.Distance(transform.position, target.position);
 
 if(distanceFromPlayer <= minimumDistance)
  {
      Bite();
  }
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

132 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 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 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 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 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 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 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

Enemy attack constantly with a coroutine 1 Answer

Why won't my AI change waypoints for patroling? 1 Answer

Enemy attack and player health! 1 Answer

Implementing a few special attacks for a boss 0 Answers

enemy need to stop and shoot while walking on waypoint 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