- Home /
How can I get my camera behind my moving ball ?
I'm trying to do something simple. In a golf game, I want my camera to follow the ball at a certain distance. So basically I created a Vector 3 offset and in my camera update I set my camera to the position of the ball + my offset.
Vector3 offset;
offset = new Vector3 (-3, 1, -1.5f);
//update
Vector3 pos = ball.transform.position + offset;
transform.position = pos;
However, the camera offset seems to change depending of the shot :
Here the ball is on the right of thecamera and on the next shot:
the ball is behind the camera.
Answer by ShySam · Aug 16, 2020 at 10:51 AM
@Steyo you can try to increase the offset or use (int) values incited of numbers in the offset like:-
//put these values out of any void .. no need to bu them under the void update to use them public int Value1 = -3 public int Value2= 1 public int Value3 = -1.5
//then add them to the offset like this: offset = new Vector3 (Value1 , Value2, Value3 ); . also you can make the camera look at the ball at all times by adding this line to void ubdate [ transform.forward = lookToward.normalized; ] (don't take the brackets [] ! . if that did't work look fore (Third person camera follow script )
@Steyo the secreipt will be like this\\
public int Value1 = -3 public int Value2= 1 public int Value3 = -1.5
Vector3 offset; offset = new Vector3 (Value1 , Value2, Value3 );
void update () { Vector3 pos = ball.transform.position + offset; transform.position = pos; }
Answer by highpockets · Aug 16, 2020 at 11:45 AM
The offset you are giving is not changing based on the ball’s moving direction, assuming the ball has a parent transform that doesn’t rotate with the ball as it rolls, you can just make the camera a child of that and it should take care of this for you, but otherwise you need to get the offset based on the balls movement direction.
//You could get the movement direction from the rigid body velocity for example, but let’s assume you have that direction and it’s normalized for brevity sake
Vector3 localPos = -ballMoveDirection * 1.5; //a local position 1.5 units behind the ball
//add some height to That position
localPos = new Vector3(localPos.x, localPos.y + 3, localPos.z);
//place the cam at the new pos
camTransform.position = ballTransformPosition + localPos;
//now make sure the cam is looking at the ball
camTransform.LookAt(ballTransform);
You might have to customize it to fit your needs, but this is more or less what you need
This look clear enough but how do I get ball$$anonymous$$oveDirection ? It's a Vector3 I'm supposed to get from Rigidbody ?
You can get the move direction if the ball has a rigidbody by accessing the velocity vector and normalizing it. Otherwise just test the last frame position vs current frame position of the ball and the current position $$anonymous$$us the last position is the direction, but then remember to normalize it to be able to use it effectively.
One thing to think about is if the ball moves upwards, this will make the camera go downwards, so you might want to flatten the directions over a constant plane so the camera is always above the ball. For that, you can you Vector3.ProjectOnPlane which takes the normal of the plane and the direction you want to flatten onto it as parameters and returns the flattened direction.
Your answer
Follow this Question
Related Questions
Camera Controller to Player Position Conflict 1 Answer
Wht isn't the sphere moving and rebounding properly? 1 Answer
Enemy spawn script/player position(java/js) 1 Answer
Trying to create a camera that follows player and mimics headset tracking(HTC Vive) 1 Answer
Move one object with another, without parenting it to the object? 1 Answer