Moving a gameobject with mouse

Hello all.

I have just begun to play with Unity and trying to learn by doing small, simple projects and following tutorials. However - some of the (simplest) thing I want do to seems very hard by this point. Therefore I beg you to be kind - I promise I’ll be better soon :slight_smile:

Yesterday I begun a simple project which I need to make a gameobject move by mouse input.
I have attached the following script to the gameobject (an excerpt):

if(Input.GetAxis("Mouse X") < 0) {
transform.position.x -= playerMoveSpeed * Time.deltaTime;
    	//print("Moved left");
    }
    
    if(Input.GetAxis("Mouse X") > 0) {
    	transform.position.x += playerMoveSpeed * Time.deltaTime;
    	//print("Moved right");
    }
    
    if(Input.GetAxis("Mouse Y") > 0) {
    	transform.position.z += playerMoveSpeed * Time.deltaTime;
    	//print("Moved up");
    }
    
    if(Input.GetAxis("Mouse Y") < 0) {
    	transform.position.z -= playerMoveSpeed * Time.deltaTime;
    	//print("Moved down");
    }

This should be simple in my mind, but the gameobject don’t follow the mouse! I can move the object right after starting the game, but the object is moving slower and slower and almost stop moving even with large mouse movements. The mouse pointer and objects is also not the same (the pointer start at Unitys “playbutton” above the gamescreen, but the object is on the bottom).

Can anybody give me a hint how to solve this, or tell me if it’s possible at all.

It is simple. I was actually having the same problem a while back.
Step 1: Make a Canvas and resize it to the scope of your main camera view.
Step2 : Make an Image a blank Image and set it’s alpha to 0. The image should also take the same space as the Parent Canvas. you set the alpha to 0 so it’s invisble.
Step3: Write this script and attach it to the image object.

`using UnityEngine;
using UnityEngine.EventSystems;

public class MousePostioning : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
public Transform object;

public void OnPointerUp(PointerEventData data)
{
//----
}

public void OnPointerDown(PointerEventData mouse)
{
object.position = Camera.main.ScreenToWorldPoint(mouse.position);
}

public void OnDrag(PointerEventData mouse)
{
object.position = Camera.main.ScreenToWorldPoint(mouse.position);
}

Then just drag and drop what ever you want to move into the object spot and your good to go. Just
make sure the image and it’s parent Canvas are enabled during play time.