In my game, the player's movement script and the camera's movement script are separate.
Both of them use "W", "A", "S", and "D" to move. The camera can also use "Q" and "E" to rotate around the player.

transform.RotateAround(player.transform.position, Vector3.up, 2f);   //or -2f

The player can use a skill where the animation and the movement are separate. For this, I use the following code to move:

public IEnumerator movement() {
    for(int i = 0; i < 66; i++) {        //animation length is 0.67second
        yield return new WaitForSeconds(0.01f);
        controller.Move(this.transform.forward * 0.03f);    

When it moves, the camera should also move along with the player, which I have tried:

public void followplayerskill() {
    float tempxoffset=xoffset;
    float tempyoffset=yoffset;
    float tempzoffset=zoffset;

    this.transform.position = player.transform.position + new Vector3(tempxoffset, tempyoffset, tempzoffset);

It works if the camera does not rotate,
but once I press "Q" or "E" to rotate the camera before using the skills, the camera does not move as I would expect, which is shown in this video:
how can I correct it so that the camera can move correctly?

  • \$\begingroup\$ Where is your camera? Have you traced where the camera is and where the player is (along with its rotation). In your video, it does not show what you're trying to achieve. Where are those x|y|zoffset coming from? \$\endgroup\$ – Vaillancourt Feb 12 '14 at 11:26
  • \$\begingroup\$ my camera is not a child of the player, it is a separate gameobject. In the video, i try to use the skill(jump forward) and the video successfully follow the character for the first few try, but once i rotate the camera and try again, the camera moved to the unexpected position. the x,y,z offset is the distance on the character and the player position. \$\endgroup\$ – brian661 Feb 12 '14 at 12:49
  • \$\begingroup\$ Your followplayerskill does not appear to take into account the rotation of the player. Since we don't know how you're setting your offsets, we can't know what you're doing wrong. \$\endgroup\$ – MichaelHouse Feb 12 '14 at 15:34
  • \$\begingroup\$ sorry some mistake in the code, the x,y,z,offset should be tempx,y,z,offset, which are calculated above \$\endgroup\$ – brian661 Feb 13 '14 at 7:51

From what I can fetch of what you've said, you're using unity with a Transform object which contains a rotate and a translate.

I'm not familiar with unity but here goes:

The documentation does not state what happens if you set a rotate and a translate on the same transform. But you could specify explicitly what to do by adding children transforms: first set the translation as the parent, then the rotation as the children transform.

You have to make sure that the rotation is always made after the translation.

  • 1
    \$\begingroup\$ This one probably isn't relevant to unity. Unity rotations are always about the transform's center (local 0,0,0 coordinate) completely independent of translation. Setting position translates the object in worldspace, completely independent of rotation. \$\endgroup\$ – DMGregory Apr 14 '14 at 12:57

It looks like your x,y,zoffset variables are set up for the camera in its default position (say, "South" of the player), and don't update when the camera is turned (say, to put the camera "West" of the player) - so when you perform the skill, the camera teleports back to its initial ("South") offset, which puts the player off-screen to its left.

I may be misreading, but it looks like for everything other than the skill, you're tracking positions for the camera and player independently, counting on the fact that moving them by the same offsets should keep them in synch.

The trouble with this approach is that any glitch in your code, rounding error, or unexpected physics interaction, can cause the two to diverge from your desired relative positions, and they have no means to correct this divergence.

A better approach is often to have the camera keep track of a desired offset from the player (often in spherical/cylindrical coordinates) and in each timestep re-calculate its position using the player's (possibly with some smoothing applied).

Your rotation/zoom functions then never actually move the camera - they just update the relative offset coordinates, which automatically gets picked up when the camera is positioned.

Here's a crude example - not best practice by any means, but it should prevent the camera from coming unglued from the character at any time:

GameCamera.RotateCamera(float angularRate)
   cameraYaw += angularRate * Time.deltaTime;

 // moving the camera in "Update" in Unity can cause judder when combined with certain smoothing techniques.
   // Compute world offset vector from relative offset in cylindrical coordinates:
   Vector3 cameraOffset = Quaternion.AngleAxis(cameraYaw, Vector3.up) * Vector3.back;
   cameraOffset *= followDistance;
   cameraOffset += Vector3.up * cameraHeight;

   // You can lerp from the old position to the new to make the camera smoother.
   transform.position = playerAvatar.transform.position + cameraOffset;

This shouldn't need any special handling for the player's skill - wherever the player goes, the camera will follow.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.