Follow and Bounce Object in Unity 2D Without Update()

Follow and Bounce Object in Unity 2D Without Update()

This tutorial explains how to create a smooth following and bouncing system for a 2D game object in Unity We’ll use Coroutines to make the object follow a moving ball and apply jump forces upon contact.

🎮 Step 1: Attach the Script

Create a new C# script named FollowAndBounce.cs and attach it to an object (for example, a platform or AI helper) that will follow the ball.

🧠 Step 2: The Complete Script

using UnityEngine;
using System.Collections;

public class FollowAndBounce : MonoBehaviour
{
    [SerializeField] Transform ball;
    Rigidbody2D rb;
    [SerializeField] float jumfore = 4f;
    [SerializeField] float speed = 5f;
    [SerializeField] float followCheckInterval = 0.1f; // check if ball moves
    [SerializeField] float followSmooth = 0.02f;       // update smoothness

    Coroutine followCoroutine;
    Vector3 lastBallPos;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        lastBallPos = ball.position;
        StartCoroutine(CheckBallMovement());
    }

    // ✅ Detect if the ball is moving
    private IEnumerator CheckBallMovement()
    {
        while (true)
        {
            if (ball == null) yield break;

            // If the ball is moving
            if (Vector3.Distance(ball.position, lastBallPos) > 0.001f)
            {
                // Start following if not already doing so
                if (followCoroutine == null)
                    followCoroutine = StartCoroutine(FollowBallSmooth());
            }
            else
            {
                // Stop following when the ball stops
                if (followCoroutine != null)
                {
                    StopCoroutine(followCoroutine);
                    followCoroutine = null;
                }
            }

            lastBallPos = ball.position;
            yield return new WaitForSeconds(followCheckInterval);
        }
    }

    // ✅ Smoothly follow the ball’s X position
    private IEnumerator FollowBallSmooth()
    {
        while (true)
        {
            if (ball != null)
            {
                Vector3 current = transform.position;
                Vector3 target = new Vector3(ball.position.x, current.y, current.z);
                transform.position = Vector3.Lerp(current, target, Time.deltaTime * speed);
            }

            yield return new WaitForSeconds(followSmooth);
        }
    }

    // ✅ Apply jump force when colliding with the ball
    private void OnTriggerStay2D(Collider2D other)
    {
        ApplyJumpForce(other);
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        ApplyJumpForce(other);
    }

    private void ApplyJumpForce(Collider2D other)
    {
        if (other.GetComponent<ball>())
        {
            Rigidbody2D otherRb = other.attachedRigidbody;
            if (otherRb != null && otherRb.velocity.y <= 0)
            {
                Vector2 velocity = otherRb.velocity;
                velocity.y = jumfore;
                velocity.x += jumfore * Mathf.Sin(Time.time * speed);
                otherRb.velocity = velocity;
            }
        }
    }
}

⚙️ Step 3: Explanation

  • No Update(): We use Coroutines (CheckBallMovement() and FollowBallSmooth()) to handle motion efficiently.
  • Follow Logic: The object tracks the ball’s X-axis position and smoothly moves using Vector3.Lerp.
  • Bounce Logic: When the object collides with the ball, it applies upward velocity and a small sine-based horizontal bounce.
  • Performance: The system sleeps when the ball stops, saving CPU usage.

💡 SEO Keywords

  • Unity 2D follow ball script
  • Unity coroutine movement
  • Unity bounce with Rigidbody2D
  • Unity smooth follow tutorial
  • Unity physics jump example
  • Unity no Update method
  • Follow object in Unity 2D
  • Unity coroutine follow example
Pro tip: This pattern is perfect for AI companions, bouncing floors, or magnetic objects that track players automatically.