Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Rotating parent + moving child issue

Discussion in 'Scripting' started by canuckvisa, Apr 13, 2021.

  1. canuckvisa

    canuckvisa

    Joined:
    Apr 26, 2020
    Posts:
    8
    Hello dear community, I am completely stuck with an issue.
    Please, watch a video to get a better understanding of what I am talking about:

    Code (classes related to the problem: AsteroidsSpawner, AsteroidsController, CoreController): https://github.com/Likeside/ExtinctionRunner

    The problem:
    Several gameobjects (asteroids) are children of core gameobject. They move towards another gameobject, which is also child of core gameobject (when it is not child of core gameobject, the problem persists, so I guess it does not matter).

    Input rotates the core gameobject
    Code (CSharp):
    1. void Rotate(float axis)
    2.         {
    3.             if (axis > 0)
    4.             {
    5.                 _coreView.transform.Rotate(_rotationAxis, _rotationSpeed*Time.deltaTime);
    6.             }
    7.  
    8.             if (axis < 0)
    9.             {
    10.                 _coreView.transform.Rotate(_rotationAxis, -(_rotationSpeed*Time.deltaTime));
    11.  
    12.             }
    13.         }
    FixedUpdate moves the rigidbodies of asteroids towards "_meteoritesTarget" gameobject.
    Code (CSharp):
    1. foreach (var asteroidRbSpeed in _listOfAsteroidsRbSpeed)
    2.             {
    3.                 Vector2 direction = (Vector2)_meteoritesTarget.transform.position - asteroidRbSpeed.Item1.position;
    4.                 direction.Normalize();
    5.                 asteroidRbSpeed.Item1.MovePosition(asteroidRbSpeed.Item1.position + direction * (Time.fixedDeltaTime*asteroidRbSpeed.Item2));
    6.             }
    When there is no input and no rotation of the core gameobject, asteroids move as expected. But when core gameobject rotates the slow down or completely freeze. I want them to move with a permanent speed.

    Asteroid has two components: Spriterenderer and Rigidbody2D, which is Kinematic. Freezing rotation on Rigidbody does not solve the issue.

    Thank you in advance for your help.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    It feels like the asteroids movement code just isn't being called when rotating.

    Put in some Debug.Log() calls and make sure it still is.

    ALSO: print out the value of Time.fixedDeltaTime and see if it goes to zero.

    Also, do you need to do this with fixed update? Are you relying on collisions to come from Physics?
     
  3. canuckvisa

    canuckvisa

    Joined:
    Apr 26, 2020
    Posts:
    8
    Thank you for a quick reply.
    Movement happens, not sure if it is slower or faster. I can see it with Debug.Log and inspector.
    Time.fixedDeltaTime stays at 0.02, it does not go to zero.

    I plan to add collision detection, so that asteroids will collide with the planet. Also, I am using fixed update because I have read that it is the better way to work with Rigidbodies.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    I would begin isolating parts of the code out to try and figure out what's missing.

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    Personally I would not use Rigidbodies for a game like this; I would just check my own collisions, either with distance or by overlap.
     
  5. canuckvisa

    canuckvisa

    Joined:
    Apr 26, 2020
    Posts:
    8
    Thank you for the advice, I will continue trying to solve the issue, I think, there might be a problem with calculating a direction of an asteroid during rotation.

    If nothing helps I will try to switch to moving with transform.translate.
     
  6. canuckvisa

    canuckvisa

    Joined:
    Apr 26, 2020
    Posts:
    8
    I have accidentally found a solution. Went to Edit > Project Settings > Physics2D > turned ON Auto Sync Transforms. Not sure how it works, though.