1
\$\begingroup\$

INTRO:

I am trying to make a camera move around in a fast moving, rotating in all axis, spaceship.... This person has achieved exactly what I need to achieve if that is a good reference for you (go to 5:30):

youtube.com/watch?v=eQt7TwCr6ao&ab_channel=TomWeiland

MY PROGRESS:

I have partially achieved this, by parenting the camera to the first person controller in a static ship and applying changes in transformations from the moving ship to the camera by using the following script. It works position-wise but rotation-wise the camera does not rotate around the ships origin when it rotates, or when I achieve this the camera doesnt move at all.

 public Transform TargetParent;

private Vector3 _localPosition;
private Vector3 _localEulerAngel;
private Vector3 _localScale;
private Vector3 _nextScale;

private Vector3 _lastPosition;
private Vector3 _lastEulerAngel;
private Vector3 _lastScale;


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

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

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

private void UpdateLastTranform()
{
    _lastPosition = transform.position;
    _lastEulerAngel = transform.eulerAngles;
    _lastScale = transform.localScale;
}

EDIT:

So this is the scene setup: enter image description here

CONCLUSION:

I need to make the camera be able to move and look around the rigidbody ship that can be rotating in all axis and moving at fast speeds in accordance with the static player controller

\$\endgroup\$
6
  • 1
    \$\begingroup\$ "I have tried using parent scripts that equal the first person controllers change in location and rotation" show us this code and explain how the output differs from what you expect. \$\endgroup\$
    – DMGregory
    Commented Jun 3, 2022 at 2:53
  • \$\begingroup\$ ok I have showed the scripts in an edit in my post, I was also trying to detect a change in rotation and location on the player controller so i could then appply that change locally to the camera? \$\endgroup\$
    – Object
    Commented Jun 3, 2022 at 5:24
  • \$\begingroup\$ also when the parent script is enabled the camera doesn't follow the ship. Instead, it just stands still and moves and rotates with the player controller. \$\endgroup\$
    – Object
    Commented Jun 3, 2022 at 5:29
  • \$\begingroup\$ Also this is very important... This person has achieved exactly what I need to achieve if that is a good reference for you (go to 5:30): youtube.com/watch?v=eQt7TwCr6ao&ab_channel=TomWeiland \$\endgroup\$
    – Object
    Commented Jun 3, 2022 at 6:24
  • \$\begingroup\$ So you have two copies of the spaceship, one that moves and one that doesn't move? This is not clear from the way you wrote your post. \$\endgroup\$
    – Kevin
    Commented Jun 4, 2022 at 0:58

1 Answer 1

1
\$\begingroup\$

If I understand correctly, you have two copies of your spaceship, one that is a moving Rigidbody and one that is stationary. You want the player to walk around the stationary ship, while the camera moves in the same way inside the Rigidbody ship.

Perhaps I'm not understanding something, but it seems like you're making this much more complicated than it needs to be. It seems like all you would need is this:

//attach to camera that is a child of the moving spaceship
public class TrackedCamera : MonoBehaviour {
    //the first-person controller should be a child of the stationary spaceship
    [SerializeField] private Transform firstPersonController;

    void Update() {
        //give us the same position and rotation in our parent as the first person controller has in its parent
        transform.localPosition = firstPersonController.localPosition;
        transform.localRotation = firstPersonController.localRotation;
    }
}

I'm not sure what you were trying to do with the scale, so I omitted that.

\$\endgroup\$
10
  • \$\begingroup\$ yes i am not using the scale, I edited my post so you can understand it better as well, I am testing out the script right now \$\endgroup\$
    – Object
    Commented Jun 4, 2022 at 1:28
  • \$\begingroup\$ The camera just teleports to the player controller... \$\endgroup\$
    – Object
    Commented Jun 4, 2022 at 1:41
  • \$\begingroup\$ @Object That means you didn't parent them correctly. The first person controller should be a child of the stationary spaceship, and the camera should be a child of the moving spaceship. \$\endgroup\$
    – Kevin
    Commented Jun 4, 2022 at 1:53
  • \$\begingroup\$ I am pretty sure i did that correctly?: pasteboard.co/F0FatQQDENgM.png \$\endgroup\$
    – Object
    Commented Jun 4, 2022 at 1:58
  • 1
    \$\begingroup\$ @Object: Try this: drag two new copies of the ship mesh into the scene - not prefabs or anything like that, just the mesh. Without making any other changes, make the camera a child of one, and the first person controller a child of the other. Then start the scene. \$\endgroup\$
    – Kevin
    Commented Jun 4, 2022 at 2:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .