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 Lord_Slifer · Aug 01, 2013 at 05:11 PM · movement scriptstopzombies

Zombies keep stopping in mid chase

I am trying the totally original idea of making a zombie game. I have a script to control the zombies, but they will constantly stop while they are attacking you. They pause for about a second, and then resume chasing you. It's probably something obvious, but this is my first project, so go easy. Here's my code:

 var walkSpeed = 3.0;
 var rotateSpeed = 30.0;
 var attackSpeed = 8.0;
 var attackRotateSpeed = 80.0;
 
 var damage = 10.0;
 var viewAngle = 20.0;
 var attackRadius = 2.5;
 var attackTurnTime = 1.0;
 var attackPosition = new Vector3 (0, 1, 0);
 
 var directionTravelTime = 2.0;
 var idleTime = 1.5;
 var attackDistance = 15.0;
 
 private var isAttacking = false;
 private var lastAttackTime = 0.0;
 private var nextPauseTime = 0.0;
 private var distanceToPlayer;
 private var timeToNewDirection = 0.0;
 
 var target : Transform;
 
 private var characterController : CharacterController;
 characterController = GetComponent(CharacterController);
 
 function Start () 
 {
     if(!target)
         target = GameObject.FindWithTag("Player").transform;
         
     yield WaitForSeconds(idleTime);
     
     while (true)    
     {
         
         yield Idle();
 
         yield Attack();
     }
 }
 
 function Idle()
 {
     while (true)
     {    if (Time.time > timeToNewDirection)
         {
             yield WaitForSeconds(idleTime);
             
             var RandomDirection = Random.value;
             if(Random.value > 5)
                 transform.Rotate(Vector3(0,20,0), rotateSpeed);
             else
                 transform.Rotate(Vector3(0,-20,0), rotateSpeed);
                 
             timeToNewDirection = Time.time + directionTravelTime;
         }
         var walkForward = transform.TransformDirection(Vector3.forward);
         characterController.SimpleMove(walkForward * walkSpeed);
         
         distanceToPlayer = transform.position - target.position;
         
         if (distanceToPlayer.magnitude < attackDistance)
             return;
             
         yield;
     }
 }
 function Attack()
 {
     isattacking = true;
     var angle = 0.0;
     var time = 0.0;
     while (angle > viewAngle || time < attackTurnTime)
     {
         time += Time.deltaTime;
         angle = Mathf.Abs(FacePlayer(target.position, attackRotateSpeed));
         move = Mathf.Clamp01((90 - angle) / 90);
         direction = transform.TransformDirection(Vector3.forward * attackSpeed * move);
         characterController.SimpleMove(direction);
         yield;
     }
     var lostSight = false;
     while(!lostSight)
     {
         angle = FacePlayer(target.position, attackRotateSpeed);
         if(Mathf.Abs(angle)> viewAngle)
         lostSight = true;
         if (lostSight)
             break;
             
         var location = transform.TransformPoint(attackPosition) - target.position;
         if(Time.time > lastAttackTime + 1.0 && location.magnitude < attackRadius)
         {
             target.SendMessage("ApplyDamage", damage);
             
             lastAttackTime = Time.time;
         }
         if(location.magnitude > attackRadius)
             break;
             
         if (characterController.velocity.magnitude < attackSpeed * 0.3)
             break;
             
             yield ;
         
     }
     isAttacking = false;
 }
 
 function FacePlayer(targetLocation : Vector3, rotateSpeed : float) : float
 {
     var relativeLocation = transform.InverseTransformPoint(targetLocation);
     var angle = Mathf.Atan2 (relativeLocation.x,
                 relativeLocation.z) * Mathf.Rad2Deg;
                 
     var maxRotation = rotateSpeed * Time.deltaTime;
     var clampedAngle = Mathf.Clamp(angle, -maxRotation, maxRotation);
     
     transform.Rotate(0, clampedAngle, 0);
     return angle;
 }
 
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
0

Answer by creighcl · Aug 05, 2013 at 05:46 PM

At a glance, I notice that you're calling Idle and Attack over and over in your start function. Your idle time is 1.5 seconds, so you would Idle for 1.5 seconds then run through the attack routine for the attack turn time which is set to 1.0. So he stops for 1.5, chases for 1, right?

Not sure what you want it to do, but I would suggest instead of looping in the Start method to alternate back and forth between these, maybe you constantly check to see if the player is in attack range and in the update function say if (inRange){ attack(); } else { idle(); }

Really depends on how your game is meant to work :-)

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

15 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

Related Questions

Fighting Streets of Rage style movement script 0 Answers

how do i set the eular angles of a parent object to the same thing as the child object, without changing the rotation of the child object. 0 Answers

I can't make my ball jump using distToGround and Physics.Raycast 0 Answers

Enemy shakes on Y-axis when is moving straight on the X-axis 0 Answers

Make an object move towards a Player 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