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

using navmeshagent for point and click controller

Discussion in 'Scripting' started by Pierre-skinnerup, Mar 3, 2013.

  1. Pierre-skinnerup

    Pierre-skinnerup

    Joined:
    Aug 20, 2012
    Posts:
    8
    Hey everybody

    Have anybody tried to make a Diablo style point and click controller using the NavMeshAgent for pathfinding ?? im trying to make one and got some problems with adding some animation to my character.

    im using mecanim Animator


    Best Regards

    Pierre Skinnerup
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,614
    How are you adding the animation? It doesn't need to have anything to do with what kind of controller you're using for motion.

    Without knowing more there's not a lot we can do to help you.

    In case it is of use to you, in one of my projects I've completely decoupled animation from the movement and physics and have them in seperate components. The physics and movement component does its thing however it wants. The animation component caches the current and previous position and orientation, and applies an appropriate animation depending on how they're changing.
     
  3. Pierre-skinnerup

    Pierre-skinnerup

    Joined:
    Aug 20, 2012
    Posts:
    8
    Here is what i got so far :) the problem i have is that when it reached its destination it begins to jitter between idle and run animation :)

    Code (csharp):
    1. #pragma strict
    2.  
    3. var minionMeshAgent : NavMeshAgent;
    4.  
    5. var anim : Animator;
    6. private var animSpeed = 1;
    7. private var h : float;
    8. private var a : boolean;
    9.  
    10. private var targetPosition : Vector3;
    11. var playerTransform : Transform;
    12.  
    13. var isLocked = false;
    14.  
    15. function Start () {
    16.     targetPosition = this.transform.position;
    17. }
    18.  
    19. function Update () {
    20.  
    21.     if(Input.GetKey(KeyCode.Mouse1)) {
    22.         var playerPlane = new Plane(Vector3.up, transform.position);
    23.         var playerRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    24.         var hitdist : float;
    25.  
    26.         if (playerPlane.Raycast (playerRay, hitdist)) {
    27.             var targetPoint = playerRay.GetPoint(hitdist);
    28.             targetPosition = playerRay.GetPoint(hitdist);
    29.             isLocked = false;
    30.         }
    31.     }
    32.      if(Input.GetKey(KeyCode.Mouse0)) {
    33.          var enemyRay = Camera.main.ScreenPointToRay (Input.mousePosition);
    34.          var enemyHit : RaycastHit;
    35.          
    36.          if(Physics.Raycast(enemyRay, enemyHit, Mathf.Infinity)) {
    37.              if(enemyHit.collider.gameObject.tag == "Enemy") {
    38.                  targetPosition = enemyHit.point;
    39.                  isLocked = true;
    40.              }
    41.          }
    42.      }
    43.  
    44.     minionMeshAgent.destination = targetPosition;
    45.     CheckAnimation();
    46. }
    47.  
    48. function CheckAnimation () {
    49.     var dist = Vector3.Distance(targetPosition, playerTransform.position);
    50.     if(minionMeshAgent.hasPath == false) {
    51.     h = 0.0;
    52.     }
    53.     else if(minionMeshAgent.hasPath == true) {
    54.     h = 1.0;
    55.     }
    56.    
    57.     print(minionMeshAgent.hasPath);
    58.     anim.SetFloat("Speed", h);
    59.     anim.speed = animSpeed;
    60. }
    61.  
    62. function Attacking ()
    63. {
    64.      var enemyDist = Vector3.Distance(targetPosition, playerTransform.position);
    65.          if(isLocked == true) {
    66.              if(enemyDist <= 0.23) {
    67.                  a = true;
    68.              }
    69.              else
    70.              {
    71.                  a = false;
    72.              }
    73.          }
    74.          anim.SetBool("isAttacking", a);
    75. }
     
  4. Pierre-skinnerup

    Pierre-skinnerup

    Joined:
    Aug 20, 2012
    Posts:
    8
    a little bump :)

    still working on solving this is there a way to check if the Position i get from the ray are reachable with the navmesh ?

    Best Regards

    Pierre