Need help inverting the X axis on my paddle

I’m back with the same problem, but operating completely differently!

I’ll post below pictures, but it appears that the mirrored paddle goes off screen, until I move my mouse off screen. Bit weird and quite different to the previous problem I have on the previous build that I have built myself where-as here I took resources from the course to catch up to where I was before I lost my stuff. I’ll post the code and some screenshots of the effect, the playspace and so on.

Reverse Paddle Code:

using UnityEngine;

public class ReversePaddle : MonoBehaviour
{

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{
    MoveWithMouse();
}
void MoveWithMouse()
{
    Vector3 paddlePos = new Vector3(0.5f, transform.position.y, 0f);
    float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
    paddlePos.x = Mathf.Clamp((mousePosInBlocks * -1), 0.5f, 15.5f);
    transform.position = paddlePos;
}
}

Effect:

Mouse Off Screen:

MMPlaySpace:


Any help will be much appreciated.

From memory, you wanted a paddle at the top and at the bottom and for the to move in the opposite direction. e.g player moves mouse to the right, bottom paddle moves to the right, top.paddle moves to the left.

Assuming the is the case, why not have one method which is responsible for calculating the position based on the mouse at that given time and then use that value twice - once normally for the bottom paddle and then reverse the value and use it for the top paddle.

You are then not reliant on where the mouse is, or rather was, and the paddles should keep in sync.

Not sure what you mean. Do you mean possibly linking to instead of the mouse position to the other paddle instead?

Exactly.

So if the bottom.paddle.moves left, the top paddle moves right. All decisions being based on the position of the first paddle.

Hmm…interesting idea…just been working a bit on that idea but struggling to create the code to find the paddle and use it in the context that’s currently there, however I get a feeling I need to re-work the code to work a different way.

Here’s what I have so far in regards to that:

using UnityEngine;

public class ReversePaddle : MonoBehaviour
{
private Paddle paddle;
void Start()
{
paddle = GameObject.FindObjectOfType();
}
void Update()
{
MoveWithMouse();
}
void MoveWithMouse()
{
Vector3 paddlePos = new Vector3(0.5f, transform.position.y, 0f);
float mousePosInBlocks = Input.paddle.x / Screen.width * 16;
paddlePos.x = Mathf.Clamp((mousePosInBlocks * -1), 0.5f, 15.5f);
transform.position = paddlePos;
}
}

Take the code you have on the bottom paddle, which you know is working 100% and moving the paddle as expected…

  • Add a variable for the top paddle
  • Find the top paddle in the Start() method and populate the variable
  • within the MoveWithMouse() calculate the position of the bottom paddle, probably easiest to store that in a temporary variable
  • assign this position to the bottom paddle
  • invert the value in the temporary variable
  • assign this position to the top paddle

Remember, they will have differing Y values that will need over riding in the position assignment.

If this works, you might consider making it a bit more generic rather than attaching to one paddle and controlling the other from it, but best to get it working first…

@Rob

Hi Rob, I’ve been re-reading over this post for the past couple of days and found that I am uncertain what is going on here. I had attempted to find the paddle in the code, however that’s about as far as I have gotten with this.

The movewithmouse command doesn’t like me adding numbers to it in any way. Do I need to add a Vector 3 of somekind?

sigh this “Simple Idea” I had it just getting more and more complicated as it goes on…think I’ll make a start with laser defender, but I’m going to keep working on this. I’m just moving on with the work so I can feel like I’m actually progressing in my learning.

Hi @Aron_Marczylo,

Can you paste up for me please your paddle script. Not the reverse one but the one which works for the main (bottom) paddle.

Also, provide for me the name of the second paddle (the top one).

I will have a proper look at thia for you this evening after my son has gone to bed (around 9.30pm UK time). :slight_smile:

u
sing UnityEngine;
public class ReversePaddle : MonoBehaviour
{
private Paddle paddle;
void Start()
{
paddle = GameObject.FindObjectOfType();
}
void Update()
{
MoveWithMouse(8.06, 0.34, 0);
}
void MoveWithMouse()
{
Vector3 paddlePos = new Vector3(0.5f, transform.position.y, 0f);
float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
paddlePos.x = Mathf.Clamp((mousePosInBlocks * -1), 0.5f, 15.5f);
transform.position = paddlePos;
}
}

That’s your reverse paddle code… can I have the paddle code please :slight_smile:

whoops, sorry about that:

using UnityEngine;
using System.Collections;

public class Paddle : MonoBehaviour {

public bool autoPlay = false;
public float minX, maxX;
private Ball ball;

void Start () {
	ball = GameObject.FindObjectOfType<Ball>();
}
void Update () {
	if (!autoPlay) {
		MoveWithMouse();
	} else {
		AutoPlay();
	}
}
void AutoPlay() {
	Vector3 paddlePos = new Vector3 (0.5f, this.transform.position.y, 0f);
	Vector3 ballPos = ball.transform.position;
	paddlePos.x = Mathf.Clamp(ballPos.x, minX, maxX);
	this.transform.position = paddlePos;
}
void MoveWithMouse () {
	Vector3 paddlePos = new Vector3 (0.5f, this.transform.position.y, 0f);
	float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
	paddlePos.x = Mathf.Clamp(mousePosInBlocks, minX, maxX);
	this.transform.position = paddlePos;
}

}

hehe, no worries… my lad will be off to bed shortly so I will have a look just after :slight_smile:


Updated Tue Dec 27 2016 21:35

Ok, he hasn’t gone to bed yet, but this is a first stab at it via a text editor, it hasn’t been tested but let’s see what happens…

Steps:

  • Comment out all of your code within the ReversePaddle.cs file, so that only the class definition remains, thus we still have a Type of ReversePaddle.

  • Save the ReversePaddle.cs file.

  • Add this line to your fields at the top of your class definition;

    public ReversePaddle reversePaddle;

  • Add this code after your existing code within the MoveWithMouse() method;

      // reverse paddle position code
      float reverseMousePosInBlocks = mousePosInBlocks * -1;
      
      Vector3 reversePaddlePos = new Vector3 (0.5f, reversePaddle.transform.position.y, 0f);
      reversePaddlePos.x = Mathf.Clamp(reverseMousePosInBlocks, minX, maxX);
      reversePaddle.transform.position = reversePaddlePos;
    
  • Within the Inspector, drag the ReversePaddle game object from the hierarchy within your scene to the ReversePaddle field exposed within your Paddle script component.

  • Within the Scene, ensure that both paddles are positioned in the centrally (horizontally) within your game space (make sure that the X value for the transform on both are identical)

  • Save the scene.

  • Save the project.

  • Run it, see what happens


Full code for Paddle.cs

using UnityEngine;
using System.Collections;

public class Paddle : MonoBehaviour {

    public bool autoPlay = false;
    public float minX, maxX;
    public ReversePaddle reversePaddle;
    private Ball ball;

    void Start () {
	ball = GameObject.FindObjectOfType<Ball>();
    }

    void Update () {
	if (!autoPlay) {
		MoveWithMouse();
	} else {
		AutoPlay();
	}
    }

    void AutoPlay() {
	Vector3 paddlePos = new Vector3 (0.5f, this.transform.position.y, 0f);
	Vector3 ballPos = ball.transform.position;
	paddlePos.x = Mathf.Clamp(ballPos.x, minX, maxX);
	this.transform.position = paddlePos;
    }

    void MoveWithMouse () {
	
	// your original code that handles moving the paddle
	Vector3 paddlePos = new Vector3 (0.5f, this.transform.position.y, 0f);
	float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
	paddlePos.x = Mathf.Clamp(mousePosInBlocks, minX, maxX);
	this.transform.position = paddlePos;
	
	// reverse paddle position code
	float reverseMousePosInBlocks = mousePosInBlocks * -1;
	
	Vector3 reversePaddlePos = new Vector3 (0.5f, reversePaddle.transform.position.y, 0f);
	reversePaddlePos.x = Mathf.Clamp(reverseMousePosInBlocks, minX, maxX);
	reversePaddle.transform.position = reversePaddlePos;
    }
}

Updated Tue Dec 27 2016 22:49

I should have added earlier, this is written quickly and for the purpose of getting it to work, there is a lot of duplication in here and I don’t really like the idea of one paddle controlling the other, so there are a number of improvements that could be made - if the above works! :slight_smile: Do let me know…

It thinks the code is incorrect and ontop of that there’s no area for me to drag the game object to…

Shouldn’t the begining be changed to a float with ReversePaddle?

it also doesn’t like the reverseMousePosInBlocks code nor the minX, MaxX part either.

Ok… a bit more work is needed then! :slight_smile:

Any chance you can put the whole project up somewhere for me to download?

The game object being passed in / referenced should be a “Paddle” - e.g. the actual paddle game object itself…

I did miss defining the reversePaddlePosInBlocks as a float though… so yes that’s wrong… damn, and there’s an extra bracket in there too!.. (correct in the above now - sorry)

Can you package up the whole thing for me to grab and open in Unity? I can look at it right now :slight_smile:


Updated Wed Dec 28 2016 00:13

Ok - I downloaded the complete course version from the end of the section in the end…

Opened the project and converted to Unity 5 but didn’t make any code changes with regards to the deprecated methods.

I’ve realised that it not so much the theory but the maths that is at fault with my previous solution.

It looks like a little offset is being generated and I’ve not quite worked out why yet. If you watch the video below you will see that one paddle is able to reach the edge before the other and then vice versa.

