Mouse won't stick with paddle

So the title pretty much covers the whole issue, when I go to move the paddle with the mouse they wont stick together. There is always a gap between them what’s weird though is when looking at the mouse position through debug.log it comes up the same as the paddle transform position which can’t be right right?. I made the game in 2D first and didn’t have this issue before but now im having trouble making it in 3D.

I left the code the same as in the 2D course for this post but the things i’ve tried to make work are, using “screentoworldpoint” , “transform.transformpoint”. Little to zero knowledge on what either of those two things did.

Pictures

Summary

Code

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

public class Paddle : MonoBehaviour
{
    [SerializeField] float ScreenWidthInUnits = 16f;
    [SerializeField] float xMin = 1f, xMax = 15f;


    // Start is called before the first frame update
    void Start()
    {

    }

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

    public void Paddle_Movement()
    {
        Debug.Log(Input.mousePosition.x / Screen.width * ScreenWidthInUnits);

        float PaddlePositionX_Axis = Input.mousePosition.x / Screen.width * ScreenWidthInUnits;

        Vector3 PaddlePosition = new Vector3(transform.position.x, transform.position.y, transform.position.z);
        
        PaddlePosition.x = Mathf.Clamp(PaddlePositionX_Axis, xMin, xMax);
        transform.position = PaddlePosition;
      
    }
}

Edit: I am very lost because no matter what I tried it felt like the distance between the mouse and paddle wasn’t shortening and the only thing that was happening when changing the code around was the paddle would slow down / go faster.

It’s all good now, I managed to find a video online that explains it quite well, https://www.youtube.com/watch?v=7OJQ6MbHuvQ. It was very helpful, there was a slight issue of trying to clamp the x position afterwards but that was an easier fix compared to the previous issue I was having.

Code

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

public class Paddle : MonoBehaviour
{

    [SerializeField] float xMin = -8.5f, xMax = 8f;
    [SerializeField] float DistanceZ = 6.5f, DistanceY = -7.5f;

    [SerializeField] bool UseInitial_CameraDistance = false;

    private float Actual_DistanceZ;
    private float Actual_DistanceY;
    


    // Start is called before the first frame update
    void Start()
    {
        SetCameraDistance();
    }

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

        Paddle_Movement();
        
    }

    public void Paddle_Movement()
    { 

        Vector3 Mouse_Position = Input.mousePosition;
        Mouse_Position.z = Actual_DistanceZ;
        Mouse_Position.y = Actual_DistanceY;

        transform.position = Camera.main.ScreenToWorldPoint(Mouse_Position);

        var pos = transform.position;
        pos.x = Mathf.Clamp(transform.position.x, xMin, xMax);
        transform.position = pos;

    }

    public void SetCameraDistance()
    {

        if (UseInitial_CameraDistance)
        {
            Actual_DistanceZ = (transform.position - Camera.main.transform.position).magnitude;
            Actual_DistanceY = (transform.position - Camera.main.transform.position).magnitude;
            
        }
        else
        {
            Actual_DistanceY = DistanceY;
            Actual_DistanceZ = DistanceZ;
        }

    }
} 
1 Like

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

Privacy & Terms