Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

2D transform change not working

Discussion in 'Physics' started by jsull1, Aug 31, 2018.

  1. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    99
    I wrote this simple script just to move my character down on the z axis when it hits a trigger and I have zero idea why its not working. I've ran a debug and the trigger is working fine but not changing anything. The floats change but they don't seem to be effecting my characters position at all. I've done this before and it worked fine, my character has a rigid body and colliders and is simulated so its got to be the script.

    Code (CSharp):
    1. public float zaxis;
    2.     public float DownLevel;
    3.  
    4.     void Update () {
    5.         gameObject.transform.position = new Vector3 (this.transform.position.x,
    6.             this.transform.position.y,
    7.             zaxis);
    8.  
    9.  
    10.     }
    11.     void OnTriggerEnter2D(Collider2D other) {
    12.         if (other.CompareTag ("DogDropper")) {
    13.             zaxis = DownLevel;
    14.         }
    15.     }
    16.  
    17. }
     
  2. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    207
    Well, since I don't know what DownLevel is I am going to take a guess as to what your problem is.
    I suspect it is this:

    Code (CSharp):
    1. [QUOTE="jsull1, post: 3626881, member: 1763880"]
    2. gameObject.transform.position = new Vector3 (this.transform.position.x,
    3.             this.transform.position.y,
    4.             zaxis);
    5. [/QUOTE]
    You need to subtract from your current position and you are not doing that. So it should look something like this.

    Code (CSharp):
    1. void Update () {
    2.         gameObject.transform.position = new Vector3 (this.transform.position.x,
    3.             this.transform.position.y, this.transform.position.z -
    4.             zaxis);
    5.     }
    Since I don't know what DownLevel is you may need to do more than this. Note that you never set DownLevel to anything in this script.
     
    jsull1 likes this.
  3. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    99
    Hey I'm sorry I should have taken this down when I found my issue, turns out there was a conflicting script that stated the axis was equal to transform.position.z so it wouldn't change for more than a frame. Thank you though!
     
    StickyHoneybuns likes this.
  4. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    99
    I dont see how to delete this thread haha