Camera follow player with Lerp and Added another example of camera not lerping, but moving away after idle
Added a tiny demo of my issue, and clarified a little bit of more information
Example
Code
CameraController.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; /* ** Camera following player (Smoothing and angle): https://youtu.be/4HpC--2iowE ** Maximo: https://www.youtube.com/watch?v=bXNFxQpp2qk&ab_channel=iHeartGameDev ** Moving character relative to camera: https://forum.unity.com/threads/moving-character-relative-to-camera.383086/ ** Camera follow v2: https://youtu.be/Jpqt2gRHXtc?list=PLq_nO-RwB516fNlRBce0GbtJSfysAjOgU */ public class CameraController : MonoBehaviour { public GameObject player; public PlayerControl playerControlScript; private Vector3 newCameraPos; public bool stillIdle; void Start() { stillIdle = false; PlayerControl playerControlScript = GetComponent<PlayerControl>(); } void LateUpdate() { player = GameObject.FindGameObjectWithTag("Player"); if (playerControlScript.GetfirstInput()) //True { stillIdle = true; newCameraPos = Vector3.Lerp(transform.position, playerControlScript.transform.position, Time.deltaTime); transform.position = new Vector3(newCameraPos.x, 1, newCameraPos.z); } if (stillIdle) transform.position = new Vector3(transform.position.x + 0.69f * Time.deltaTime, transform.position.y, transform.position.z); //Moving camera away effect } } PlayerControl.cs
public class PlayerControl : MonoBehaviour { bool firstInput; Vector3 startPos; Vector3 endPos; public bool GetfirstInput() //I was learning how to have a Get function while my member was private from another script file { return firstInput; } void Update() { if (Input.GetButtonDown("up") || Input.GetButtonDown("left") || Input.GetButtonDown("right") || Input.GetButtonDown("down")) { //if game starts { //Other variables being initialized here firstInput = true; } } } } Hierarchy/Inspector
Main Camera
Player Object
Code
using System.Collections; using System.Collections.Generic; using UnityEngine; /* ** Camera following player (Smoothing and angle): https://youtu.be/4HpC--2iowE ** Maximo: https://www.youtube.com/watch?v=bXNFxQpp2qk&ab_channel=iHeartGameDev ** Moving character relative to camera: https://forum.unity.com/threads/moving-character-relative-to-camera.383086/ ** Camera follow v2: https://youtu.be/Jpqt2gRHXtc?list=PLq_nO-RwB516fNlRBce0GbtJSfysAjOgU */ public class CameraController : MonoBehaviour { public GameObject player; public PlayerControl playerControlScript; private Vector3 newCameraPos; public bool stillIdle; void Start() { stillIdle = false; PlayerControl playerControlScript = GetComponent<PlayerControl>(); } void LateUpdate() { player = GameObject.FindGameObjectWithTag("Player"); if (playerControlScript.GetfirstInput()) //True { stillIdle = true; newCameraPos = Vector3.Lerp(transform.position, playerControlScript.transform.position, Time.deltaTime); transform.position = new Vector3(newCameraPos.x, 1, newCameraPos.z); } if (stillIdle) transform.position = new Vector3(transform.position.x + 0.69f * Time.deltaTime, transform.position.y, transform.position.z); //Moving camera away effect } } Example
Code
CameraController.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; /* ** Camera following player (Smoothing and angle): https://youtu.be/4HpC--2iowE ** Maximo: https://www.youtube.com/watch?v=bXNFxQpp2qk&ab_channel=iHeartGameDev ** Moving character relative to camera: https://forum.unity.com/threads/moving-character-relative-to-camera.383086/ ** Camera follow v2: https://youtu.be/Jpqt2gRHXtc?list=PLq_nO-RwB516fNlRBce0GbtJSfysAjOgU */ public class CameraController : MonoBehaviour { public GameObject player; public PlayerControl playerControlScript; private Vector3 newCameraPos; public bool stillIdle; void Start() { stillIdle = false; PlayerControl playerControlScript = GetComponent<PlayerControl>(); } void LateUpdate() { player = GameObject.FindGameObjectWithTag("Player"); if (playerControlScript.GetfirstInput()) //True { stillIdle = true; newCameraPos = Vector3.Lerp(transform.position, playerControlScript.transform.position, Time.deltaTime); transform.position = new Vector3(newCameraPos.x, 1, newCameraPos.z); } if (stillIdle) transform.position = new Vector3(transform.position.x + 0.69f * Time.deltaTime, transform.position.y, transform.position.z); //Moving camera away effect } } PlayerControl.cs
public class PlayerControl : MonoBehaviour { bool firstInput; Vector3 startPos; Vector3 endPos; public bool GetfirstInput() //I was learning how to have a Get function while my member was private from another script file { return firstInput; } void Update() { if (Input.GetButtonDown("up") || Input.GetButtonDown("left") || Input.GetButtonDown("right") || Input.GetButtonDown("down")) { //if game starts { //Other variables being initialized here firstInput = true; } } } } Hierarchy/Inspector
Main Camera
Player Object
Loading
Loading
lang-cs



