Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need Help with Moving Enemy using Mecanim?

Discussion in 'Scripting' started by CelticKnight, Apr 27, 2015.

  1. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Hello Guys,

    I was trying to make my way through the Stealth Tutorial - starting at part 4 because that was the part that interested me as I had the player moving, shooting, creating damage in a First Person way.

    But because of my complete stupidity it lost me very quickly into lesson 403. So, I modified the goal greatly and tried to get the robot model to "Walk" to a single checkpoint. I did implement Lesons 401 - 402 as I could basically, I and mean basically understand what was happening in those lessons.

    Anyway, what I managed to come up with was a robot "sliding" to the checkpoint - and not walking. It's probably something to do with the the variables speed and angular_speed are not being sent to the enemy controller, but, I don't know for certain and need some help.

    I don't expect to be given the code, nor, want that, but, need tips or directions as to what I need to do and/or Unity docs that will help or a YouTube tut that will help me figure out that specific goal.

    The code I used is below.

    As always thankyou for you time and patience.

    Regards,
    CK.


    Code (JavaScript):
    1.  
    2. #pragma strict
    3.  
    4.  
    5. var player: Transform;
    6. var distanceToRobot: float;
    7. var anime: Animator;
    8. var theBool: boolean;
    9. var navMesh: NavMeshAgent;
    10. //wherewewantourrobottomove to
    11. var wayPoint: Transform;
    12.  
    13.  
    14. function Start () {
    15. anime = GetComponent(Animator);
    16. navMesh = GetComponent(NavMeshAgent);
    17. anime.SetLayerWeight(1, 1f);
    18. //this causes a problem I can't fix ?!?!?!??!???
    19. //anime.SetLayerWeight(2,1f);
    20. theBool = false;
    21. }
    22.  
    23.  
    24. function Update () {
    25. distanceToRobot = Vector3.Distance(player.position, transform.position);
    26. print("DistancetoRobot:"+distanceToRobot);
    27.  
    28. navMesh.SetDestination(wayPoint.position);
    29. //how to make robot walk to waypoint
    30. //instead of slide
    31.  
    32.  
    33. if(distanceToRobot>15){
    34. theBool = false;
    35. anime.SetBool("PlayerInSight", theBool);
    36. }
    37.  
    38. print("DistancetoRobot:"+distanceToRobot);
    39.  
    40. if(distanceToRobot<15){
    41. theBool = true;
    42. anime.SetBool("PlayerInSight", theBool);
    43. }
    44.  
    45. }//end update
    46.  
     
  2. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    I have yet to see the tutorials but let me make an assumption that the animation controllers that the "enemy" is using is similar to that of "player". In that case, you would also need to set the values for motion into the "enemy" controller, like what you do for "player". Like which variable makes the "player" animate move, you need to set it into that of the "enemy" too.
     
  3. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Sorry it took me so long to reply I have been extremely busy with martial art classes, including some surprises and a very sore body :D.

    So, to the topic at hand, I also assume that the controls of the "player" are like that of the "enemy", however, because I wanted my player to be a first person controller rather than the tutorials 3rd person controller what I have done is very different. So, I have no idea how to set the variables to control movement properly, or get the robot to "turn" correctly or even add in more waypoints, how to get the shooting animation of the robot linked up to a projectile, how to make the robot stop and shoot when the "player" is in a certain radius and then go back to patrolling. For this I am totally forgetting about how to get the robot guard to chase after us - or getting the other robot guards to converge, or have the guards recognise the players sounds :confused:.

    There are just so many things I can't do - that it's getting progressively harder to see what I can do!

    I really need another tutorial rather than one like the Unity one which just spoonfeeds the code to you and with some minor problems in 4.6.1f - and not working at all to some reports in Unity5 and they aren't going to change it either so it just gets further and further out-of-date and I've looked for books on the subject and there are none.

    Thankyou and Regards,
    CK.
     
  4. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    Can you show me what the PlayerController script looks like?
    Sorry, I've never looked at all these tutorials. So I wouldn't know how the AnimationControllers are set up in the tutorials.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    That might just be the animation having looping turned off. There's a checkbox named "loop time" on each animation - it has to be on for the animation to play more than once.
     
  6. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
    Looking at the script, he's not sending any other parameters into the animation controller to tell the enemy to play the animation.
     
  7. CelticKnight

    CelticKnight

    Joined:
    Jan 12, 2015
    Posts:
    378
    Still, it did give me something else to check over to make sure it was correct - and that's always a good thing :). Which I duly did!

    Yes, without the setFloat's for the animator being set was one of the reasons for the sliding - I had a feeling about that but wasn't sure, there was another contributing reason from another section of the project which has now gone bye-bye :p.