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

Move object to edge of another objects position

Discussion in 'Scripting' started by shawnrevels, Sep 20, 2018.

  1. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    So I feel like I'm messing up something very simple. I know it is but I cannot figure out why the object isnt moving to the new position.


    Code (CSharp):
    1. public StandSpawner SS;
    2.  
    3. transform.position = new Vector3 (transform.position.x, transform.position.y, SS._standPrefab.transform.position.z * 2);
    So I'm moving the object to the position of the stand but to its edge. This just keeps placing it in the middle of the stand.
     
  2. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    Did you intend to move it to the standSpawner's z-position multiplied by 2? Maybe you meant:
    Code (CSharp):
    1. transform.position = new Vector3 (transform.position.x, transform.position.y, SS._standPrefab.transform.position.z + 2);
    (Note the 'SS._standPrefab.transform.position.z + 2' as if it was multiplied by 2, it would give strange results, for example if the z-position was zero, then the new position would also be zero because 0*2=0.)

    If that didn't solve your problem, then please give your question in more detail, it's kind of difficult to understand ;)
     
  3. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    I figured it out. I wanted to move an object to another objects z position but to its edge along the z. I just moved it to the z position of the stand then adjusted its position relevant to the stands size since my stands spawn with diff scales.
     
  4. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    Alright, nice to see you figured it out ;)
     
    shawnrevels likes this.
  5. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    8 years C# your of thought I would have figured it out.