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;
GameObject ball = Instantiate(projectile, transform.position,
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.
- Where is the camera to start with?
- Is there a cannon to which the camera is attached as a child?
- Do you have a player to which the camera is attached as a child?
- Are you happy for the camera to become detached and follow the cannonball at full speed?
- What happens to the camera if it’s a long way from the cannon to start with?
- 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?
- Do you expect a new zoom value when following a cannonball?
- What happens to the camera when the cannonball hits something?
- What happens to the camera if the cannonball doesn’t hit anything?
- At what point does the camera move back to its original position?
- If the camera is following the cannonball, what’s the distance and angle between them?
- 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?
- 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.
- 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)
- 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.
- No, I envision the cannonball as the playerObject for this scene/minigame.
- Yes, I want the camera to follow at full speed (for now).
- I don’t see it being an issue. After 3 shots I would want the scene to ‘reset’.
- See above
- No new zoom value, but knowing how to adjust that would be appreciated.
- 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).
- Eventually I would program collisions w/ the ground.
- See 5.
- I think I will be doing a 2d project, so the camera would remain in side view.
- I would like to know how to do both but instantly is fine for now.
- 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;
vcam = GetComponent<CinemachineVirtualCamera>();
// Update is called once per frame
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!).