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

Follow object to mouse position

Discussion in 'Scripting' started by ilyamep, Jul 12, 2015.

  1. ilyamep

    ilyamep

    Joined:
    Feb 21, 2015
    Posts:
    12
    Hello everyone
    Its hard for me to explain what I want to reach
    So I found this video where you can see what I want to do



    I need to follow some object to mouse, but only in unity's X and Z in 3D world
    I tried google many times but only what I found is this

    (Observe from 10 seconds)

    Code (CSharp):
    1.     private Vector3 screenPoint;
    2.  
    3.     void Start () {
    4.         screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
    5.     }
    6.  
    7.     void Update () {
    8.         Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    9.         Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint);
    10.         transform.position = curPosition;
    11.     }
    12. }
    But this uses x y z coordiantes :(

    Thanks for help and sorry for my terrible English.
     
  2. BPPHarv

    BPPHarv

    Joined:
    Jun 9, 2012
    Posts:
    318
    Try this instead.

    Code (CSharp):
    1.  
    2.   void Update () {
    3.      //do a raycast from the camera through the screen
    4.      //at the mouse cursor position into the 3d world
    5.      RaycastHit hit;
    6.      Vector3 mousePos = Input.mousePosition;
    7.      if (!Physics.Raycast(Camera.main.ScreenPointToRay(mousePos), out hit))
    8.        {
    9.        //didn't hit anything. don't do anything
    10.        return;
    11.        }
    12.  
    13.      //must have hit something
    14.      //set the transform position to the point in 3d space where
    15.      //our raycast intersected something
    16.      transform.position = hit.point;
    17.      }
    18.  
     
    yektasarioglu likes this.
  3. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    With your current code, you just need to alter the y you get.

    Code (CSharp):
    1.  
    2. curPosition.y = transform.position.y;
    3. transform.position = curPosition;
    4.  
    If you are looking for spell effects like that, you should research decals as they will be extremely helpful.
     
  4. ilyamep

    ilyamep

    Joined:
    Feb 21, 2015
    Posts:
    12
    That is what I need, thanks, it works perfectly
     
  5. ilyamep

    ilyamep

    Joined:
    Feb 21, 2015
    Posts:
    12
    Thanks for your attention, I will search decals as well.