Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 micha-max · Jun 07, 2011 at 02:45 PM · attackzombie

Script Attack zombie

hello, I am sorry for my poor English.
I would like to create a IA which attacks my player.

I have to use script provided in a demonstration. (platform). my IA moves and attack the player, but all are just in “idle” (the animation of walk and attack does not engage) ... the script are after. can you help me please? thank you for your answer.

 /*
     animations played are:
     idle, threaten, turnjump, attackrun
 */
 
 var attackTurnTime = 0.7;
 var rotateSpeed = 120.0;
 var attackDistance = 17.0;
 var extraRunTime = 2.0;
 var damage = 1;
 
 var attackSpeed = 5.0;
 var attackRotateSpeed = 20.0;
 
 var idleTime = 1.6;
 
 var punchPosition = new Vector3 (0.4, 0, 0.7);
 var punchRadius = 1.1;
 
 // sounds
 var idleSound : AudioClip;    // played during "idle" state.
 var attackSound : AudioClip;    // played during the seek and attack modes.
 
 private var attackAngle = 10.0;
 private var isAttacking = false;
 private var lastPunchTime = 0.0;
 
 var target : Transform;
 
 // Cache a reference to the controller
 private var characterController : CharacterController;
 characterController = GetComponent(CharacterController);
 
 // Cache a link to LevelStatus state machine script:
 var levelStateMachine : LevelStatus;
 
 function Start ()
 {
     levelStateMachine = GameObject.Find("/Level").GetComponent(LevelStatus);
 
     if (!levelStateMachine)
     {
         Debug.Log("EnemyPoliceGuy: ERROR! NO LEVEL STATUS SCRIPT FOUND.");
     }
 
     if (!target)
         target = GameObject.FindWithTag("Player").transform;
     
     animation.wrapMode = WrapMode.Loop;
 
     // Setup animations
 animation.Play("idle");
     animation["threaten"].layer = 0;
     animation["turnjump"].layer = 1;
     animation["gothit"].layer = 1;
     animation["gothit"].layer = 1;
     
     // initialize audio clip. Make sure it's set to the "idle" sound.
     audio.clip = idleSound;
     
     yield WaitForSeconds(Random.value);
     
     // Just attack for now
     while (true)    
     {
         // Don't do anything when idle. And wait for player to be in range!
         // This is the perfect time for the player to attack us
         yield Idle();
 
         // Prepare, turn to player and attack him
         yield Attack();
     }
 }
 
 
 function Idle ()
 {
     // if idling sound isn't already set up, set it and start it playing.
     if (idleSound)
     {
         if (audio.clip != idleSound)
         {
             audio.Stop();
             audio.clip = idleSound;
             audio.loop = true;
             audio.Play();    // play the idle sound.
         }
     }
     
     // Don't do anything when idle
     // The perfect time for the player to attack us
     yield WaitForSeconds(idleTime);
 
     // And if the player is really far away.
     // We just idle around until he comes back
     // unless we're dying, in which case we just keep idling.
     while (true)
     {
         characterController.SimpleMove(Vector3.zero);
         yield WaitForSeconds(0.2);
         
         var offset = transform.position - target.position;
         
         // if player is in range again, stop lazyness
         // Good Hunting!        
         if (offset.magnitude < attackDistance)
             return;
     }
 } 
 
 function RotateTowardsPosition (targetPos : Vector3, rotateSpeed : float) : float
 {
     // Compute relative point and get the angle towards it
     var relative = transform.InverseTransformPoint(targetPos);
     var angle = Mathf.Atan2 (relative.x, relative.z) * Mathf.Rad2Deg;
     // Clamp it with the max rotation speed
     var maxRotation = rotateSpeed * Time.deltaTime;
     var clampedAngle = Mathf.Clamp(angle, -maxRotation, maxRotation);
     // Rotate
     transform.Rotate(0, clampedAngle, 0);
     // Return the current angle
     return angle;
 }
 
 function Attack ()
 {
     isAttacking = true;
     
     if (attackSound)
     {
         if (audio.clip != attackSound)
         {
             audio.Stop();    // stop the idling audio so we can switch out the audio clip.
             audio.clip = attackSound;
             audio.loop = true;    // change the clip, then play
             audio.Play();
         }
     }
     
     // Already queue up the attack run animation but set it's blend wieght to 0
     // it gets blended in later
     // it is looping so it will keep playing until we stop it.
     animation.Play("attackrun");
     
     // First we wait for a bit so the player can prepare while we turn around
     // As we near an angle of 0, we will begin to move
     var angle : float;
     angle = 180.0;
     var time : float;
     time = 0.0;
     var direction : Vector3;
     while (angle > 5 || time < attackTurnTime)
     {
         time += Time.deltaTime;
         angle = Mathf.Abs(RotateTowardsPosition(target.position, rotateSpeed));
         move = Mathf.Clamp01((90 - angle) / 90);
         
         // depending on the angle, start moving
         animation["attackrun"].weight = animation["attackrun"].speed = move;
         direction = transform.TransformDirection(Vector3.forward * attackSpeed * move);
         characterController.SimpleMove(direction);
         
         yield;
     }
     
     // Run towards player
     var timer = 0.0;
     var lostSight = false;
     while (timer < extraRunTime)
     {
         angle = RotateTowardsPosition(target.position, attackRotateSpeed);
             
         // The angle of our forward direction and the player position is larger than 50 degrees
         // That means he is out of sight
         if (Mathf.Abs(angle) > 40)
             lostSight = true;
             
         // If we lost sight then we keep running for some more time (extraRunTime). 
         // then stop attacking 
         if (lostSight)
             timer += Time.deltaTime;    
         
         // Just move forward at constant speed
         direction = transform.TransformDirection(Vector3.forward * attackSpeed);
         characterController.SimpleMove(direction);
 
         // Keep looking if we are hitting our target
         // If we are, knock them out of the way dealing damage
         var pos = transform.TransformPoint(punchPosition);
         if(Time.time > lastPunchTime + 0.3 && (pos - target.position).magnitude < punchRadius)
         {
             // deal damage
             target.SendMessage("ApplyDamage", damage);
             // knock the player back and to the side
             var slamDirection = transform.InverseTransformDirection(target.position - transform.position);
             slamDirection.y = 0;
             slamDirection.z = 1;
             if (slamDirection.x >= 0)
                 slamDirection.x = 1;
             else
                 slamDirection.x = -1;
             target.SendMessage("Slam", transform.TransformDirection(slamDirection));
             lastPunchTime = Time.time;
         }
 
         // We are not actually moving forward.
         // This probably means we ran into a wall or something. Stop attacking the player.
         if (characterController.velocity.magnitude < attackSpeed * 0.3)
             break;
         
         // yield for one frame
         yield;
     }
 
     isAttacking = false;
     
     // Now we can go back to playing the idle animation
     animation.CrossFade("idle");
 }
 
 function ApplyDamage ()
 {
     animation.CrossFade("gothit");
 }
 
 function OnDrawGizmosSelected ()
 {
     Gizmos.color = Color.yellow;
     Gizmos.DrawWireSphere (transform.TransformPoint(punchPosition), punchRadius);
     Gizmos.color = Color.red;
     Gizmos.DrawWireSphere (transform.position, attackDistance);
 }
 
 @script RequireComponent(AudioSource)

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 Evil-Dog · Jun 07, 2011 at 02:49 PM 1
Share

