- Home /
The question is answered
how to make the camera follow the player only in one axis (the z axis)
im making an endless runner game and i want that the camera follow the player only in z axis
im using the simple "roll a ball" unity tutorial camera script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class cameramove : MonoBehaviour {
public GameObject player;
private Vector3 offset;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;
}
// Update is called once per frame
void LateUpdate () {
transform.position = player.transform.position+ offset ;
}
}
so how can i modify it to make the camera follow the player only in the z axis
and i want to ask why they are using lateupdate()
any help will be appreciated ^ ^
A bit too late but if anyone needs the solution, here is one:
public class CameraController : $$anonymous$$onoBehaviour
{
public GameObject player = null; // You can use [SerializeField] as well
private Vector3 cameraOffset;
private void Start()
{
cameraOffset = transform.position - player.transform.position;
}
private void LateUpdate()
{
Vector3 currentPosition = transform.position;
currentPosition.z = (cameraOffset + player.transform.position).z;
transform.position = currentPosition;
}
}
Follow this Question
Related Questions
Follow after height 1 Answer
How do i get a camera to follow a Bird/Plane smoothly? 2 Answers
Camera Jitter using Fixed or LateUpdate, need it to be smooth in both. 2 Answers
Super Monkey Ball type Camera? 2 Answers
How to make your character move? 7 Answers