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

Click to move in first person with a NavMeshAgent

Discussion in 'Navigation' started by mmonis2014, Feb 4, 2018.

  1. mmonis2014

    mmonis2014

    Joined:
    Feb 4, 2018
    Posts:
    3
    • Would it be possible to do this? I've referred to these resources without any luck:
      Question on a similar topic (I admit that for this one, I could look a bit more closely as to how to do this, but I'm not sure if I clearly understand what I'd need to do with the best answer - plus, the answers there don't appear to refer to the NavMeshAgent, which I used from some other resources)
    • Unity3D livestream
    • Another video
    Currently, I have a Capsule standing on a plane without a Rigidbody, and with a SmoothMouseLook script attached to the Main Camera (to which I have made it a child of the Capsule). I have baked a NavMesh. I am aiming to move the Capsule to no avail.
    2018-02-04 17_18_51-.png 2018-02-04 17_18_59-.png 2018-02-04 17_19_05-.png
     
  2. mmonis2014

    mmonis2014

    Joined:
    Feb 4, 2018
    Posts:
    3
    Judging from this, what I believe I need is a ray going from the Capsule to the point clicked in the camera, but I'm not sure how to do this.
     
  3. mmonis2014

    mmonis2014

    Joined:
    Feb 4, 2018
    Posts:
    3
    Based on this code, I have it working, but I'm wondering how I could keep my camera still:


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.AI; // for NavMeshAgent to work
    6. public class PlayerMovement : MonoBehaviour {
    7.     private Animator anim;
    8.     private NavMeshAgent navMeshAgent;
    9.     private Vector3 position;
    10.     public static Vector3 cursorPosition;
    11.     public CharacterController controller;
    12.     public float speed;
    13.     private Vector3 newPosition;
    14.     // shooting and animation variables - ignore
    15.     public float shootDistance = 10f;
    16.     public float shootRate = .5f;
    17.     private Transform targetedEnemy; // coordinates shoot enemy
    18.     private Ray shootRay;
    19.     private RaycastHit shootHit; // info about what hit
    20.     private bool walking; // walk animation playing
    21.     private bool enemyClicked;
    22.     private float nextFire;
    23.     // for setup - do before "start"
    24.     void Awake() {
    25.         // get components and apply to field
    26.         navMeshAgent = GetComponent<NavMeshAgent>();
    27.     }
    28.     // Update is called once per frame
    29.     void Update() {
    30.         locateCursor();
    31.         // cast ray from mouse position from camera to scene
    32.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    33.         RaycastHit hit; // more than boolean
    34.         // https://answers.unity.com/questions/885341/fps-using-mouse-to-move-rather-than-keys.html
    35.         if (Input.GetMouseButtonDown(0)) // https://www.youtube.com/watch?v=GANwdCKoimU
    36.         {
    37.             //Locate where the player clicked on the terrain
    38.             locatePosition();
    39.         } else
    40.         {
    41.         }
    42.         // if (navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance)
    43.         moveToPosition();
    44.     }
    45.     void locatePosition()
    46.     {
    47.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    48.         RaycastHit hit;
    49.         if (Physics.Raycast(ray, out hit, 1000))
    50.         {
    51.             if (hit.collider.tag != "Player" && hit.collider.tag != "Enemy")
    52.             {
    53.                 navMeshAgent.destination = hit.point;
    54.             }
    55.         }
    56.     }
    57.     void locateCursor()
    58.     {
    59.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    60.         RaycastHit hit;
    61.         if (Physics.Raycast(ray, out hit, 1000))
    62.         {
    63.             cursorPosition = hit.point;
    64.         }
    65.     }
    66.     void moveToPosition()
    67.     {
    68.         //Game Object is moving
    69.         if (Vector3.Distance(transform.position, position) > 1)
    70.         {
    71.             Quaternion newRotation = Quaternion.LookRotation(position - transform.position, Vector3.forward);
    72.             newRotation.x = 0f;
    73.             newRotation.z = 0f;
    74.             transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
    75.             // controller.SimpleMove(transform.forward * speed);
    76.             // transform.position = Vector3.Lerp(transform.position, newPosition, 0.5f);
    77.            
    78.         }
    79.         //Game Object is not moving
    80.         else
    81.         {
    82.            
    83.         }
    84.     }
    85. }
    86.  
    In this video I made, I did not move my mouse, yet the game object is rotating against my intentions.
     
  4. alexaruel

    alexaruel

    Joined:
    Dec 12, 2023
    Posts:
    1
    Hi @mmonis2014 - did you ever figure out how to get this to work? I would like to do the same thing.