Trying to set up a 3rd person view camera

I am new to unity and writing script. I am trying to figure out how I can make a 3rd person view camera able to scroll away from the player and back in before transitioning to first person. I have figured out how to scroll between first and 3rd person and even to scroll further away from my character by one instance. the issue I am running into is I want to be able to scroll away from the player up to a point before it stops and then scroll back up to the player to a point before it transitions back to first person.

The biggest help would be if someone knew how I could do something like

if (localposition = x, y, z)
{
dosomething
}

Never compare floating point values for equality due floating point imprecision. You can ask greater than less than though.

For what you want, you would control a linear scalar value that the camera rig would use for “how far back I am.”

That value would be used to position the camera, and you would control that value, not the camera position.

So lets say it goes from 1m to 5m behind the player: you would change it and clamp it to that.

BTW, camera stuff is pretty tricky… it might be best to use Cinemachine from the Unity Package Manager.

You almost have it, just check the distance from the camera position to the player position (Vector3.distance) and if it’s greater than (value you want), do something.

haven’t been able to figure that part out yet.

I keep getting errors at every turn.

this is what I have right now

public class CameraSwitch : MonoBehaviour
{
public GameObject ThirdPersonView;
public GameObject FirstPersonView;

void Update()
{

if (Input.GetAxis(“Mouse ScrollWheel”) < 0)
{
ThirdPersonView.SetActive(true);
FirstPersonView.SetActive(false);
}

if (gameObject.activeSelf == true)
{
if (Input.GetAxis(“Mouse ScrollWheel”) < 0)
{
ThirdPersonView.transform.position = transform.TransformPoint(new Vector3(0, 2, -4));
}

}

if (Input.GetAxis(“Mouse ScrollWheel”) > 0)
{
ThirdPersonView.SetActive(false);
FirstPersonView.SetActive(true);
}

{ }
}
}

so I want to be able to scroll out a few more instances and then back in all the way before moving back into first person. on the middle 3 lines I am able to scroll out once right now and I want to be able to put additional points further back. However at the moment as you might be able to tell once I scroll out once I am now stuck with the camera at that position and cant scroll it back in. but i can switch back and forth between 1st and 3rd person still.

I will have to do more research into this option as 85% of what you just said went right over my head. I understand the concept but have 0 clue on execution.

As I noted,

So you are starting with some of the HARDEST CODE to get right. You might want to start with simpler game stuff and leave Cinemachine to manage your camera.

Up to you though.

ALSO, if you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

well if its the some of the hardest code to get right that makes me want to figure it out more lol. thanks for the heads up on the code tags too. like I said I’m new so any information is useful at this point. Thank you!

Managed to get it figured out. I set a separate button that would toggle between first and third person view and then I set up the third person view camera on the scroll wheel with a clamp between 60-100. In case anyone is interested here’s the code.

public class CameraSwitch : MonoBehaviour
{
    public GameObject ThirdPersonView;
    public GameObject FirstPersonView;



    void Update()
    {
      

        if (Input.GetButtonDown("Camera Toggle") && FirstPersonView.activeSelf == true)
        {
            ThirdPersonView.SetActive(true);
            FirstPersonView.SetActive(false);

            if (FirstPersonView.activeSelf == false)
            {
                return;
            }
        }

        if (Input.GetButtonDown("Camera Toggle") && ThirdPersonView.activeSelf == true)
        {


            ThirdPersonView.SetActive(false);
            FirstPersonView.SetActive(true);

            if (ThirdPersonView.activeSelf == false)
            {
                return;
            }
        }
      
        if (ThirdPersonView.activeSelf == true)
        {
            ThirdPersonView.GetComponent<Camera>().fieldOfView = Mathf.Clamp(ThirdPersonView.GetComponent<Camera>().fieldOfView, 60f, 100f);
          
            if (Input.GetAxis ("Mouse ScrollWheel") > 0)
            {
                ThirdPersonView.GetComponent<Camera> ().fieldOfView--;
            }
            if (Input.GetAxis ("Mouse ScrollWheel") < 0)
            {
                ThirdPersonView.GetComponent<Camera> ().fieldOfView++;
            }
        }





    }
}
1 Like