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
1
Question by deadmaggot1 · Apr 02, 2015 at 06:08 PM · scripting beginner

Melee & range 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. i havent been able to fine anything online to help me with this. I'm still quite new to this so any help would be amazing.

the script is:

 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.<Rigidbody>().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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Adding audio to gun shot 2 Answers

How do I make a coroutine from another script stop? 3 Answers

Change Resolution Scaling Mode by script (Android) (SOLVED FOR ME) 2 Answers

Endless runner spawning infinite tiles 0 Answers

Creating one class or using polymorphism? 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