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 Michael 12 · Mar 22, 2011 at 02:20 PM · animationaiattackmelee

I'm still having Trouble with this AI script

OK I've made some changes to my Alien Zombie AI script but I still can't seem to get it to function the way it sould. Right now my Alien Zombies will follow me arround like lost puppies if I get within their attack range. And when I get out of range instead of returning to their patrol routine or even going back to just plain "Idle" they kind of run on the spot doing their "RunAttack" animation?? It's cufuddling me buggy trying to figure out why they are doing that.

Also I can't figure out how to get my Alien Zombie to inflict damage to my FPS player using his "Attack" animation when he is right in front of him or right up close if you like??

Here is my code so far: Sorry for posting this same question only asked differently but they seem to get lost after a while and get no more responses.

var speed = 3.0; var rotationSpeed = 5.0; var attackRange = 30.0; var dontComeCloserRange = 5.0; var pickNextWaypointDistance = 2.0; var target : Transform; var modelAnimation : Animation;

// Make sure there is always a character controller @script RequireComponent (CharacterController)

function Start () { // Auto setup player as target through tags if (target == null && GameObject.FindWithTag("Player")) target = GameObject.FindWithTag("Player").transform;

 Patrol();

}

function Patrol () { var curWayPoint = AutoWayPoint.FindClosest(transform.position); while (true) { var waypointPosition = curWayPoint.transform.position; // Are we close to a waypoint? -> pick the next one! if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance) curWayPoint = PickNextWaypoint (curWayPoint);

     // Attack the player and wait until
     // - player is killed
     // - player is out of sight     
     if (CanSeeTarget ())
         yield StartCoroutine("AttackPlayer");

     // Move towards our target
     MoveTowards(waypointPosition);

     yield;
 }

}

function CanSeeTarget () : boolean { if (Vector3.Distance(transform.position, target.position) > attackRange) return false;

 var hit : RaycastHit;
 if (Physics.Linecast (transform.position, target.position, hit))
     return hit.transform == target;

 return false;

}

function AttackPlayer () { var lastVisiblePlayerPosition = target.position; while (true) { if (CanSeeTarget ()) { // Target is dead - stop hunting if (target == null) return;

         // Target is too far away - give up 
         var distance = Vector3.Distance(transform.position, target.position);
         if (distance &gt; attackRange * 3)
             return;

         lastVisiblePlayerPosition = target.position;
         if (distance &gt; dontComeCloserRange)
             MoveTowards (lastVisiblePlayerPosition);

             //Attack player with "Attack" animation which is       basically a clawing and biting animation
         //this "Attack" is supposed to inflict damage to the FPS player
         else {
             modelAnimation.CrossFade("Attack");
             RotateTowards(lastVisiblePlayerPosition);
         }

         var forward = transform.TransformDirection(Vector3.forward);
         var targetDirection = lastVisiblePlayerPosition - transform.position;
         targetDirection.y = 0;

         var angle = Vector3.Angle(targetDirection, forward);
     } 

     yield;
 }

}

function SearchPlayer (position : Vector3) { // Run towards the player but after 3 seconds timeout and go back to Patroling var timeout = 3.0; while (timeout > 0.0) { MoveTowards(position);

     // We found the player
     if (CanSeeTarget ())
         return;

     timeout -= Time.deltaTime;
     yield;
 }

}

function RotateTowards (position : Vector3) { SendMessage("SetSpeed", 0.0);

 var direction = position - transform.position;
 direction.y = 0;
 if (direction.magnitude &lt; 0.1)
     return;

 // Rotate towards the target
 transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
 transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);

}

function MoveTowards (position : Vector3) { var direction = position - transform.position; direction.y = 0; if (direction.magnitude < 0.5) { SendMessage("SetSpeed", 0.0); return; }

 // Rotate towards the target
 transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
 transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);

 // Modify speed so we slow down when we are not facing the target
 var forward = transform.TransformDirection(Vector3.forward);
 var speedModifier = Vector3.Dot(forward, direction.normalized);
 speedModifier = Mathf.Clamp01(speedModifier);

 // Move the character
 direction = forward * speed * speedModifier;
 GetComponent (CharacterController).SimpleMove(direction);

 SendMessage("SetSpeed", speed * speedModifier, SendMessageOptions.DontRequireReceiver);

}

function PickNextWaypoint (currentWaypoint : AutoWayPoint) { // We want to find the waypoint where the character has to turn the least

 // The direction in which we are walking
 var forward = transform.TransformDirection(Vector3.forward);

 // The closer two vectors, the larger the dot product will be.
 var best = currentWaypoint;
 var bestDot = -10.0;
 for (var cur : AutoWayPoint in currentWaypoint.connected) {
     var direction = Vector3.Normalize(cur.transform.position - transform.position);
     var dot = Vector3.Dot(direction, forward);
     if (dot &gt; bestDot &amp;&amp; cur != currentWaypoint) {
         bestDot = dot;
         best = cur;
     }
 }

 return best;

}

