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

RotateAround - Multiple Objects, rotating while moving.

Discussion in 'Scripting' started by Darhkuan, Nov 27, 2014.

  1. Darhkuan

    Darhkuan

    Joined:
    Feb 24, 2014
    Posts:
    24
    Hi all,

    I've spent the last couple of days trying to get this and I'm not sure if I'm overlooking something really simple or just going about it incorrectly.

    I have a number of objects that are grouped together (think units in a regiment, each unit is its own gameobject).

    I have a script which finds the middle point of this regiment, and then on right click rotates all of these objects around that middle point. So far so good (or so it seems).

    The problem comes when I add in the fact that the whole regiment follows the mouse until it is "placed" - so if I right click while this is happening I want it to be able to both rotate and remember the rotation.
    Unit placement is done via mouse co-ords from a raycast and adjusted based on the middle point I spoke of earlier. This has the habit of ignoring the rotate around and just resetting it to 0.

    I've experimented a lot with finding a relative point of the unit
    [
    Code (CSharp):
    1. transform.RotateAround(_rotationPoint, Vector3.up, _rotateVelocity * Time.deltaTime);
    2.         _currentRotation = _rotationPoint - transform.position;
    ]

    this is the rotatearound script, which then stores its relative position from the current middle point of the regiment.

    Then when the mouse moves we get a value from the ray, and re-draw the units out into their positions and then finally add the relative value from the stored part (so ideally we get a well spaced out group of units/gameobjects facing another direction).

    Code (CSharp):
    1.  if (_currentRotation+ newLocationSpot != transform.position)
    2.             {
    3.                 newLocationSpot.x = newLocationSpot.x + _currentRotation.x;
    4.                 newLocationSpot.z = newLocationSpot.z + _currentRotation.z;
    5.             }
    the result is mainly a random spacing between the units (from being piled on top of each other, to having double the space of normal), jerky rotation and inaccurate rotation.

    I can post more code if needed, but thought i'd describe it first and see if someone had a novel idea/solution that I was missing!
     
  2. Darhkuan

    Darhkuan

    Joined:
    Feb 24, 2014
    Posts:
    24
    Any ideas guys? Shameless bump of the thread!
     
  3. Darhkuan

    Darhkuan

    Joined:
    Feb 24, 2014
    Posts:
    24
    so having done some more investigation it seems to be an issue relating to HOW we rotate things.
    In using
    Code (CSharp):
    1. transform.RotateAround(_rotationPoint,new Vector3(0,90,0), _rotateVelocity * Time.deltaTime);
    it appears at certain points it gets confused and starts flipping angles by 180 (so if the rotate around is on a circle, all of a sudden the objects appear on the opposite point of the circle), and thus also messing up some of the positioning.

    I understand this can be a problem with Euler angles & gimble lock (I believe) - but I thought Rotate around was meant to solve this as it only deals with Quaternions which don't have ambiguous references for different points within a rotation?

    Thoroughly confused now ....