Home > Software design >  How do i tell the camera to only follow the gameObject x orientation
How do i tell the camera to only follow the gameObject x orientation

Time:04-21

Hello everyone beginner here, i am working on the moving vehicle challenge and i could make the camera follow the truck and also could make the camera switch between views (driver view/back view) the problem is when i switch to back view the initial x rotation for the camera is set to 0 so i want the camera to follow the player x orientation when in driver view so i don't lose the back view orientation, you can see my code below and the link for the project package here, Thank you

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FollowPlayer : MonoBehaviour
 {
     //Player GameObject variable (the vehicle)
     public GameObject player;
     //Fixing the camera vertical position
     private Vector3 offset = new Vector3(0, 5, -7);
     private Vector3 offset2 = new Vector3(0, 2.5f, 0.3f);
     private int currentTarget;
     public bool camController;
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void LateUpdate()
     {
         camController = Input.GetButtonDown("Fire1");
         if (camController) { 
             if (offset == offset2)
             {
                 currentTarget = 2;
                 
             } else
             {
                 currentTarget = 1;
                
             }
  
             switch(currentTarget)
             {
                 case 1:
                     offset = offset2;
                     break;
                 case 2:
                     offset = new Vector3(0, 5, -7);
                     break;
             }
         }
         //Offset the camera behind the player by adding to the player's position
         transform.position = player.transform.position   offset;
         transform.rotation = player.transform.rotation;
         Debug.Log(camController);
     }
 }

CodePudding user response:

I think it would be easier to have multiple cameras and switch between them. So create your 2 cameras, add Parent Constraints to them and set them up as you need. Then create a script which enables and disables the cameras like this:

using UnityEngine;
public class SwitchCams : MonoBehaviour
{
    public GameObject cam1;
    public GameObject cam2;

    bool isCam1 = true;
    
    void Start(){
        cam1.SetActive(true);
        cam1.SetActive(false);
    } 

    void Update(){
        bool shouldSwitch = Input.GetButtonDown("Fire1");
        if(shouldSwitch){
            isCam1 = !isCam1;
            cam1.SetActive(isCam1);
            cam2.SetActive(!isCam1);
        } 
    } 
} 

For some reason in unity the easiest way to switch between cameras is to enable and disable them. So this script just does that. Remember to drag your cameras into the slots cam1 and cam2 of the script in the inspector.

  • Related