- Home /
Camera to follow a target within a circle?
Hello Devs,
I am working on a script where the camera follows the player (only on 'y' axis). If the player moves in X or Z axis I would like the camera to follow only when the player moves beyonds a circle with a radius 'r' and the origin as camera's X & Z co-ordinates.
To brief it in another view, I would like to have camera's co-ordinates (X & Z) as origin and with a radius r, I want to create a imaginary circle. Lets say a gameobject is a target for the camera and the camera is following the target across Y axis. I would like the camera to follow the player, one and only if it moves beyond the CIRCLE.
I did try out but I got stuck at a certain point. The code is as follows,
Vector2 DrawCircle(float originX, float originY, float r)
{
Vector2 cir;
for (int i= 0; i < 180; i++)
{
Circle.x = r * Mathf.Cos(i*Mathf.DegToRad) + originX;
Circle.y = r * Mathf.Sin(i*Mathf.DegToRad) + originY;
cir = new Vector2(Circle.x, Circle.y);
return cir;
}
}
Please do help me, if you could. If you think any other solution, kindly let me know.
Thank you and Please,
Karsnen.
How about I use a cylindrical collider and then instruct the camera to follow the gameobject on CollisionExit and Not to follow the player on CollisionEnter.
Again I have optimization concerns, as it would be run throughout the entire game and the target platform is ios?
I'm having trouble understanding what you're trying to accomplish, could you draw a diagram or something visual?
Why are you recalculating this per frame? Why not create a disc mesh and just scale/translate it around?
Answer by DaveA · Jul 18, 2012 at 10:10 PM
Put this script on your camera (untested):
var radius = 5f;
var target : Transform; // drop your target here or assign it somewhere
function Update()
{
if ((target.position - transform.position).magnitude >= radius)
transform.LookAt (target);
}