Comment
Add comment · Show 2
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 Kiloblargh · Jun 04, 2011 at 04:38 PM 0
Share

This is barely an answer, but I would check out what Speed$$anonymous$$odifier is doing with a Debug.Log. That's mainly because I don't really understand or trust "dot". If you totally know what that's doing, disregard. Also, "while (true) {" sticks out as something I wouldn't write. Have several boolean variables declared at the top and check them;

 var patrolling : System.Boolean = true; 
 while (patrolling) {

I think you need to have distinct modes for your enemy and maybe now you don't. That is, either it's patrolling, or it's chasing the player, or it's attacking, but make sure it's never trying to do more than one thing at a time. Right now it may be running more than one of those functions at the same time which causes the 'lost puppy' behavior.

avatar image RetepTrun · Dec 11, 2011 at 11:45 PM 0
Share

Is this just an edit of the FPS tutorial robot script?

To make the attack animation do damage I could write a little something that would work for the robot of that tutorial. I would attach a ball collider to this hand that did damage to things inside when it was "fired"

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Owen-Reynolds · Mar 22, 2011 at 04:45 PM

The animation can fool you -- for example, your code may be correctly not moving for a while, but the runAttack animation you started 6 seconds ago is still playing, so you think the code is trying to run.

Maybe leave animation off, so you can focus on the movement and turning. Once the code works, there are usually very obvious places to set animation. For example, whenever the code sets direction to 0, also play "idle."

For checking the move code, these are two old programming tricks, Unity style:

o Add things like Debug.Log("Back to patrol from attack"); so you can see if the flow is correct.

o Show some vars in the inspector: Make public var ShowWayPnt : Transform; and set it in Patrol: ShowWayPnt = waypointPosition; This will tell you if the stall is because you're on the Waypoint already, or if direction is somehow always set to 0 (do the same thing to show direction.)

Eventually, you'll get a result that isn't what you expected, which leads to the problem.

Comment
Add comment · Show 6 · 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 Michael 12 · Mar 22, 2011 at 05:27 PM 0
Share

Thanks Pat for the awesome advice. But looking at my code can you spot any what might be obvious errors to someone far more code savy than I? I've been checking the Unity Animation script reference but I can't seem to get much headway from that. I'm such a "noob" ;)

avatar image Owen-Reynolds · Mar 23, 2011 at 09:08 PM 0
Share

Do the animation later, after the motion is done. Focus on why it won't return to the waypoint after attacking. If you have zero animation, it will be much easier to see if it is stopped, or just moving very slowly (for example.) I didn't spot any obvious problems.

avatar image Michael 12 · Mar 25, 2011 at 02:33 PM 0
Share

I'm not sure what's going on inside of this thing. I've noticed that sometimes when I'm out of range they will wander back to their waypoint and sometimes they just sort of "Run on the Spot" at the last place where I sudenly bacame out of range. Plus, and this is the REALLY big pain in the ass, is I have STILL NOT been able to figure out haw to make these guys actually attack my player and inflict damage. It's getting to the pount where I'm just going to say "Frak It!" and just give them weapons and make a gun fire animation and just do it that way which is TOTALLY NOT what I want! :(

avatar image Owen-Reynolds · Mar 25, 2011 at 04:19 PM 0
Share

Test until you see a pattern -- maybe they are O$$anonymous$$ if they lose you out of attack range, but get stuck going from attack range to out-of-sight. That will narrow down the problem. Highlight the enemy and watch what the variables are. For attack, break it down. Do 1 point of damage every frame you are in range. Then think how to wait in between damage ticks (another coroutine? A timeLeftToattack variable?)

avatar image Michael 12 · Mar 25, 2011 at 09:06 PM 0
Share

How does that work with his "Attack" animation though. Heck I don't really understand how this script call the other animations into play. That FPS tutorial does not get into HOW that AI script works they just say attach this tou your robot and then do this and that and the other things and Whallah! Your robot works. There is an AI animation script and I guess they somehow talk to each other but where and how I don't understand. Right now this is a $$anonymous$$AJOR stumbling block for me been searching all over hells 1/2 acre trying to find answers too.

Show more comments
avatar image
1

Answer by LukaKotar · Dec 11, 2011 at 10:49 AM

Try setting rotation speed a little bit higher, if they spin around you in circles. I hope this answers your question! ;)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Ai Zombie Melee Attack script. 5 Answers

Not identifying the collider? 2 Answers

How to make an animation play for AI? 0 Answers

Melee attacking, using force and health values 1 Answer

2 Animations At once 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