- Home /
Question by
ARTIFICIALINNOVATION · Nov 01, 2021 at 01:19 PM ·
tagscamera follow
Camera not following a GameObject with Tag after spawned in the scene?
I am making a racing game in which the player has to select the car and it loads the scene. The camera has FindWithTag("Player").transform;
. But, when the prefab spawns as a prefab(clone) The camera, is not able to follow the cars.
#pragma warning disable 649
namespace UnityStandardAssets.Utility
{
public class SmoothFollow : MonoBehaviour
{
public CarUIClass CarUI;
private int gearst = 0;
private float thisAngle = -150;
// The target we are following
[SerializeField]
private Transform target;
// The distance in the x-z plane to the target
[SerializeField]
private float distance = 10.0f;
// the height we want the camera to be above the target
[SerializeField]
private float height = 5.0f;
[SerializeField]
private float rotationDamping;
[SerializeField]
private float heightDamping;
private VehicleControl carScript;
// Use this for initialization
void Start()
{
target = GameObject.FindWithTag("Player").transform;
}
// Update is called once per frame
void LateUpdate()
{
// Early out if we don't have a target
if (!target)
return;
// Calculate the current rotation angles
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position = new Vector3(transform.position.x ,currentHeight , transform.position.z);
// Always look at the target
transform.LookAt(target);
}
public void CarAccelForward(float amount)
{
carScript.accelFwd = amount;
}
public void CarAccelBack(float amount)
{
carScript.accelBack = amount;
}
public void CarSteer(float amount)
{
carScript.steerAmount = amount;
}
public void CarHandBrake(bool HBrakeing)
{
carScript.brake = HBrakeing;
}
[System.Serializable]
public class CarUIClass
{
public Image tachometerNeedle;
public Image barShiftGUI;
public Text speedText;
public Text GearText;
}
public void ShowCarUI()
{
gearst = carScript.currentGear;
CarUI.speedText.text = ((int)carScript.speed).ToString();
if (carScript.carSetting.automaticGear)
{
if (gearst > 0 && carScript.speed > 1)
{
CarUI.GearText.color = Color.white;
CarUI.GearText.text = gearst.ToString();
}
else if (carScript.speed > 1)
{
CarUI.GearText.color = Color.white;
CarUI.GearText.text = "R";
}
else
{
CarUI.GearText.color = Color.white;
CarUI.GearText.text = "N";
}
}
else
{
if (carScript.NeutralGear)
{
CarUI.GearText.color = Color.white;
CarUI.GearText.text = "N";
}
else
{
if (carScript.currentGear != 0)
{
CarUI.GearText.color = Color.white;
CarUI.GearText.text = gearst.ToString();
}
else
{
CarUI.GearText.color = Color.white;
CarUI.GearText.text = "R";
}
}
}
thisAngle = (carScript.motorRPM / 20) - 175;
thisAngle = Mathf.Clamp(thisAngle, -180, 90);
CarUI.tachometerNeedle.rectTransform.rotation = Quaternion.Euler(0, 0, -thisAngle);
CarUI.barShiftGUI.rectTransform.localScale = new Vector3(carScript.powerShift / 100.0f, 1, 1);
}
}
}
Comment
Answer by Betina_art · Nov 01, 2021 at 01:42 PM
Hi! Have you considered using Cinemachine? You can simply pass in the transform of the selected car to cinemachine's Follow and LookAt and it should follow the specified car automatically:
using Cinemachine;
public CinemachineVirtualCamera camera; //just attach the cinemachine camera here from the editor
WhicheverMethod()
{
vcam.LookAt = this.transform;
vcam.Follow = this.transform;
}
Your answer
