Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

MouseClick player moving

Discussion in 'Scripting' started by CodeWurm, May 16, 2016.

  1. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Hello

    I'm searching for a good understandable script to let my player move around the game when I click on a place on the terrain.
    Also for a good camera position that will follow the player, and to look around, you know something like in a mmorpg.

    I found this on a website: http://www.thegamecontriver.com/2014/10/move-to-mouse-click-touch-position-unity.html

    To let my player move, but when I click on the terrain to let my player move, It selects my terrain instead of moving my player.

    Somebody can help me?
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    It's quite easy, you get the position you need to move the player to by raycasting from the camera in the position of the mouse:
    Ray ray = yourcamera (can be Camera.main, if you have it taggged).ScreenPointToRay(Input.mousePosition);
    HitInfo hit;
    if (Physics.Raycast(ray, out hit)) {
    Vector3 point = hit.point;​
    }
    then with a lerp (or by setting the velocity and checking of the player reached the point), you can easily move the player to the position you wanted him to go
     
  3. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Code (CSharp):
    1. public class mouseClick {
    2.  
    3.         void Update() {
    4.             if (Input.GetMouseButtonDown(0)){
    5.                 transform.position = new Vector3(10, 20, 30);
    6.                
    7.                 Camera.main.ScreenPointToRay    (Input.mousePosition);
    8.     Gives a red sign     >>        HitInfo hit;
    9.                 if (Physics.Raycast (Ray, out hit)) {
    10.                     Vector3 Point = hit.point; << point is red
    11.  
    12.                
    13.                 }
    14.             }
    15.         }
    16.     }
    17. }
     
  4. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    are you planning on using a NavMeshAgent on the player character?
     
  5. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    If you mean rotate to the point where I click than yes.

    I'm totally beginner, I just want
    If I click on left mouseButton move to that point where I clicked, and ofcourse I want the camera to follow the player.
     
  6. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
  7. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    this should work for moving an object to clicked position (doesnt take into account positioning the player above ground in sloped areas or many other things but it's a simple example):

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ClickToMove : MonoBehaviour
    5. {
    6.     public float duration=50,height=2;
    7.  
    8.     bool flag;
    9.     Vector3 endPoint;
    10.  
    11.     void Update ()
    12.     {
    13.         //check if left mouse button is clicked
    14.         if(Input.GetMouseButtonDown(0))
    15.         {
    16.             RaycastHit hit;
    17.             Ray ray;
    18.             //cast ray from camera to mouse position
    19.             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    20.  
    21.             //Check if the ray hits any collider
    22.             if(Physics.Raycast(ray,out hit))
    23.             {
    24.                 flag = true;
    25.                 endPoint = hit.point;
    26.                 endPoint=new Vector3(endPoint.x,endPoint.y+height,endPoint.z);
    27.             }
    28.  
    29.         }
    30.         //check if the flag for movement is true and the current gameobject position is not same as the clicked position
    31.         if(flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
    32.         {
    33.             //move the gameobject to the desired position
    34.             gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1/(duration*(Vector3.Distance(gameObject.transform.position, endPoint))));
    35.         }
    36.         //set the movement indicator flag to false if the endPoint and current gameobject position are equal
    37.         else if(flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude))
    38.         {
    39.             flag = false;
    40.             Debug.Log("I am here");
    41.         }
    42.     }
    43. }
     
    CodeWurm likes this.
  8. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Thank you worked realy good, just couldn't check if he was facing the right direction because I just created a 3d object without and face oresomething.