Search Unity

NavMeshAgent MoveToClickPoint and ThirdPersonCharacter from Standard Asset Run?

Discussion in 'Navigation' started by xtimus, Oct 26, 2016.

  1. xtimus

    xtimus

    Joined:
    Jun 16, 2014
    Posts:
    11
    I am trying to make the ThirdPersonCharacter from the standard assets navigate to a point on the map by mouse click. I combined some of the AiCharacterControl and MoveToClickPoint script from the docs to make the character walk but I'm having trouble making the character run. I tried changing velocity but no matter how high I change it, the character just walks. What I need to know is how to trigger the running animation.
    One other problem I see is that when the character jumps, he/she stays in the air.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityStandardAssets.Characters;
    3.  
    4. namespace Project{
    5.     public class MoveToClickPoint : MonoBehaviour {
    6.         private NavMeshAgent agent;
    7.         private ThirdPersonCharacter character; // the character we are controlling
    8.  
    9.         void Start() {
    10.             agent = GetComponent<NavMeshAgent>();
    11.             character = GetComponent<ThirdPersonCharacter>();
    12.         }
    13.  
    14.         void Update() {
    15.             if(Input.GetMouseButtonDown(0)) {
    16.                 RaycastHit hit;
    17.  
    18.                 if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100)) {
    19.                     agent.destination = hit.point;
    20.                 }
    21.             }
    22.             if(agent.remainingDistance > agent.stoppingDistance)
    23.                 character.Move(agent.desiredVelocity, false, false);
    24.             else
    25.                 character.Move(Vector3.zero, false, false);
    26.         }
    27.     }
    28. }