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 Jirogirg · Oct 14, 2013 at 11:53 PM · movementlerpzsimplemove

Problem with enemy moving onto player (Lerpz tutorial)

I'm working on this - https://www.assetstore.unity3d.com/#/content/5087

When player is close to enemy it should start moving. But it's not. I think the problem in simplemove. Because for one frame it moves, but then stops. And not moving until program enters in "while" cycle again. What can be wrong?

 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 * attackSpeed * move);
         
         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)
         {//3 
             // 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");
 }
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

14 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

Related Questions

Should you use CharacterController.Move or SimpleMove for enemies? 1 Answer

Player turns but won't move forward 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Get a NaN error when unpausing a game 1 Answer

Making a bubble level (not a game but work tool) 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