About 'Move Object With Mouse'!

In this video (objectives)…

  1. Find the position of the mouse on the screen.
  2. Convert the mouse position into a relative proportion of our screen represented as game units.
  3. Link movement of our mouse to movement of our paddle.

After watching (learning outcomes)… Move a game object so that it matches the position of the player's mouse.

(Unique Video Reference: 8_BR_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

I was wondering why you aren’t getting a reference to the Camera and get the positions like such:

private Camera camera;


    Vector2 mousePos = camera.ScreenToWorldPoint(Input.mousePosition); 

    Vector2 paddlePos = new Vector2 (mousePos.x, transform.position.y);
    transform.position = paddlePos;

Furthermore you could get the sprite length of the paddle to clamp its position so it perfectly aligns with the edges of the screen.

Not trying to say mine is better, I’m just curious about your solution.

1 Like

I am almost positive that I’m not using ‘best practices’, but this is my solution and it seems to work. It took me a lot longer than it should have to figure it out.

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

public class Paddle : MonoBehaviour {

    [SerializeField] float screenWidthInUnits = 16f;
	
	// Update is called once per frame
	void Update () {
        Vector2 paddlePosition = new Vector2((Input.mousePosition.x / Screen.width * screenWidthInUnits), transform.position.y);
        transform.position = paddlePosition;
	}
}

[Problem] When I run the game, the paddle disappears. Please help me with this. Thanks

I agree with you. I think, this way much easier.

By the way; you can use Camera.main instead of “camera”

Vector2 mousePos =  Camera.main.ScreenToWorldPoint(Input.mousePosition); 
Vector2 paddlePos = new Vector2 (mousePos.x, transform.position.y);
transform.position = paddlePos;

My x axis mouse position is not reading 0 at the bottom left corner of the game as it is in the lecture. Is this an issue? Am I in the right place to discuss this? I have marked the area that the mouse was in when I took the screen shot.

A good test is to grab your single block in the scene and move it to the bottom left corner. If the x value in the inspector for the block is roughly the same as you console readout for your cursor then you know the cursor script is accurate and the problem is simply that your background (and camera) are not aligned such that their bottom left corner is at the 0,0 point of the world. I forget which lecture we cover it, but at some point we go through the process of aligning the bottom left of our game with the 0,0 point.

Just to add to Rick’s reply… from memory, the background image was set to 1440 pixels wide wasn’t it. So, if for example you had your pivot point still set in the centre, rather than in the bottom left corner, the middle of the image would be 0,0 and the bottom left corner would be -720. Looks like the values you are seeing are remarkably close to that - I’d check the pivot point if I were you.


See also;

Wow, Super fast! Thank you both for getting back to me guys. I did set the pivot to bottom left as per lecture but this was still the outcome. Anyways, I put it down to bad workflow, jumping ahead and what not so deleted the project and started again. Same problem… until I realised I was mousing in the editor window and not the game window thus giving me the incorrect value. What a wally, :upside_down_face: Really enjoying the course so far and hat off to you Rick for keeping the fun in learning, Good stuff :call_me_hand:

2 Likes

You’re very welcome :slight_smile:

Glad you manage to work the issue out, always satisfying when you do it yourself. :+1:

Privacy & Terms