- Home /
LookAt and Follow different objects
Hi,
I am making a mini basketball game which you control a ball. Camera follows the ball and looks at basket but when i get close to the basket and get its left or right side camera doesn't see the ball.
Here is my code:
public class CameraControl : MonoBehaviour {
public Transform basket;
public Transform ball;
private Vector3 offset;
// Start is called before the first frame update
void Start()
{
offset = transform.position - ball.position;
}
// Update is called once per frame
void Update()
{
FollowBall();
LookAtBasket();
}
void FollowBall()
{
Vector3 position = transform.position;
position.x = ball.position.x + offset.x;
position.z = ball.position.z + offset.z;
transform.position = position;
}
void LookAtBasket()
{
transform.LookAt(basket);
}
What i am trying to accomplish is make camera stop LookAt basket and make a smooth movement to the left or right when ball gets to left or right of basket and also keep the offset with ball.
What do i need to add my code? Could you pls help me with this?
Your answer
Follow this Question
Related Questions
How to allow camera to complete an upside down rotation while using LookAt() 0 Answers
My camera keeps spinning when i move my mouse 1 Answer
Camera gets stuck when cursor is locked 0 Answers
Rotate Camera to the object 0 Answers
camera movments fixed.. character controller without using character controller -.-' 2 Answers