0
\$\begingroup\$

I need a camera follow script to chase a moving ball. I've attached the script below to my camera, but when the ball turns left and right the camera does not turn

using UnityEngine;

public class FollowPlayerCamera : MonoBehaviour
{
    public GameObject[] playerBalls; // Array of player balls
    public float smoothSpeed = 0.125f; // How smoothly the camera follows the ball
    public Vector3 offset; // Offset from the ball position

    private ActivatePlayer playerController; // Reference to the BallPlayerController script
    private Transform target; // The current active player to follow

    void Start()
    {
        playerController = FindObjectOfType<ActivatePlayer>();
        if (playerController != null)
        {
            // Ensure there is at least one player in the array
            if (playerBalls.Length > 0)
            {
                // Set the first player in the array as the initial target
                target = playerBalls[playerController.selectedPlayerIndex].transform;
            }
        }
        else
        {
            Debug.LogWarning("BallPlayerController script not found in the scene!");
        }
    }

    void LateUpdate()
    {
        if (playerController == null)
        {
            return;
        }

        // Find the active player to follow
        if (playerController.selectedPlayerIndex >= 0 && playerController.selectedPlayerIndex < playerBalls.Length)
        {
            target = playerBalls[playerController.selectedPlayerIndex].transform;
        }

        if (target == null)
        {
            // If there's no target, exit the function
            return;
        }

        // Calculate the desired position of the camera
        Vector3 desiredPosition = target.position + offset;

        // Interpolate smoothly towards the desired position
        Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);

        // Set the camera's position
        transform.position = smoothedPosition;
    }
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ This is really hard to read. As for the actual problem, hint: Vector3.Lerp is used wrongly. \$\endgroup\$
    – Zibelas
    Sep 18, 2023 at 11:56
  • \$\begingroup\$ I don't see any attempt in this code to change the camera's rotation. If you're not comfortable writing your own logic to turn the camera, have you considered using a pre-made camera control system like Cinemachine? This is available as a free package you can add to your project to get highly customizable camera behaviours without writing your own code. You'll be able to find existing examples and tutorials for configuring its follow behaviour to your liking. \$\endgroup\$
    – DMGregory
    Sep 18, 2023 at 14:28
  • \$\begingroup\$ you write any code for this camera and any video recommended and any how now to cinemachine \$\endgroup\$ Sep 19, 2023 at 4:56
  • \$\begingroup\$ Cinemachine (as suggested above) is pretty amazing. \$\endgroup\$ Sep 19, 2023 at 14:57

0

You must log in to answer this question.

Browse other questions tagged .