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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need to alter Z axis on gameObject when localScale is changed

Discussion in 'Scripting' started by Reject76, Jan 5, 2019.

  1. Reject76

    Reject76

    Joined:
    Jun 12, 2015
    Posts:
    38
    Hello good people of the Unity Community :)

    I'm thoroughly enjoying getting back to Unity and C# after a long absense. I'm usually used to VB and even then its mostly straight-forward stuff (self-taught).

    Now I'm making a Arknoid clone and I'm trying to do a simple "perk" where the player (i.e. paddle) can either get enlarged or minimized.

    So far so good...I've created a capsule that, when triggered, will either enlarge or minimize the paddles localScale by 1.5f along the Z-axis.

    However, on either side of the paddle is a wall / bumbstop. My problem is thus: when the paddle minimzed AND happens to be in contact with the wall when the paddle is reset to its original wider state, the paddle will overlap the wall and get stuck.

    I figure the solution is to check the paddle.transform.position for its current position and then move the paddle along its Z-axis enough for it to safely grow back to its original scale.

    Trouble is, I can't really figure out how. I can set three totally new vector variables by doing a transform.position = new Vector3 (5,5,5), but that's not what I want. I simply need to nudge or move it out of harms way by about 1.5f either left or right (depending on if the paddle is near the left or right wall.

    Help?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,584
    You got two walls, left and right. Right?
    Hence you can define pad location, on which side closest wall is.
    Then get position (distance) to the wall.
    If distance is less than scaling halve of factor, do nudge.
    Your nudge distance will be halve of scaling factor - position to the wall.

    So basically,
    if your distance to wall is 2 and halve of scaling factor is 3, then 4 - 3 = 1.
    Hence move by 1 away from a wall.

    That should be it.
     
  3. Reject76

    Reject76

    Joined:
    Jun 12, 2015
    Posts:
    38
    Think I got it :) Much obliged, mate!
     
    Antypodish likes this.