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 am having a hard time with getting the mouse to hover over the paddle to move. My mouse is aligning further to the left of the paddle, making the game hard to play. HELP PLEASE!

Hi Chris,

Could you perhaps pop a screenshot up of the issue you are experiencing?

I’m having a similar issue. It looks like there might be something wrong with the math I’m doing in the Paddle script. Everything is moving correctly, just not lining up with the cursor.

I’ve attached screenshots of the problem, the main camera inspector (I think the problem might be here), and the paddle inspector. Paddle%20Sprite%20Inspector Main%20Camera%20Inspector Paddle%20Placement%20Error

Here is my script:

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

public class Paddle : MonoBehaviour
{
    [SerializeField] float WorldUnitWidth = 16f;
    // Start is called before the first frame update
    void Start()
    {
        Vector2 startPosition = new Vector2(transform.position.x, transform.position.y);
    }

    // Update is called once per frame
    void Update()
    {
        float currentPosition = Input.mousePosition.x / Screen.width * WorldUnitWidth;
        Vector2 paddlePosition = new Vector2(currentPosition, transform.position.y);
        transform.position = paddlePosition;
    }
}

Got it figured out. Here’s what I missed:

All the math was correct. When I set the pivot point of the background sprite to the bottom left corner, I didn’t then set the transform position to 0,0. When the background was centered with the camera, it’s transform location was -8,-6. I moved it to 0,0, then adjusted the camera to 8,6 so that everything lined up perfectly.

This was kind of a jumbled explanation, if it didn’t make sense, I can break down my steps and maybe make a video for you if need be. This was a super frustrating problem for me, so I’d be more than happy to help you out.

1 Like

Sir if i want to develop this game and play on android phone then what changes are necessary as the player will be controlled with touch
please help me with it !

Hi all,

I’v got the camera and my background aligned, but would still need to correct the x-position by subtracting 4.675 from my value. Only then do my mouse pointer and paddle align.

Fixed it on my own by moving the camera.

1 Like

i got my paddle position to X = 12, so i move all my camera and background, and also bring the ball and the box to be center of line with x=12.

its work, but i think its my own bug lol

Having big problem with my cursor paddle atm just hope i can get this sorted out

Privacy & Terms