Hello, I don’t quite understand how to describe this, so I made a demonstration in Blender of how I want the camera to work in Unity. This can be described as the camera should follow the ball and rotate around this “pillar” while looking at the “target”.
Here’s an example of what I want
https://youtu.be/Vvj7JH8LhJ0
What does it look like at the moment
https://youtu.be/UzVrmXLKRqo
As you can see, the camera simply follows the subject from one angle.
In Blender I implemented this using the Track to constraint to keep the object in the field of view and follow the curve for the camera path, is it possible to implement something similar in Unity?
2 Answers
2
Here’s how I have done this:
public Camera cam;
public Transform spiral;
public Transform ball;
public float camDistance = 5.0f;
public float camRotAdjustment = 45f;
//Calculate direction from spiral center to ball,
var spiralPositionXZ = spiral.position;
spiralPositionXZ.y = ball.position.y; //disregard the y distance (up/down) of the ball
var ballDirectionFromSpiral = (ball.position - spiralPositionXZ).normalized;
//Calculate the required cam position
var newCamPosition = spiral.transform.position + (ballDirectionFromSpiral * camDistance);
float spiralToCamStartingHeightRatio = 1.5f; //Optional: start cam above the spiral like in the video. Assumes spiral ends on ground plane
newCamPosition.y = ball.position.y * spiralToCamStartingHeightRatio; //Keep cam at same level as ball
cam.transform.position = newCamPosition;
//Adjust cam rotation so it is more to the side of the ball
cam.transform.RotateAround (spiral.position, Vector3.up, camRotAdjustment);
//Focus on ball
cam.transform.LookAt (ball);
Hello @antalikra,
The ball rotates around the pillar so it’s Y rotation is progressively changing to keep it’s course around the pillar right? Or does your ball follow a path?
You can make your camera stick to a point in 3D space around the ball for example:
public Transform target;
public float offsetForward;
public float offsetUp;
public float offsetRight;
private void Update() {
this.transform.position = this.target.position + (this.target.forward * offsetForward + this.target.right * offsetRight + Vector3.up * offsetUp);
this.transform.LookAt(this.target.position, Vector3.up);
}
try this code and tell me if that’s what you wanted.
In the case the ball follows a specific path you just need the LookAt and make the camera follow the same path with an offset.
Not exactly what I need, I’ll try to explain the idea of the game, the ball rolls down in a spiral, the player can accelerate the ball to the right or left (in the future there will be obstacles on the spiral that will need to be avoided) and the camera should seem to follow the ball, being directed at him in front of him, but also at the same time being constantly at the same distance relative to the “pillar”, as if flying around it in a circle and descending lower and lower depending on how low the ball descended. [1/2]
– antalikraThe position of the ball along the Z and X coordinates should not affect the position of the camera. I assume that an additional shift to the side relative to the ball is needed, because what is the problem with simply following, since the ball does not roll down a straight slope, but in a spiral, a situation arises when the ball goes behind the “pillar” and is lost from view because The camera does not rotate additionally to fly around the “pillar” in a circle. [2/2]
– antalikraThat's why I used target.forward, to always be in on a precise position relative to the ball and pillar, but I just realized with what you are saying that the ball is rolling so the forward will change. You could simply take the angle of the ball from the pillar and apply it to the camera with an offset and then do a LookAt
– Cyber_Cats