0
\$\begingroup\$

I instantiated the prefab "paddle" twice then I tried clamping it to the y-axis only; using Mathf.Clamp, however, the min and max values end up being applied to both instances of "paddle" and hence they can move in the x-axis

ok so I'm trying to make a 2d mobile pong game, I have a prefab "paddle" which are the rackets/bats which the balls collide with.

I instantiated the same prefab twice one on each side of the screen :

Paddle paddle1 = Instantiate(Paddle) as Paddle;
Paddle paddle2 = Instantiate(Paddle) as Paddle;
paddle2.Init(true); //right paddle
paddle1.Init(false); //left paddle

Now i want each of the paddles to only be able to move in the y axis (UP And DOWN).

So I tried clamping them with:

var poss = transform.position;
poss.x =  Mathf.Clamp(transform.position.x, -1.0f, 1.0f);
transform.position = poss; 

However, this caused the left paddle to still be able to move to the right and the right paddle to be able to the left( I don't want them to move in the axis at all).

This is the file GameManager.cs:

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

public class GameManager: MonoBehaviour
{
    public Ball Ball;

    public Paddle Paddle;

    public static Vector2 topRight;
    public static Vector2 bottomLeft;

    // Start is called before the first frame update
    void Start() 
    {
        bottomLeft = Camera.main.ScreenToWorldPoint(new Vector2 (0,0));
        topRight = Camera.main.ScreenToWorldPoint(new Vector2 
        (Screen.width,Screen.height));

        Instantiate(Ball); 

        //Paddle paddle1 = Instantiate(Paddle) as Paddle;
        Paddle paddle1 = Instantiate(Paddle) as Paddle;
        Paddle paddle2 = Instantiate(Paddle) as Paddle;
        paddle2.Init(true); //right paddle
        paddle1.Init(false); //left paddle
    }

    void Update()
    {

    }

}

This is the file paddle.cs:

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

public class Paddle : MonoBehaviour
{
    Rigidbody2D rb;
    float directionY;
    float speed;
    float height;

    float distance;
    string input;  
    bool isRight;

    // Start is called before the first frame update
    void Start()
    {
        height = transform.localScale.y;
        speed=5f;   
    }

    public void Init(bool isRightPaddle)
    {
        isRight = isRightPaddle;
        Vector2 pos = Vector2.zero;

        if(isRightPaddle)
        {
            //Place paddle on the right of the screen
            pos = new Vector2(GameManager.topRight.x, 0);
            pos -= Vector2.right * transform.localScale.x; //Move a bit to the left
        }
        else 
        {
            //Place paddle on the left of the screen
            pos = new Vector2(GameManager.bottomLeft.x, 0);
            pos += Vector2.right * transform.localScale.x; //Move a bit to the right
        } 

        //Update this paddle's position
        transform.position = pos;
    }

    void Update()
    {
        var poss = transform.position;
        poss.x =  Mathf.Clamp(transform.position.x, -8.0f, 8.0f);
        transform.position = poss; 
    }
}

I expect each paddle to not be able to move in the x axis. Only in the y axis

c# unity3d mobile

\$\endgroup\$
1
\$\begingroup\$

TL;DR: you are updating the x position value while you want to update the y value.

Please have a look at this part of your code:

        var poss = transform.position;
        poss.x =  Mathf.Clamp(transform.position.x, -8.0f, 8.0f);
        transform.position = poss; 

What you're doing here is telling Unity:

At every frame, (line 1) I want you to get the position of my transform, (line 2) update and clamp the x value of my position to be contained between -8.0 and 8.0 but don't touch any other values, (line 3) set the position with the clamped x value as the new position of my transform.

This code will let your paddle moving on x (left / right) from -8.0 to 8.0 values. In Unity, by default, left/right axis is x, up/down is y, and forward/back is z. If you want your paddle to only move on Y when you press up and down arrow, you just have to only update the Y value of their transform.position and clamp it to the min / max values you need while forcing the default x and z values that you need.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.