Question on mouse position

Hi Nina,
OK this works perfectly. In fact, it works better than my first project where I the paddle didn’t follow the mouse properly.

Update: I don’t know how to insert mathf.clamp so my paddle goes out of the screen. My code is below now. As following the above, it doesn’t properly follow what the tutorial showed. So how do we fix this?

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

public class Paddle : MonoBehaviour
{
/*
* 1440 * 1080

[SerializeField] float screenWidthInUnits = 50f;
[SerializeField] float minX = -14f;
[SerializeField] float maxX = 6f;
*/
/*
[SerializeField] float minX = 1f;
[SerializeField] float maxX = 7f;
*/



[SerializeField] Camera Camera; // DRAG YOUR  CAMERA;
[SerializeField] float screenWidthInUnits = 50f;    
[SerializeField] float minX = 1.2f;
[SerializeField] float maxX = 14.8f;



// Start is called before the first frame update
void Start()
{
    if (!Camera) Camera = FindObjectOfType<Camera>();

}

// Update is called once per frame
void Update()
{

    //Debug.Log(Input.mousePosition.x / Screen.width);

    /*
    float mousePosInUnits = Input.mousePosition.x / Screen.width * screenWidthInUnits;
    Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
    paddlePos.x = Mathf.Clamp(mousePosInUnits, minX, maxX);
    transform.position = paddlePos;
    */

    MovePaddle();
}

void MovePaddle()
{
    Vector3 newPos = Camera.ScreenToWorldPoint(Input.mousePosition);
    newPos.y = transform.position.y; newPos.z = transform.position.z;
    transform.position = newPos;
}

}

Clamp the newPos value like in the part you commented out.


[SerializeField] float paddleXsize = 1.4f; // size of a paddle =D

[SerializeField] Camera Camera; // DRAG YOUR  CAMERA;
[SerializeField] float screenWidthInUnits = 50f;    
float minX, maxX;

// Start is called before the first frame update
void Start()
{
    if (!Camera) Camera = FindObjectOfType<Camera>();

}

// Update is called once per frame
void Update()
{

    //Debug.Log(Input.mousePosition.x / Screen.width);

    /*
    float mousePosInUnits = Input.mousePosition.x / Screen.width * screenWidthInUnits;
    Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
    paddlePos.x = Mathf.Clamp(mousePosInUnits, minX, maxX);
    transform.position = paddlePos;
    */

    MovePaddle();
}

void MovePaddle()
{
    Vector3 newPos = Camera.ScreenToWorldPoint(Input.mousePosition);
    newPos.y = transform.position.y; newPos.z = transform.position.z;
//clamp mouse pos
      Vector2 lDCorner = Camera.ViewportToWorldPoint(new Vector3(0, 0f, Camera.nearClipPlane));
      Vector2 rUCorner = Camera.ViewportToWorldPoint(new Vector3(1f, 1f, Camera.nearClipPlane));
      minX = lDCorner.x + paddleXsize;
      maxX = rUCorner.x - paddleXsize;
      newPos.x = Mathf.Clamp(newPos.x, minX, maxX);

    transform.position = newPos;
}

Read unity documentation about Camera.ViewportToWorldPoint and Camera.ScreenToWorldPoint.
It it very important in your future projects.

Yes. I was checking something and i came across Camera.ViewportToWorldPoint and Camera.ScreenToWorldPoint. In fact, i will open a new thread to understand more about this. I hope you can help there.

Nina,
I need your help here. My code is as follows and the paddle still goes out of the screen.

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

public class Paddle : MonoBehaviour
{
    /*
     * 1440 * 1080

    [SerializeField] float screenWidthInUnits = 50f;
    [SerializeField] float minX = -14f;
    [SerializeField] float maxX = 6f;
    */
    /*
    [SerializeField] float minX = 1f;
    [SerializeField] float maxX = 7f;
    */



    [SerializeField] Camera Camera; // DRAG YOUR  CAMERA;
    [SerializeField] float screenWidthInUnits = 50f;    
    [SerializeField] float minX = 1.2f;
    [SerializeField] float maxX = 14.8f;



    // Start is called before the first frame update
    void Start()
    {
        if (!Camera) Camera = FindObjectOfType<Camera>();

    }

    // Update is called once per frame
    void Update()
    {

        //Debug.Log(Input.mousePosition.x / Screen.width);

        
        float mousePosInUnits = Input.mousePosition.x / Screen.width * screenWidthInUnits;
        Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
        paddlePos.x = Mathf.Clamp(mousePosInUnits, minX, maxX);
        //transform.position = paddlePos;

        MovePaddle();
    }

    void MovePaddle()
    {
        Vector3 newPos = Camera.ScreenToWorldPoint(Input.mousePosition);
        newPos.y = transform.position.y; newPos.z = transform.position.z;
        transform.position = newPos;
    }


}

newPos is not clamped. Compare your solution to @111100’s. How did they clamp the x-value of newPos? Do the same in your code and comment out what you had commented out. You don’t need to move the paddle twice per frame.

ok thank you to both of you. I didn’t fully understand what I was doing. All I did was copy and paste. Now it works perfectly. However, can the original code be simplified so it’s easier to understand? I don’t remember the original code unless I redo the tutorial but here’s part of the code for paddle.

   void Update()
    {

        //Debug.Log(Input.mousePosition.x / Screen.width);

        /*
        float mousePosInUnits = Input.mousePosition.x / Screen.width * screenWidthInUnits;
        Vector2 paddlePos = new Vector2(transform.position.x, transform.position.y);
        paddlePos.x = Mathf.Clamp(mousePosInUnits, minX, maxX);
        transform.position = paddlePos;
        */

        MovePaddle();
    }

As Rick says in course you should follow the course but make your own researches too. Thats how you learn.

1 Like

Feel free to simplify the code as it makes sense to you. If Rick was able to make his code graspable for >7 billions people on this planet, he would definitely do that. And he would be the only tutor with this skill. :slight_smile:

A little tip for your learning journey: Try to focus on the problem and the idea/concept of the solution instead of Rick’s (or someone else’s) specific code. That’ll make it easier for you to learn and to develop your own way of thinking and approaching problems. Why do I recommend that? Because if you show the same problem to another programmer, it might be that you will get a different solution. In many cases, there are multiple ways to solve problems.

Yes. I’ll go through once I’m done with the entire course. Then I’ll learn what I should understand. This should be in the coming months.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms