Need help inverting the X axis on my paddle

I created a new game type for my block breaker where there is a second paddle at the top, but the idea is that is moves inverted compared to your paddle to make this particular game mode difficult. However, despite my initial thoughts, attempting to get this to work is not very easy.

I could’ve simply made it a double-paddle, but I wanted to be a bit more daring and now I’m back on the forums asking for help as no one appears to have found a solution for this very problem.

I copy and pasted part of the code from the other paddle, thinking I’d just need to find an easy way to tinker with the x position, but I’ve yet to find a way that makes it work and not shoving it into either the right or left collider.

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, 0.5f, 15.5f);
    transform.position = paddlePos;
}
}

update:

Thought I would give everyone a picture as to what exactly I’m trying to accomplish with this. The first picture below is what I get when I load the game up and want to remain the same. Both paddles in the centre, facing each-other.

When I move to one side, I want the second paddle at the top to move in an opposite direction so it’s inverted to where my mouse moves as demonstraited below in a terrible editing job in Pain shows you, maybe moving it further so it avoids the score like shown below.

I’ve tried to look up many ways how to do this and a common suggestion seems to be you have to find out what the location for the centre is. I hope someone is able to come up with a solution for this problem I’m having with what to code and how to code it.

bump…no suggestions?

Try multiplying the mousePosInBlocks by -1 in the math.Clamp code. Wrap it in brackets so it is definitely calculated first…

1 Like

I’ve tried a bit of doing this, but no idea what I might be doing wrong if that’s the case.

Do you mean this?

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

that doesn’t appear to work and I’ve tried placing the -1 everywhere, but it doesn’t appear to work or the paddle ends up shooting off into the right side (pushing against the collider) or to the left side of the screen and that’s where I’ve been stuck.

Does the origin is in the middle of the game space or it is on the bottom left side?

Have you tried this?

{
Vector3 paddlePos = new Vector3(0.5f, transform.position.y, 0f);
float mousePosInBlocks = -(Input.mousePosition.x / Screen.width * 16);
paddlePos.x = Mathf.Clamp(mousePosInBlocks, 0.5f, 15.5f);
transform.position = paddlePos;
}

Nope… meant this…

{
    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;
}

Untested, but figured the multiplication of -1 on the mousePosInBlocks should give you the opposite…

Still not fully firing here I’m afraid, but I figured;

1 * -1 = -1, 2 * -1 = -2

which gives you the opposite of

1, 2 etc…

Of course, looking at it now, I can see that this would create negative numbers which are less than the lowest value of your clamp - hence, the forced to the left… the above might work with a central origin.

So, how about taking the calculated value away from half of the boundary limit also?

15.5f + (1 * -1) = 14.5
15.5f + (2 * -1) = 13.5

15.5f + (14 * -1) = 1.5
15.5f + (15 * -1) = 0.5

in the above, the multiplier in the brackets is the mousePosInBlocks value;

15.5f + (mousePosInBlocks * -1)

1 Like

The bottom code appeared to work somewhat, besides the paddle on the bottom and top leaving a gap on the right depending where they are.

Reverse:

Normal Paddle:

umm…that’s exactly what it was originally. That’s the basic value for moving the mouse…

Actually I added an “-( xxx)”,
you have to make sure that the center of the game space is the origin (0,0,0), perhaps this is why this gap is appearing

…my 15.5 should have been 16… there are 16 blocks aren’t there… sorry, normally I would test it before posting. On mobile at the moment.

@Rob did those changes and both were the same problem unfortunately.

@Joao_Dalvi I attempted that code before with the minus, but it just shoved the paddle to the right side of the screen.

Hi Aron, so it’s working, but leaving a little bit of padding on either side depending which way it goes, is that correct?

When you start the scene, are they perfectly aligned at the beginning, e.g. vertically align (in the middle of the scene)?

1 Like

It appears to only be the right-side that it’s leaving room on both paddles. The top one hits left when the bottom one hasn’t hit the right one and the bottom one hits left when the top one hasn’t like the image in one of the earlier posts.

Aron, the problem probably is what Rob just said, the game space is out of the center, the x position should be half the game space width. Can you send a print of the inspector of the play space?

I renamed and re-prefabed the playspace in this instance because there are now two lose-colliders. So far the game is a bit too difficult, so that’s something I’ll work out later and the playspace will be quite different due to a previous topic where Rob helped me with programming the lives counter at the top: Making lives in Block Breaker

note:

I apologise for sounding short with you earlier. I was getting frustrated and I’ve been suffering a back problem from my second job, caught a cold and doubts keep trying to slip in to scream “Ah-ha! You suck and you’ll never improve!” but those aren’t excuses so I wanted to apologise for being a bit frustrated and coming off as ****ed off earlier in the topic. I kind of wish I didn’t want to add this to the game, but at the same time the more things I learn (even if I may never use them again) will improve my over-all knowledge.

Long story short, I’m sorry for being a bit off-the-handle earlier in my responses :frowning:

2 Likes

No problem, I know how it is, I have a ton of those too :slight_smile: I hope that you get them sorted out.

Im away from my pc right now, but it seems that the game space is indeed out of the center, try putting it on 8 on the x axis(otherwise you will have to check where is the center), perhaps the resolution of the game display
could be the problem too, try turning on maximise on play to see how it behaves? Let us know

1 Like

I just tried it, and neither appears to work sigh I’ll get back to building some levels though. I’ve been getting creative with them, but nowhere near as creative as some people have gone with their project and up and above it all!

1 Like

Tomorrow ill test it too and I will reply here if I find a solution :slight_smile:

1 Like

Just tried it, and adding the “-” does seems to work as it should. I think that either your gamespace width is different of 16 or it is out of the center

1 Like

Sounds like that makes sense…apart from the left-hand side is fine. Hmm…sorry as this is a big ask from the help you’ve given for me already, but are you able to alter the code so that you get a similar effect?

I would’ve thought if it was off centre that the left-side would also be affected. Hmm…I’ll tweek the settings and see what I get.

The center of the gamespace should be 0 in order to this code work (You can check it by using The Rect Tool to drag and snap another gameobject to the center of the gamespace to see it’s position) .

Would you mind uploading the project so I can take a look in order to see what is causing this problem? It will be my pleasure to help :slight_smile: you can send it through private mensage if you want.

Privacy & Terms