Camera Controller Problems...

Hey guys! So I have the following code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class CameraController : MonoBehaviour {
    public Transform target;

    public Vector3 offset;
    public float zoomSpeed = 5f;
    public float minZoom = 7f;
    public float maxZoom = 10f;

    public float pitch = 2f;

    private float currentZoom = 10f;
  
    private Vector3 newPosition;
  
    void Start() {
        transform.position = target.position - offset * currentZoom;
    }
  
    void Update() {
        currentZoom -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
        currentZoom = Mathf.Clamp(currentZoom, minZoom, maxZoom);
    }

    void LateUpdate() {
        transform.LookAt(target.position + Vector3.up * pitch);
      
        if(Input.GetKey("mouse 1")) {
            transform.position += new Vector3 (Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"));
        }
    }
}

So, obviously, I’m trying to make the camera rotate around the ā€œtargetā€, however, I’m having problems with setting the new Z position:

        ...if(Input.GetKey("mouse 1")) {
            transform.position += new Vector3 (Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"));
        }...

At least, I think that’s the problem. Here’s a video of what happens when I test the script out:

As you can see, for some reason I can’t ROTATE around the player. I simply move the camera on one plane (as it seems).

However, when I go up and down with my mouse, it zooms in and out, instead of actually ROTATING around the player.

Can someone please help me? I’ve been stumped on this for a couple days now and I can’t seem, for the life of me, to get this right.

Also; I know I could’ve just searched online, but I have. Everything that comes up is scripts of other people’s, and I don’t want the solution handed to me on a silver platter, I want to learn how I need to fix this, and what’s causing the issue, as I’m new to C#/Unity.

Thank you <3

So you’re definitely not thinking of the position correctly.

Consider line 34. Ignore the y-coordinate of the new Vector3 for a second and look at just the x and z. You’re adding to x and z equally each frame. This means your position is only going to be moving in a straight line.

If you think of the position of the camera as a circle you need three things: the position of the target, the distance between the target (the radius), and the angle of where it is on the circle.

Knowing these three things, you can get x and z values that will let you go around your target.

I’d suggest a 3-part camera.
part 1 is an empty game object. ā€œThe Rigā€
part 2 is an empty game object. ā€œThe Pivotā€
part 3 is the camera itself.

The rig adjusts it’s x-axis for rotation. The pivot adjusts its local y-axis for rotation (local part is important).
camera just sits there. (Though, you could zoom in on the local z-axis, too).

Something simple to setup is give the pivot a small height, like 1 or 1.5 on the y position.
give the camera maybe something like -5 for the z-position.

Then, let your code do: rig.transform.position = player.position (this would be every update, fixedupdate or late update; if you use physics, I’d recommend fixed update, but otherwise late update I think would be best).

The rest of the rotation and such can be handled by modifying the rotation of the rig or pivot.
You don’t really need to move the camera, as you’re trying, because the objects (3 parts) setup that way, plus rotation (and the camera’s z-axis of course) will all work well together.

Hope that helps :slight_smile:

Holy fuck, I’m not sure I follow completely on either of those posts… It seems really complicated.

I understand the first post, as I could get the radius, position of target, and angle… But I don’t know how I could go about doing so.

Since I’m a new Unity coder, I’d rather help with links to the unity coding documentations if it’s possible… Because I simply don’t understand how I’d input the radius, etc. in order for it to work.

What was complicated in my post? :slight_smile: Perhaps I can explain… heh.

I’m not sure how to explain… I simply don’t understand.

I mean… Where does the rig/pivot go? I know you said give it 1 on the y-position, but where do I set it on others?

Also, I don’t understand how this would make the camera rotate around ā€œtargetā€ā€¦ I know some script must be involved, but I don’t know how I’d do it with the system you suggested.

@methos5k setup is very common. Your camera hierarchy looks like this:

Rig
 -Pivot
  - Actual Camera

You set the position and rotation of the Rig to the same position of the object you want to follow. You move the Pivot’s local position to act as your offset, in this case, you want to ā€œzoom outā€ by pulling the Pivot’s z position back. Finally, you manipulate the Actual Camera for any sort of local events like screen shake.

Build the hiearchy and play around with the X/Y/Z positions and rotations in the inspector, no code, and everything will become clear.

Alright, but how would I make it rotate when I use mouse 1?

Also, to be clear I want it to rotate AROUND ā€œtargetā€ (the player)

Yes, we know you want it to go around :slight_smile:

Did you try it just in the inspector to see how it would work?

Mouse 1 = right button? Modify the x-axis rotation of the Rig. Are you saying you’re not sure how to code any of this or ?

@GroZZleR 's explanation is about 92% the same as mine :wink:

I just really like it, and I tried a ton of stuff as I was learning/failing lol :slight_smile:

I’m not degrading the method, or you. I also understood what you meant, but I didn’t realize that if you just modify the x axis then it would go around… lol… don’t make fun of me, I’m new.

I’ll try it real quick and see if it works. Thanks man

EDIT: I’m saying I barely know how to code this, yes. As I’ve stated before, I’m very new.

Oh, no worries… I didn’t think you were degrading the method or me :slight_smile:
I just wanted to confirm that we knew you wanted it to go around lol

I wouldn’t make fun of you :wink:

1 Like

How would I go about editing just the x axis through scripting?

I know I could do new Vector3, but I don’t want to edit the y and z…

Also, I’ve tried transform.position.x = yada yada, but an error occurred

first of all, you don’t want to move the position for the rotation :slight_smile:

Do you want to be holding down the right mouse button and drag the mouse to rotate?

Hmm, I see… Yeah, I want to hold the right mouse button and drag to rotate

You can try something like this … Just put it on the Rig part of the camera setup:

void LateUpdate() {
if(Input.GetMouseButton(1)) {
   float x = Input.GetAxis("Mouse X");
    // just pick a number for the sensitivity, or expose it in the inspector so you can edit it to see what you like.
   transform.rotation *= Quaternion.Euler(new Vector3(0, x * mouseSensitivity, 0));
  }
transform.position = player.position;
}

If you wanted the ā€˜y’ it could be like:

// just put this inside the if mouse statement above.
// though, pivot angle is a float and declared outside of the Update() method.
float y = Input.GetAxis("Mouse Y");
y *= -1;
pivotAngle += y * mouseSensitivity;
pivotAngle = Mathf.Clamp(pivotAngle, -45, 75);
 // camPivot is a reference to the Pivot (first child of the Rig).
camPivot.localRotation = Quaternion.Euler(pivotAngle, 0, 0);

Thanks a bunch mate! I tweaked the script a bit:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class CameraController : MonoBehaviour {
   public Transform player;
   public Transform camPivot;

   public float mouseSensitivity = 5f;

   private float pivotAngle = 0f;

   void LateUpdate() {
       if(Input.GetMouseButton(1)) {
           transform.rotation *= Quaternion.Euler(new Vector3(Input.GetAxis("Mouse Y") * mouseSensitivity, Input.GetAxis("Mouse X") * mouseSensitivity, 0));
       }

       transform.position = player.position;
   }
}

However, it’s still not quite what I want. I’m trying to get something similar to the world of warcraft camera, if you know how it works.

Here’s a video of the camera as of now:

https://dl.dropboxusercontent.com/s/7gfgdle8ic7zfoj/2018-01-08_23-15-09.mp4

Well, I wouldn’t ā€˜tweak’ it that way, personally, but to each their own.
The pivot would be like the camera goes up (and looks down) or vice versa… and on a different game object & local rotation.

Anyways, I do not know the game, but I will watch a clip and see how it goes.

Oh dear, I misread that. I thought you were posting a video for the camera in World of Warcraft ROFL.

Yes, because you ā€˜tweaked’ my code, you removed the clamping and setup lol.
now the camera is flipping over n such… that’s not good :wink:

Ah, one thing… thanks to your video, i saw your hierarchy, and I would suggest that you unparent the camera rig from your player. :slight_smile: That’s not necessary.