Question by
hocineache · Apr 20 at 03:19 PM ·
camera rotatefollow player
How do i tell the camera to only follow the gameObject x orientation
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);
}
}
Comment
Your answer