I’m thinking about the mouse position at the start of the game at the moment, e.g. both paddles are not perfectly aligned because the mouse isn’t in the middle of the screen when the game is run, it’s slightly off depending where you click on the start button.


Updated Wed Dec 28 2016 00:53

Ok - fixed…

On the line where I have;

float reversedMousePosInBlocks = (maxX + (mousePosInBlocks * -1));

…in this specific example, it’s a bit odd because min and max have been set to 1.18 and 14.8 respectively, I believe this is to do with the angled parts of the paddle, so your own game may vary slightly, but that said, as long as you factor in these values which total the width of the playable space + the mouse position in block multiplied by -1 (to reverse the value)

Anyway… the fix for this is;

float reversedMousePosInBlocks = ((maxX+minX) + (mousePosInBlocks * -1));

It now works, I believe, as you want - the code is a bit of a mess and can certainly be tidied up, and there are a few things that I personally would want to change in the rather hack and slash way I’ve cobbled this together… but - it works…


Paddle.cs

using UnityEngine;
using System.Collections;

public class Paddle : MonoBehaviour {

	public bool autoPlay = false;
	public float minX, maxX; 
	private Ball ball;

    public ReversedPaddle reversedPaddle;
	
	void Start () {
		ball = GameObject.FindObjectOfType<Ball>();
	}
		
	// Update is called once per frame
	void Update () {
		if (!autoPlay) {
			MoveWithMouse();
		} else {
			AutoPlay();
		}
	}
	
	void AutoPlay() {
		Vector3 paddlePos = new Vector3 (0.5f, this.transform.position.y, 0f);
		Vector3 ballPos = ball.transform.position;
		paddlePos.x = Mathf.Clamp(ballPos.x, minX, maxX);
		this.transform.position = paddlePos;
	}
	
	void MoveWithMouse () {
		Vector3 paddlePos = new Vector3 (0f, this.transform.position.y, 0f);
		float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
		paddlePos.x = Mathf.Clamp(mousePosInBlocks, minX, maxX);
		this.transform.position = paddlePos;


        
        Vector3 reversedPaddlePos = new Vector3(0f, reversedPaddle.transform.position.y, 0f);
        float reversedMousePosInBlocks = ((maxX+minX) + (mousePosInBlocks * -1));
        reversedPaddlePos.x = Mathf.Clamp(reversedMousePosInBlocks, minX, maxX);
        reversedPaddle.transform.position = reversedPaddlePos;
	}
}

ReversedPaddle.cs

using UnityEngine;
using System.Collections;

public class ReversedPaddle : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

Hi Ben, phew been busy myself with holidays, but wow you’ve really put in over 110%!

Been working on what you’ve given me and glad the errors have disappeared, but now I appear to have developed a new problem somehow.

It’s quite minor and I think I may have missed a step, but every-time I check your posts I can’t find anything that refers to it but when I try to drag the paddle into the box, it doesn’t take it at all. If I click on the icon to find the object and add it, it claims there are no objects or assets to add.

I’m at quite a loss as to what it’s failing to look for…

Update: Just before I posted this I suddenly realised I better check again and my gut told me to check the script and of course, my script was named “ReversePaddle” and your script refers to an object with a script named “ReversedPaddle” so that one letter difference made it impossible to identity. Now it’s able to stick!

Thankfully that is done! Now I just need to start making levels for the Mirror Madness, as I call it, take on the drawing course to add a few of my own assets and then upload to Itch.io! Thanks for all the help here Rob!

Hopefully our journey here helps someone else in the future :smiley:

1 Like

Really pleased to hear that the above code and instructions got it working for you Aron :slight_smile:

I will look forward to seeing your game showcased here soon :slight_smile:

Can you mark this one as solved now Aron :slight_smile:

yeah just trying to find out how to do that again…I did that for previous topics, but can’t seem to find out how to do that on here…maybe the post is too old or something?

1 Like

Aha! User guides are coming soon! :slight_smile:

In the mean time… if you click on the ellipses next to the Reply on a post you’ll expand the toolbar;



The Solution option is indicated in my screenshot above. This is the preferred method, although some people pop [Solved] in the title text if perhaps previously they had [Question] or [Help] etc.

Hope this helps :slight_smile:

yeah there’s only Share a link, bookmark or share a link. Nothing for editing, unlike the more recent posts… Also I’ve clicked on your solution post as the solution for the topic, but still doesn’t seem to have done it…

It adds a note to the first post in the topic and also pops a note against the specific post that you identified as being the solution.

It doesn’t display a tick or anything on the main forum summary;

https://community.gamedev.tv

…but does on the sub-forum summary, e.g. on the Unity one;

https://community.gamedev.tv/c/unity

Privacy & Terms