1
\$\begingroup\$

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; } } } 
\$\endgroup\$
2
  • \$\begingroup\$ If you're using the code I offered in answer to your previous question, please go back and click the ✅ icon to mark that answer as "Accepted". Do I understand correctly you're following the player on the y+ axis, but you want to pull back on the z- axis? Is your character being moved with a Rigidbody/Rigidbody2D from which we can read a velocity, or can you show us your character control script? \$\endgroup\$ Commented Apr 18, 2024 at 20:11
  • \$\begingroup\$ Ok, just marked the check mark on the first question. Yes, I am using rigidbody on player, I will provide the logic of that on here. Thank you \$\endgroup\$ Commented Apr 18, 2024 at 20:29

1 Answer 1

0
\$\begingroup\$

Use SmoothDamp, very similar to what you are currently doing with the Y axis.

// ... // Notice this is now a Rigidbody2D, not a Transform: [SerializeField] private Rigidbody2D followTarget; // You will need to customize these two values: [SerializeField] private float defaultZDistance = 4; [SerializeField] private float speedToZDistanceFactor = 1; float _velocityZ; // 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.transform.position.y + offset); // Smoothly chase targetHeight on y axis. pos.y = Mathf.SmoothDamp( pos.y, targetHeight, ref _velocity, smoothDampTime ); // Smoothly set distance on Z axis. float offsetZ = defaultZDistance + followTarget.velocity.x * speedToZDistanceFactor; float destinationZ = followTarget.transform.position.z - offsetZ; pos.z = Mathf.SmoothDamp( pos.z, destinationZ, ref _velocityZ, smoothDampTime ); // Apply smoothed chasing position to // this (camera) object's transform. transform.position = pos; } // ... 
\$\endgroup\$
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.