Rotating around a fixed pivot point

Hi people,

I am trying to move the camera around its y-axis but I couldn’t manage to find how to make that happen. When I drag the y component of the transform of the camera in the inspector, ıt turns pivoting around the y axis which is what I try to achieve. But when I try to do it by code transform.Rotate or transform.Rotatearound the pivot point shifts.
Any ideas?

1 Like

When you rotate an object with the Transform component, it rotates based on the world axis, but when using Transform.Rotate it will rotate on the local axis, meaning that if your camera is even slightly tilted the behavior will not be the expected, for this, you have to set the rotation relative to the world space, rather than the local.

 transform.Rotate(Vector3.up, Space.World);

I couldn’t manage to achieve what I wanted or maybe I am doing something wrong. I am at Top down space shooter section of the 2d course and I thought maybe I can do something different. So I created a 3d hollow cylinder with a larger height than the camera. Then I placed the camera at the center of the cylinder and placed a texture on the inner wall of the cylinder. Now I want to modify the Rotate -y- the value of the camera when I press a button so that camera turns amongst the inner wall of the cylinder to create a bank effect.
I thought that ı can make the player plane child of the camera when the bank button pressed and turn the image accordingly so that the player plane image would travel with the camera and tilt the plane image so that image wouldn’t be distorted. As I mentioned above when I drag the value in the inspector, the camera turns on itself, on the y axis, fixed on the origin, and that I want to simulate with a press of a button.

I created this script and attached it to the main camera. (I also tried creating an empty game object as the parent of the main camera and tried turning that but I couldn’t manage to achieve what I try that way too)

public class CameraMover : MonoBehaviour
{

    float cameraAngle = 0f;
    public Transform pivotPoint;
    public GameObject player;
    Player playEr;
    Coroutine timerCoroutine;
    Camera mainCamera;





   
    // Start is called before the first frame update
    void Start()
    {

        playEr = FindObjectOfType<Player>();
        mainCamera = FindObjectOfType<Camera>();
        
    }

    // Update is called once per frame
    void Update()
    {
        TurnCamera();
    }

    private void TurnCamera()
    {
        if (Input.GetKey(KeyCode.Q))
        {

            timerCoroutine=StartCoroutine(CameraTurnTimer());
           
           


             transform.Rotate(Vector3.up,cameraAngle,Space.World);
           
            mainCamera.transform.SetParent(player.transform);
            
        

             player.transform.rotation = Quaternion.Euler(0, transform.eulerAngles.y + cameraAngle, 0);
        }
         if (Input.GetKeyUp(KeyCode.Q))
        {
            StopCoroutine(timerCoroutine);
            Debug.Log("1");
            mainCamera.transform.parent = null;
            Debug.Log("2");
            cameraAngle = 0;
            Debug.Log("3");
            //playEr.SetUpMoveBoundries();
           

        }
            

        
    }

    IEnumerator CameraTurnTimer()
    {
        while (true)
        {
            cameraAngle = (cameraAngle + 3)*Time.deltaTime;
            yield return new WaitForSeconds(1f);
        }
    }
   
}

I’m sorry, I don’t really understand what you are trying to do, a couple of images would be great. What do you mean by bank effect?

Why are you childing the camera to another object and then rotate that object? That’s gonna change the pivot point, let me show you what I mean with the following gifs, imagine the green cube is the camera:

This is what your code is doing, since you are rotating the parent object the camera will also rotate:
ezgif-2-d9ff59c1b03f

According to what you are saying you want this:
ezgif-2-8261e2316549

You just need to erase those lines that parent the camera to the player. But of course, I might be quite wrong, so further explanation on your part, with images, would be great to help you further.

Hey man, thanks for the quick answer and sorry for the bad explanation…=)…I am trying to make a top-down plane shooter with a fixed camera. Fixed in the sense of the camera will not follow the player. The player can move inside the frames of the camera. So to achieve the sense of continuous forward movement, an image, placed on a large enough quad, is scrolling on the negative y-axis.

ezgif.com-gif-maker

This is already what was shown on the Top-Down Shooter section of the course. What I thought was that if I put a hollow cylinder instead of a quad and place the camera on the center, I could turn the camera and add a little animation to the player plane sprite to create a sense of bank/tilt motion. Kind of like a hand of a watch. The camera itself sits on the center of the watch, the length of the hand represents the vision of the camera, and the player image sits at the end of the hand. So when a button is pushed, the hand starts to turn and when the button released, stops.

That’s why I added the player sprite as the child of the camera since I was turning the camera, but I also need to rotate the image on itself so that it wouldn’t look squished.

Now the camera turns as I wanted when I got rid of the player movement line. The challenge now for me to figure out how to turn the player image with the camera and tilt the sprite so it wouldn’t look squished.

Have you tried rotating the cylinder instead?

Yes, at first I did that. But then when there are enemies on the screen, they would still be on the screen when the player banks. I thought that If I move the camera, that way the enemy would shift out of the camera and that would be cool…=)

But now that I wrote to you in detail, I think that ı need to calculate the translation of the player sprite vector according to the angel of the camera and then tilt the sprite so that it would look the same. I don’t even know if it works gameplay-wise.

It can definitely work, but it will require a lot of work, I’m not truly concerned about the effect, that’s somewhat simple to achieve, but about the enemies, specially if you are doing a 2D game, rotating won’t actually prevent the collisions with other enemies, so you’ll either have to adjust everything to work on a 3D setting or find a way to make this work on a truly 2D environment.

The way to make it work in 2D, I think, would be to rotate the cylinder and then move to the side all the enemies, this can be easily achieved if you parent them to an object, and just move that object to the side.

Let me give you an example, take a look at this:

ezgif-3-0afa894b1de5

In the image above there are four cubes; the ones that are closer to the camera are rotated by 90 degrees on the Y axis, the ones further have a rotation of (0,0,0). The 2 red cubes have a Rigidbody2D and a collider, the green ones only a collider.

You can clearly see that if you rotate an object that is using 2D physics, the collisions will behave weirdly. That means that rotating the camera alongside the player will work to achieve the effect you are looking for, but also means that colliders will change in size. Also take a look at this other gif:

ezgif-3-1eb88294ccbd

Here we have a very similar situation, the red square has a collider2D as so does the green square, you can see the red cube stops even tho they are not truly touching each other, at least not in the Z axis, that’s because the Z axis doesn’t exists in 2D, meaning they will collide regardless, again, that will cause issues with the rotation of the camera, the player will collide with enemies that aren’t even in sight.

You’ll have to figure out how to achive your effect taking this issues into consideration.

Dude, first of all, thanks for your interest and really detailed answers, you are awesome…=)
Secondly, you are right, I didn’t even think about the colliders 'couse I was a bit excited about that I thought of the cylinder. I’ll probably try to implement moving the cylinder and shifting the enemies accordingly. Thanks again …Cheers…=)

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms