Quick fix help!

I am trying to get my code so that my Paddle will follow the mouse only on the X axis. It is a 3D Brickbreaker game. How do i make the paddle follow the mouse? My current script is working great, except when the mouse goes over the

var rayCastPlane : Transform;

function Update () {

	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;

		if (Physics.Raycast (ray, hit, 200)) {
			Debug.DrawLine (ray.origin, hit.point);
				
					transform.position.x = hit.point.x;
					transform.position.z = hit.point.z;
					
		}

	
	
}

Delete the transform.position.z line. :smiley:

Quick edit: I’m assuming you only want to move the paddle along one axis - your post unfortunately cut off early so not sure if that’s your only problem.

Thanks! Thats exactly what i was looking for :slight_smile:
But now i have another problem. How do i restrict the paddle from moving off the screen? I already
tried putting walls but the paddle just goes right through them

Just limit the movement to within the walls, ie:

var leftWall:Transform;
var rightWall:Transform;
var rayCastPlane : Transform;

function Update () {

	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	var hit : RaycastHit;

		if (Physics.Raycast (ray, hit, 200)) {
			Debug.DrawLine (ray.origin, hit.point);
				if (transform.position.x>leftWall.position.x&transform.position.x<rightWall.position.x){
					transform.position.x = hit.point.x;
					
				}	
		}

	
	
}

Where leftWall and rightWall are assigned in the inspector. You can easily tailor it so that the left side doesn’t go half through the left wall/right side through right wall easily enough by adding/subtracting half the width of your paddle.

Hope that helps.

Thanks so much!

Wait, Its not working!

Assets/Scripts/PaddleController.js(12,67): BCE0019: ‘position’ is not a member of ‘UnityEngine.GameObject’.

It was me being stupid: Updated! :smile:

Hah

Not giving me any errors. Thanks!

A moderator can close this now