Discussing your follow camera

How did you find the implementation of your follow camera? Easy, or did you have troubles with it?

this could have been a really good place, to introduce cinemachine, and use it to create a simple follow camera, rather than coding a sort of primitive short camera, just thought, i’d throw this small piece of feedback here. but, not that it matters much, we’re intermediate we should be able to handle some little extra code :smiley:

Great point about Cinemachine. I do plan to do a whole section on Cinemachine, cut scenes, etc later on… we could also talk about using Cinemachine to drive the main follow camera. For now we wanted to keep it simple to keep the momentum going and not dig too deep into a new system.

2 Likes

I was stumped by the wording of the challenge. I implemented Camera.main.transform.position when I should have just done transform.position. But overall it was an easy video to digest and I am confident with how I am tackling these challenges.

1 Like

Do you plan to teach Cinemachine?:smiley:

Yep!

1 Like

I have probably missed something but when clicking on follow camera… i do not have the target field in the inspector. I am going to check my code now but thought I would check here in case someone else had same issue. UPDATE - checked my code and realised that I hadn’t hit Save on my Visual Code. (it was as simple as that). Leaving this comment here as others may have the same issue :confused:

Its great when it’s a simple fix like that!

In one of my games i used empty game object as a camera desired position.
Then i put simple several lines code script on camera.
transform.position = Vector3.Lerp(transform.position, desiredPosition, followSpeed * Time.deltaTime);
Very simple and fast.

Damn, and here I was doing all this code. Thank you, I did not know that the vector3 itself had a lerp!

1 Like

is there a way i could use my mouses right click to be able to rotate the camera around the player? the click to move is a cool, but id like to be able to still allow my player to move the camera around a little bit more.
For example, can i take in MouseButtonDown(1) as an input and rotate the camera on… i think that would be the Y axis so the camera can look around? maybe with target.transform.rotation, or something on those lines

edit: i also think it would be really cool to use the mouse wheel to zoom in and out with the camera, would that be possible?

With a standard camera, it’s a matter of creating a separate game object that follows the player, with the camera childed to it, and then rotating the follow object by some value calculated by the mouse movement.
With the Cinecamera, it gets a bit more complicated. You can experiment with the Aim component in the Cinebrain, but so far, I’ve had no luck with making that look right.

Yep its easyest way. Make a transform that will be pivot point for camera, and put camera inside. Then use something like (not sure)
if(Input.GetMouseButton(1))
{Quaternion angle = pivot.transform.rotation;
angle.eulerAngles += Vector3.up * Input.GetAxis(“Mouse X”) * sensitivityX * Time.deltaTime;
pivot.transform.rotation = angle;}

oooh sorry i didnt see your response, i will have to try this out

How to add a camera zoom in and out ?

After some research i managed to make a camera zoom in and out with this script

if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
    //GetComponent<Camera>().fieldOfView--;
    GetComponent<Transform>().position = new Vector3(transform.position.x + .2f, transform.position.y - .3f, transform.position.z);
    transform.Rotate(-2, 0, 0);
}else if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
    //GetComponent<Camera>().fieldOfView++;
    GetComponent<Transform>().position = new Vector3(transform.position.x - .2f, transform.position.y + .3f, transform.position.z);
    transform.Rotate(2, 0, 0);
}

but i couldn’t make a limit to the camera zoom that it stops at certain distance ether out or in
and how can i make the zoom animated like smoothly.

This will work well for a standard camera. Bear in mind that this script will not work once we switch to the Cinemachine due to the way the Cinemachine brain takes control of the camera.
For limits, you’ll need to create some global variables with the min and max values for x, y, and the rotation.
The trick is instead of changing transform.position, you’ll need to deal with transform.localPosition so that you can compare the ranges. localPosition is calculated relative to the parent gameobject.

1 Like

For camera following management I used the FreeLookCameraRig prefab in the Standard Asset and modified the script to manage the vision around the player using the right mouse button… It’s not so bad.

I wrote my own (with the help of Youtube) following camera.
I am planning to use it on Android touchscreen.

[SerializeField] bool debug = false;
    [SerializeField] Transform target;
    [Range(1, 100)] [SerializeField] float smoothness = 1f;
    [SerializeField] Vector3 forwardOffset;
    [SerializeField] Vector3 backwardOffset;
    private Vector3 offset;

    private void Start()
    {
        offset = forwardOffset;
    }
    private void Update()
    {
        if (!debug)
        {
            if (Input.GetMouseButton(0))
            {
                Vector3 mPos = Input.mousePosition;

                if (offset == forwardOffset && mPos.y < Screen.height / 6)
                {
                    offset = backwardOffset;
                }
                else if (offset == backwardOffset && mPos.y > Screen.height / 2)
                {
                    offset = forwardOffset;
                }
            }
            Move();
        }
    }

    private void Move()
    {
        Vector3 desiredPos = target.position + offset;
        Vector3 smoothPos = Vector3.Lerp(transform.position, desiredPos, 1 / smoothness);
        transform.position = smoothPos;
    }
1 Like

I’m assuming later on in the course we’ll talk about implementing some kind of click and hold feature? That is, instead of spamming left click click click.

I have returned from 3 hours in the future. The answer is yes, in a few lectures we cover it for anyone else that stumbles across this on your journey. :smiley:

1 Like