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

Unity 3d character move along with mouse

Discussion in 'Scripting' started by Dhanalaxmi Ragireddy, Aug 19, 2015.

  1. Dhanalaxmi Ragireddy

    Dhanalaxmi Ragireddy

    Joined:
    Nov 12, 2013
    Posts:
    24
    How can i move a character along with the mouse in 3d??
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    That's a loaded question: About a dozen different ways. One of the most popular is to raycast from the mouse position, in the direction the camera is facing, either every frame or on click. Using the raycast, you'd use some layer or "RaycastHit" checks to figure out what it is that needs to happen (if the raycast hit something on the "terrain" layer, or the object his had the "terrain" tag, then pass the Vector3 of the spot where the raycast hit the terrain object to the movement controller for the character, for instance).

    Here's a MoveToPoint function that you can use if you're attaching a perfectly normal CharacterController script from the Unity StandardAssets to your character.
     
  3. Dhanalaxmi Ragireddy

    Dhanalaxmi Ragireddy

    Joined:
    Nov 12, 2013
    Posts:
    24
    if(Input.GetMouseButtonDown(0))
    {
    Rayray=Camera.main.ScreenPointToRay(Input.mousePosition);
    if(Physics.Raycast(ray,outhit))
    {
    obj=hit.collider.gameObject;
    if(obj.name == "boy_anim_fbx")
    {
    m_startpos=hit.point;
    m_goboy.transform.position = m_startpos;
    }
    }
    }
    if(Input.GetMouseButtonUp(0))
    {
    m_endpos =hit.point;
    m_goboy.transform.position = m_endpos;

    }

    i was trying like this but its not working...
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    "Not working" isn't enough information. How isn't it working exactly? I don't know what half of these variables are because their definitions and how they're being used aren't here- so post the rest of the script too. Also, be sure to put any code you post in [code ][/code ] tags.
     
    Timelog likes this.
  5. Dhanalaxmi Ragireddy

    Dhanalaxmi Ragireddy

    Joined:
    Nov 12, 2013
    Posts:
    24
    Code (csharp):
    1.  
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4.  
    5. publicclassRiding : MonoBehaviour
    6. {
    7. publicAnimatorm_boyAnimation;
    8. publicGameObjectm_goboy;
    9. Vector3m_startpos,m_endpos;
    10. RaycastHithit;
    11. GameObjectobj = null;
    12.  
    13.  
    14. //Usethisforinitialization
    15. voidStart ()
    16.  {
    17.  }
    18.  
    19. //Updateiscalledonceperframe
    20. voidUpdate ()
    21.  {
    22. if(Input.GetMouseButtonDown(0))
    23.  {
    24. Rayray=Camera.main.ScreenPointToRay(Input.mousePosition);
    25. if(Physics.Raycast(ray,outhit))
    26.  {
    27. obj=hit.collider.gameObject;
    28. if(obj.name == "boy_anim_fbx")
    29.  {
    30. //m_boyAnimation.SetBool("walk",false);
    31. m_startpos=hit.point;
    32. m_goboy.transform.position = m_startpos;
    33.  }
    34.  }
    35.  }
    36. if(Input.GetMouseButtonUp(0))
    37.  {
    38. m_endpos =hit.point;
    39. //m_boyAnimation.SetBool("walk",true);
    40. m_goboy.transform.position = m_endpos;
    41. m_goboy.transform.position = Vector3.MoveTowards(m_startpos,m_endpos,Time.deltaTime*1);
    42.  
    43.  }
    44.  
    45.  }
    46.  
    47. }
    48.  
     
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    EDIT: Sorry, a bit annoyed because it's still nearly impossible to read. Give me a minute.
     
  7. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Something more like this, perhaps? I'm not really sure what the point of the "OnMouseUp" stuff was.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Riding : MonoBehaviour
    5. {
    6.     public Animator m_boyAnimation;
    7.     public GameObject m_goboy;
    8.  
    9.     private Transform m_goboyTransform;
    10.     private Vector3 m_goalPos;
    11.  
    12.     void Start()
    13.     {
    14.         if (m_goboy != null)
    15.             m_goboyTransform = m_goboy.GetComponent<Transform>();
    16.  
    17.         m_goalPos = m_goboyTransform.position;
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if (m_goboy != null)
    23.         {
    24.             if (Input.GetMouseButtonDown(0))
    25.             {
    26.                 RaycastHit hit;
    27.                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    28.  
    29.                 if (Physics.Raycast(ray, out hit))
    30.                 {
    31.                     if (hit.collider.name == "boy_anim_fbx")
    32.                     {
    33.                         m_goalPos = hit.point;
    34.                     }
    35.                 }
    36.             }
    37.  
    38.             if (Vector3.Distance(m_goboyTransform.position, m_goalPos) >= .1f)
    39.             {
    40.                 //m_boyAnimation.SetBool("walk", true);
    41.                 m_goboyTransform.position = Vector3.MoveTowards(m_goboyTransform.position, m_goalPos, Time.deltaTime * 1);
    42.             }
    43.             else
    44.             {
    45.                 //m_boyAnimation.SetBool("walk", false);
    46.                 m_goalPos = m_goboyTransform.position;
    47.             }
    48.         }
    49.     }
    50. }