Moving object to mouse position

I’m trying to store my mouse position inside a variable and then use this variable on the z-as of my player, but I’m getting errors anyone can explain what I do wrong.

error CS0103: The name mousePose' does not exist in the current context error CS0119: Expression denotes a type’, where a variable', value’ or method group' was expected error CS0120: An object reference is required to access non-static member UnityEngine.Component.transform’

// Update is called once per frame
void Update () {
playerMove ();
}

void playerMove()
{
if(Input.GetMouseButtonDown(0))
{
Debug.Log ("clicked");
mousePose = Camera.main.ScreenToWorldPoint (Input.mousePosition);
player.transform.position = Vector3 (5, 5, mousePose);
}
}

You need to declare mousePose at the top of the class:

Vector3 mousePose;
public GameObject player;

You need to drag your player from the hierarchy onto the player variable in the inspector.
On this line you only need the z part of the Vector3:

player.transform.position = new Vector3 (5, 5, mousePose.z);
1 Like

I changed it but now when I click it takes the y position and the block falls through the plane. When I remove the position it says that vector3 can’t take 1 argument. What I want is to move the block towards the mouse click position only one the z-as.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPlayer : MonoBehaviour {

    Vector3 mousePose;
    public GameObject player;

    // Use this for initialization
    void Start () {
     
    }
 
    // Update is called once per frame
    void Update ()
    {
        playerMove ();
    }

    void playerMove()
    {
        if(Input.GetMouseButtonDown(0))
        {
            mousePose = Camera.main.ScreenToWorldPoint (Input.mousePosition);
            player.transform.position = new Vector3 (mousePose.y, mousePose.z) * Time.deltaTime;
        }
    }

}

I think you want:

 player.transform.position = new Vector3 (player.transform.position.x, player.transform.position.y, mousePose.z) * Time.deltaTime;

I’m not sure, though.