Switch between camera code not working

Hello, im a beginner of unity


I have created a code to change first person to third person


The problem is that is not working

My code:

public class CameraToggle : MonoBehaviour
{

    public Rigidbody rb;


    // Start is called before the first frame update
    void Start()
    {
        if(Input.GetKeyDown("t"))
        {
            rb.velocity = new Vector3(0, 0, -5);
        }
    }

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

I added this script in Main Camera
AND IT DOESN’T WORKS WHEN I CLICK THE KEY “T”


In console there is no error
plss someone help!

It seems you are changing velocity of Camera Toggle script. Also you are checking input in the “Start” method which is called only once at monobehaviour life cycle. As per my knowledge changing velocity will not change your camera position or switch between camera. If you want to switch between First person and Third person camera, You can do one thing.

Make empty game object, which will follow your player. Make camera gameobject child of this game object. Let’s call this game object camera parent object. Attach camera follower script to this object and make 2 public vector3 fields in it like this.

public Vector3 thirdPersonPosition;
public Vector3 firstPersonPosition;

First set camera at the offset from the player where it will be work like third person camera, for ex. Put camera 5 units away from the player position (Camera parent gameobject position will be same as player position, change only camera position). When you have set camera at desired position, note down its position and assign this position to “thirdPersonPosition” variable of Camera follower script in the inspector. Now do the same for the first person camera, set camera to the first person position and assign it to “firstPersonPosition” variable in inspector.

Final Script

public class CameraFollower : MonoBehaviour
{
     public Vector3 thirdPersonPosition;
     public Vector3 firstPersonPosition;
     **//Make another boolean in camera follower script.**
     private bool isThirdPerson = true; //Change this accordingly.

     **//Add reference for camera game object**
     public Transform cameraTrans;

     **//Add reference for player gameobject**
     public Transform playerTrans;

     private void Start()
     {
           cameraTrans.localPosition = isThirdPerson ? thirdPersonCamera : firstPersonCamera;
     }

     private void Update()
     {
           if(Input.GetKeyDown("t"))
          {
                isThirdPerson = !isThirdPerson;
                cameraTrans.localPosition = isThirdPerson ? thirdPersonCamera : firstPersonCamera;
          }
          transform.position = playerTrans.position
     }
}

Note that , I have set local position of the camera and not the position. This is because, we need to reposition camera according to it’s parent and not through 3D world position.
There you go!!!

If this answer helped you, please up vote it.