Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    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. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

2D transform change not working

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

  1. jsull1

    jsull1

    Joined:
    Apr 3, 2018
    Posts:
    98
    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:
    98
    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:
    98
    I dont see how to delete this thread haha