How can I make the camera move further away from the player according to player's speed?
So, the camera is at default distance, but when the player catches a boost (increases speed), the camera smoothly moves further back. When player gradually gets back to normal speed, the camera gradually comes back to default distance.
I'm using UNITY; C# ; Game is at it's very beginning, very open to any suggestions.
This is the camera follow logic I'm using:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class SmoothUpwardFollow : MonoBehaviour { public Transform followTarget; [Tooltip("How far above the player the camera should aim, in world units")] public float offset = 0f; [Tooltip("How gently the camera should accelerate/decelerate — small values are more responsive")] public float smoothDampTime = 0.2f; // Used to ensure camera's speed is continuous, not jerky. float _velocity; // Update at end of frame, after player character // has finished moving in Update or FixedUpdate. void LateUpdate() { var pos = transform.position; // Max ensures we only move up, not down. float targetHeight = Mathf.Max(pos.y, followTarget.position.y + offset); // Smoothly chase targetHeight on y axis. pos.y = Mathf.SmoothDamp( pos.y, targetHeight, ref _velocity, smoothDampTime ); // Apply smoothed chasing position to // this (camera) object's transform. transform.position = pos; } } I moving player with a RigidBody component:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class BirdScript : MonoBehaviour { public Rigidbody2D myRigidbody; public float flapStrength; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Space) == true) { myRigidbody.velocity = Vector2.up * flapStrength; } } }