Search Unity

2D transform change not working

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

  1. jsull1

    jsull1

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