Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Move in X axis and stop on walls [Solved]

Discussion in 'Scripting' started by Noamxrx, Dec 12, 2021.

  1. Noamxrx

    Noamxrx

    Joined:
    Jul 25, 2018
    Posts:
    42
    I've started developing a Mobile game a few days ago and it's going great.
    But I'm having only 1 issue with the development of the movement system.

    The player controls a racket position in its X axis using touch.
    I'm using Transform.position but it ignores collisions (Walls in my case).



    I tried using both Transform and Rigidbody.
    I've got better results using Transform, but my problem hasn't been resolved and the ball can bypass walls.

    The racket built like this:
    -- Instantiator (The object that moves)
    -- Ball, the item that the player drops, Its randomly generated with different sizes.


    Anybody knows how can I make Transform.position (Or another method idk) stop when colliding with walls?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    Try these steps:

    - make two public Transform variables, one called
    LeftLimit
    and one called
    RightLimit


    - make two empty GameObjects (named the same) as children of your playfield above, moving them to where you want the center of the racquet to stop moving

    - in the inspector, drag those two GameObjects into the variable fields

    - in code just ask if the position is less or not and clamp it:

    Code (csharp):
    1. float newXPosition = ... however you get this now
    2.  
    3. if (newXPosition < LeftLimit.position.x)
    4. {
    5.    newXPosition = LeftLimit.position.x;
    6. }
    7. if (newXPosition > RightLimit.position.x)
    8. {
    9.    newXPosition = RightLimit.position.x;
    10. }
    11.  
    12. // now use the new X position to drive your racquet, always within bounds now
    You can even shorten it a lot with Mathf.Clamp() but I prefer the longhand above because it's more obvious and (to me) reads better.
     
  3. Noamxrx

    Noamxrx

    Joined:
    Jul 25, 2018
    Posts:
    42
    Hey!, thanks for your awesome response :)
    I added clamping using Mathf:
    Code (CSharp):
    1. if (Input.touchCount > 0)
    2.             {
    3.                 touch = Input.GetTouch(0);
    4.                 if (touch.phase == TouchPhase.Moved)
    5.                 {
    6.                 transform.position = new Vector3(transform.position.x + touch.deltaPosition.x * moveSpeed, transform.position.y, transform.position.z);
    7.                 transform.position = new Vector3(Mathf.Clamp(transform.position.x, leftLimit.position.x, rightLimit.position.x), transform.position.y, transform.position.z);
    8.                 }
    9.  
    10.             }
    The method works great, but since I'm using 4 prefabs and each one has a different size, some of them over passing the border, also by shrinking the limits sizes the small balls will be far from the border.


    Do you know how I can use the same method with different ball sizes?.
    I thought about storing the Limits for each prefab, but I rather prefer that it will detect walls automatically.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    You could make a ball property size script to tell how big each ball is and use that value to inset the value of the clamping.

    Are these balls supposed to fall into place? You may want to use 2D physics to do that part... not sure of your game design. If you did that the you would need colliders around the field.
     
  5. Noamxrx

    Noamxrx

    Joined:
    Jul 25, 2018
    Posts:
    42
    You are hero!
    I tried detecting the size of the ball using triggers but after reading your comment and researching, it works using the method you said!

    This is the code:
    Code (CSharp):
    1.     public void Update()
    2.     {
    3.             if (Input.touchCount > 0)
    4.             {
    5.                 touch = Input.GetTouch(0);
    6.                 if (touch.phase == TouchPhase.Moved)
    7.                 {
    8.                 transform.position = new Vector3(transform.position.x + touch.deltaPosition.x * moveSpeed * Time.deltaTime, transform.position.y, transform.position.z);
    9.                 transform.position = new Vector3(Mathf.Clamp(transform.position.x, leftLimit.position.x + transform.localScale.x / 2, rightLimit.position.x - transform.localScale.x / 2), transform.position.y, transform.position.z);
    10.                 }
    11.  
    12.             }
    13.     }


    I really appreciate your help!
     
    Kurt-Dekker likes this.