Search Unity

Question 2D change size transform.localScale cause rotation in 3d

Discussion in '2D' started by zalntarantu, Oct 10, 2021.

  1. zalntarantu

    zalntarantu

    Joined:
    Jul 18, 2021
    Posts:
    2
    We have:
    - png of Player dragged to scene with rigidbody2d dynamic and collider2d.
    - png of Egg dragged to scene with rigidbody2d dynamic and collider2d trigger. Tagged in inspector.

    Object Player has size changing script (when player collide with tagged object - player grows, object destroys):
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class FeedChangeSize : MonoBehaviour
    4. {
    5.     public float grow;
    6.     public float shrink;
    7.  
    8.  
    9.     private void OnTriggerEnter2D(Collider2D collision) {
    10.         if (collision.gameObject.tag == "canCola")//
    11.         {
    12.             transform.localScale += new Vector3(grow, grow,0);
    13.             GetComponent<Rigidbody2D>().mass += 0.9f;//
    14.             GetComponent<PlayerControls2D>().jumpForce += 0.5f;//
    15.             Destroy(collision.gameObject);
    16.  
    17.         }
    18.  
    19.         if (collision.gameObject.tag == "Egg_crab_0")
    20.         {
    21.             transform.localScale += new Vector3(grow, grow,0);
    22.             GetComponent<Rigidbody2D>().mass += 1.8f;
    23.             GetComponent<PlayerControls2D>().jumpForce += 0.7f;
    24.             Destroy(collision.gameObject);
    25.         }
    26.     }
    27. }
    Problem- Player rotate a bit around Y axis in 3D (remember its a 2D game, z rotation is locked) after every collision with tagged object.
    Video of rotation https://vk.com/video49473556_456239420
     
    Last edited: Oct 11, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Lock any Rigidbody2D rotations that you don't want to change, otherwise the physics system will spin things if it finds a net torque applied to them from a collision.

    Also, I'm not sure changing the scale like that will reliably trigger collisions. You may need to call .MovePosition() with a zero vector to notify the physics system.

    This is because with Physics (or Physics2D), never move colliders by the transform directly. You are bypassing the physics system.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) to move things.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    The physics system has 3 degrees of motion only: X & Y position and Z rotation. It can only change these.

    I don't see evidence of Y-axis rotation, only what might look like it and here you're messing with the scale so no idea but if there is indeed a rotation in the Y axis then it'll be in your code. I'm surprised you didn't show something useful like the Transform itself just a video taken with a phone.

    I am not sure why you're doing all that mass manipulation but you might want to look into using AutoMass where the physics system calculates the mass from the area of the collider for you based upon a fixed density you can specify. This is actually how the 2D physics wants to work by default (Box2D) but for historic reasons, Unity always explicitly specified mass.
     
  4. zalntarantu

    zalntarantu

    Joined:
    Jul 18, 2021
    Posts:
    2
    Yes, you are right. In my Controls script Scale sets to -X to turn picture of player when it goes left and in Grow script grow var is +, so when player goes left we get -X+grow.