Use the preformated text option to show code, now it's all messy the little icon with the binary.

avatar image G_Sacristan · Jun 07, 2011 at 03:15 PM 1
Share

chaos xD format as code

avatar image almo · Jun 07, 2011 at 03:17 PM 0
Share

Was going to fix it for him, but each line has a > in front of it. Can't be bothered.

avatar image CHPedersen · Jun 08, 2011 at 09:10 AM 0
Share

Besides formatting the code correctly, it's also a great idea to try to limit the amount of code you post in general. See if you can narrow your question down to a snippet of code that you think is the location of your problem. This greatly increases the chance that someone is going to bother trying to read it. Usually no one wants to go through 100+ lines of irrelevant code to pick out the bit that isn't working as expected.

avatar image Muzz5 · Jun 08, 2011 at 09:40 AM 0
Share

Formatted it for you guys.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by G_Sacristan · Jun 08, 2011 at 09:48 AM

i guess the problem is that idle is in Update and gotHit is not on Update (animation needs to be in Update like function to be showed correctly) .

Just use booleans to set which animation is active and which is not and it should be ok.

Comment
Add comment · Show 2 · 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 micha-max · Jun 08, 2011 at 10:02 AM 0
Share

thx for your answer, you have source with correct script ? with idle, walk , attack melee ? i give you my mail : designerz@live.fr if you have 10 $$anonymous$$ for explain me.

avatar image G_Sacristan · Jun 08, 2011 at 10:29 AM 0
Share

done - check ur email.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Zombie attack script help 1 Answer

How can I make my Axe in unity do damage to my Zombies 6 Answers

My attack script is broken, please help. 1 Answer

Attack Script 1 Answer

Ai Zombie Melee Attack script. 5 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