Friday, June 3, 2022
HomeGame Developmentphysics - How you can make an object transfer and rotate the...

physics – How you can make an object transfer and rotate the identical quantity as one other obejct


INTRO:

I’m making an attempt to make a system the place a participant can stroll round within a shifting inflexible physique with ease.

MY PROGRESS:

I’ve deliberate to attain this by getting having an occasion of the inflexible physique (spaceship) that’s static with a first-person controller within it. I’d then mother or father the digicam to the inflexible physique spaceship and make its rotation and site change if the primary individual controllers location and rotation change, I’ve tried utilizing mother or father scripts that equal the primary individual controller change in location and rotation however none of it really works, the digicam simply stands nonetheless when the ship strikes…

EDIT:

So that is the scene setup:
enter image description here

I’ve additionally used a number of completely different variations of the mother or father scripts that make the digicam a toddler of the participant (FPSPlayer) when this script is disabled the digicam simply strikes with the ship and its frozen (location clever not rotation-wise).

 public Rework TargetParent;

non-public Vector3 _localPosition;
non-public Vector3 _localEulerAngel;
non-public Vector3 _localScale;
non-public Vector3 _nextScale;

non-public Vector3 _lastPosition;
non-public Vector3 _lastEulerAngel;
non-public Vector3 _lastScale;


void Begin()
{
    UpdateLastTranform();
    _localPosition = TargetParent.InverseTransformPoint(rework.place);
    _localEulerAngel = rework.eulerAngles - TargetParent.eulerAngles;
    for (int i = 0; i < 3; i++)
    {
        _localScale[i] = rework.localScale[i] / TargetParent.localScale[i];
    }
    _nextScale = new Vector3(0, 0, 0);
}

void Replace()
{
    _localPosition += _lastPosition - rework.place;
    _localEulerAngel += _lastEulerAngel - rework.eulerAngles;
    for (int i = 0; i < 3; i++)
    {
        _nextScale[i] = _localScale[i] * (_lastScale[i] / rework.localScale[i]);
    }
    _localScale = _nextScale;

    rework.place = TargetParent.TransformPoint(_localPosition);
    rework.eulerAngles = TargetParent.eulerAngles + _localEulerAngel;
    for (int i = 0; i < 3; i++)
    {
        _nextScale[i] = TargetParent.localScale[i] * _localScale[i];
    }
    rework.localScale = _nextScale;
    UpdateLastTranform();
}

non-public void UpdateLastTranform()
{
    _lastPosition = rework.place;
    _lastEulerAngel = rework.eulerAngles;
    _lastScale = rework.localScale;
}

And in addition this script:

    public Rework Guardian;//Bear in mind to assign the mother or father rework 
non-public Vector3 pos, fw, up;

void Begin()
{
    pos = Guardian.rework.InverseTransformPoint(rework.place);
    fw = Guardian.rework.InverseTransformDirection(rework.ahead);
    up = Guardian.rework.InverseTransformDirection(rework.up);
}
void Replace()
{
    var newpos = Guardian.rework.TransformPoint(pos);
    var newfw = Guardian.rework.TransformDirection(fw);
    var newup = Guardian.rework.TransformDirection(up);
    var newrot = Quaternion.LookRotation(newfw, newup);
    rework.place = newpos;
    rework.rotation = newrot;
}

and in addition this one:

  public Rework FakeParent;

non-public Vector3 _positionOffset;
non-public Quaternion _rotationOffset;

non-public void Begin()
{
    if (FakeParent != null)
    {
        SetFakeParent(FakeParent);
    }
}

non-public void Replace()
{
    if (FakeParent == null)
        return;

    var targetPos = FakeParent.place - _positionOffset;
    var targetRot = FakeParent.localRotation * _rotationOffset;

    rework.place += RotatePointAroundPivot(targetPos, FakeParent.place, targetRot);
    rework.localRotation = targetRot;
}

public void SetFakeParent(Rework mother or father)
{
    //Offset vector
    _positionOffset += mother or father.place - rework.place;
    //Offset rotation
    _rotationOffset = Quaternion.Inverse(mother or father.localRotation * rework.localRotation);
    //Our faux mother or father
    FakeParent = mother or father;
}

public Vector3 RotatePointAroundPivot(Vector3 level, Vector3 pivot, Quaternion rotation)
{
    //Get a path from the pivot to the purpose
    Vector3 dir = level - pivot;
    //Rotate vector round pivot
    dir = rotation * dir;
    //Calc the rotated vector
    level = dir + pivot;
    //Return calculated vector
    return level;
}

CONCLUSION:

All I have to do is detect a transformational or rotational change within the participant controller and apply that to the digicam contained in the house ship.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments