Search Unity

how convert mouse position to player position

Discussion in 'Scripting' started by kishore_1234, Mar 15, 2018.

  1. kishore_1234

    kishore_1234

    Joined:
    Aug 14, 2017
    Posts:
    8
    if (Input.GetKeyDown(KeyCode.Space))
    {
    RaycastHit hitinfo;
    Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    if (Physics.Raycast (ray, out hitinfo))
    {
    placeCubeNear (hitinfo.point);
    }
    }
    }

    private void placeCubeNear(Vector3 clickpoint)
    {
    var finalposition = grid.GetNearestPointOnGrid (clickpoint);
    GameObject.CreatePrimitive (PrimitiveType.Cube).transform.position = finalposition;
    }


    I want instantiate cube at the players position but i have only for the mouse can any help me with this.
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    First of all, please use code tags as specified at the first thread on this forum.
    What is the problem with your current code?
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Do you perhaps want to check where your raycast has hit, so it only works when you click on the player, and not just any 'hit' in the world? :) Or something else?
     
  4. kishore_1234

    kishore_1234

    Joined:
    Aug 14, 2017
    Posts:
    8
    hi I cleared it thanks a lot