How to make a camera follow a instantiated object/projectile?

I have a question, apologies if I have not provided enough detail, I am still very new to all this!

I am trying to do the “Using C# to launch projectiles” tutorial. Doing it by the letter is easy and works.

I am trying to go a bit above and beyond and get the camera to follow the cannonball that gets instantiated, and if two cannonballs are fired, to follow the ‘average position’ of the two (or three). Now, that is a secondary issue. But how can I get the camera to follow the cannonball?

Link to tutorial: Using C# to launch projectiles - Unity Learn

public class LaunchProjectile : MonoBehaviour
    public GameObject projectile;
    public float launchVelocity = 700f;
    void Update()
        if (Input.GetButtonDown("Fire1"))
            GameObject ball = Instantiate(projectile, transform.position,  
            ball.GetComponent<Rigidbody>().AddRelativeForce(new Vector3 
                                                 (0, launchVelocity,0));

Thanks for posting as a separate question. Hopefully you’ll understand why by the end of my answer. This is one of those questions that needs a lot of unpacking. It raises many questions.

  1. Where is the camera to start with?
  2. Is there a cannon to which the camera is attached as a child?
  3. Do you have a player to which the camera is attached as a child?
  4. Are you happy for the camera to become detached and follow the cannonball at full speed?
  5. What happens to the camera if it’s a long way from the cannon to start with?
  6. In that case, are you happy for the camera to jump to its new relative position immediately or do you want it to be a smooth transition?
  7. Do you expect a new zoom value when following a cannonball?
  8. What happens to the camera when the cannonball hits something?
  9. What happens to the camera if the cannonball doesn’t hit anything?
  10. At what point does the camera move back to its original position?
  11. If the camera is following the cannonball, what’s the distance and angle between them?
  12. If you fire a second/third/fourth cannonball after the first, how do you expect the camera to move to a new “average” position? Instantly or smoothly?
  13. What happens if one of the (many) cannonballs hits something? Does the camera follow a new “average” position or stay with the collision?

Please don’t think I’m picky but the coding implications of many of these make the solution very complex. The simplest solution is that you pick an offset and, in Update() you move the camera to the cannonball’s position plus the offset, possibly rotating the camera to look at the ball a little more. You still need to decide how and when the camera returns (typically saving a start position which is used again at the end). Only works for a single cannonball, though…

How do you answer questions like these? Actually you do it by playing with physical toys. Have real objects for player, cannon, camera, cannonball and play it through for real. Ask yourself where things should be at all times and what happens before, during and afterwards. Only then can you start to design your program. It’s what we call prototyping the game play.

First, apologies for the duplicate questions (on my end I got errors and thought it didn’t post), I’ll delete them right after posting this.

  1. I would want the camera to start more or less where the cannonball will instantiate. So lets say (2, 1, -3.5) with rotation (0, -90, 0)
  2. The camera is not currently a child of the cannon. For the record I do know how to make the camera track a ‘playerObject’ but Instantiation seems to work differently. Or I just haven’t figured it out.
  3. No, I envision the cannonball as the playerObject for this scene/minigame.
  4. Yes, I want the camera to follow at full speed (for now).
  5. I don’t see it being an issue. After 3 shots I would want the scene to ‘reset’.
  6. See above
  7. No new zoom value, but knowing how to adjust that would be appreciated.
  8. I know how to program (basic) collision detection so ultimately I would want the camera to pause on where the collision occured (to enjoy the hypothetical animation/particle effects for 3 seconds or until next click).
  9. Eventually I would program collisions w/ the ground.
  10. See 5.
  11. I think I will be doing a 2d project, so the camera would remain in side view.
  12. I would like to know how to do both but instantly is fine for now.
  13. Ideally… If its one cannon ball it follows it. If its two cannonballs it follows the average position. If the first one is destroyed, follow the 2nd. If its three, follow the average position.

Hope that helps! Thanks again.


Can someone help me out here?

So I went over to the forums and got some proper help from a bloke by the name of Gregoryl!

I used Cinemachine although I’m sure this could be implemented without it. There are a couple key parts.
Declaring the Cinemachine as a variable. The key difference, regarding an Instantiated object, is checking for null, as I understand it, before the Instantiation occurs, your target will be null, and this would normally throw an error. Instead you use that null check to FindWithTag (this means your instantiated prefab must have the correct tag on it, in this case “Player”).

And then you ‘simply’ get the vcam to LookAt and Follow your player/projectile/tagged object!

public GameObject tPlayer;
public Transform tFollowTarget;
private CinemachineVirtualCamera vcam;

void Start()

       vcam = GetComponent<CinemachineVirtualCamera>();
    // Update is called once per frame
    void Update()
       if (tPlayer == null)
        tPlayer = GameObject.FindWithTag("Player");
        if(tPlayer != null)
            tFollowTarget = tPlayer.transform;
            vcam.LookAt = tFollowTarget;
            vcam.Follow = tFollowTarget;

h/t to Gregoryl. Here is the full thread if anyone is interested (years from now!).