Search Unity

Player Starts Rotating After Using Setparent On Another Object

Discussion in 'Scripting' started by jcarellanov, Apr 14, 2019.

  1. jcarellanov

    jcarellanov

    Joined:
    Jan 20, 2019
    Posts:
    4
    Hi, I finished one of the 2d video tutorials and now I'm playing around on my own. My current goal is to be able to move some colored balls into the center once I've touched them with the UFO. I move the UFO with a joystick. The balls spawn on the screen with random positioning and velocity, I've made them dynamic rigidbodies2d so they can bounce of the walls. Once I touch them with the ufo, the idea is to be able to carry them. I did it like this (balls are tagged as Stone1, 2 etc.)


    private void OnCollisionEnter2D(Collision2D other)
    {

    if (other.gameObject.tag.ToString()=="Stone1")
    {
    other.gameObject.GetComponent<Rigidbody2D>().isKinematic = true;
    other.gameObject.transform.SetParent(transform,true);

    Debug.Log("Touched Stone1");
    }


    }


    This somewhat works since it makes the ball stick to the UFO, but after moving it with the joystick the ufo starts rotating for some reasong, which makes movement erratic.

    Thanks
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    That's weird, if they're set to kinematic they shouldn't be interfering with the UFO. How is the UFO gameobject setup and how do you move it?
     
    jcarellanov likes this.
  3. jcarellanov

    jcarellanov

    Joined:
    Jan 20, 2019
    Posts:
    4
    Here is the code for UFO (PlayerController), at start I do this:

    rb2d = GetComponent<Rigidbody2D>();

    and then in Fixed Update

    private void FixedUpdate() // with joystick
    {

    horizontalMove = FixedJoystick.Horizontal * speed;
    verticalMove = FixedJoystick.Vertical * speed;
    Vector2 movement = new Vector2(horizontalMove, verticalMove);
    // rb2d.AddForce(movement);
    rb2d.velocity = movement;
    }


    commenting out the line that makes Stones kinematic does not appear to make a difference
     

    Attached Files:

  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Weird!

    Maybe try setting simulated to false instead of turning kinematic on, that should disable all physics interactions:
    Code (csharp):
    1.  
    2. other.gameObject.GetComponent<Rigidbody2D>().simulated = false;
    3.  
    It's also possible the centerOfMass of your UFO is changing after parenting, so try resetting that to 0,0 after parenting the ball as well if the above doesn't work.
     
    jcarellanov likes this.
  5. jcarellanov

    jcarellanov

    Joined:
    Jan 20, 2019
    Posts:
    4
    So first I tried setting the simulation off, which made the rotation more stable (always counter clockwise). Then I tried

    rb2d.fixedAngle = true


    and this combination seems to work! The rotation is gone. One thing that is weird is that if I comment setting the simulation to false like this


    //other.gameObject.GetComponent<Rigidbody2D>().simulated = false;
    other.gameObject.transform.SetParent(transform,true);
    rb2d.fixedAngle = true;


    the ball bounces off the UFO and does not stick.

    So at this point now my goal is to make the balls disappear once they reach the sink in the center. This is my sink script:

    private void OnCollisionEnter2D(Collision2D other)
    {
    //Color myColor = gameObject.GetComponent<SpriteRenderer>().color;
    //Color otherColor = other.gameObject.GetComponent<SpriteRenderer>().color;
    Debug.Log("Sink Collision Detected with " + other.gameObject.tag.ToString());
    // Debug.Log( "myColor is " + myColor.ToString() );
    //Debug.Log("Ball is " + otherColor.ToString());


    if (other.gameObject.tag.ToString() == "Stone1")
    {
    // other.gameObject.transform.parent = null;
    other.gameObject.SetActive(false);
    Debug.Log("Color Match");
    }
    }


    However, the sink collision with the ball is not getting detected, but it does detect collisions with the ufo??

    Thanks!
     

    Attached Files:

  6. jcarellanov

    jcarellanov

    Joined:
    Jan 20, 2019
    Posts:
    4
    Oh, it's because setting the simulation to false will do deactivate the colliders...