x


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.

more ▼

asked Jul 18 '12 at 06:02 PM

Karsnen gravatar image

Karsnen
753 28 47 57

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?

Jul 18 '12 at 06:37 PM Karsnen

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?

Jul 18 '12 at 07:23 PM Mortoc
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
}
more ▼

answered Jul 18 '12 at 10:10 PM

DaveA gravatar image

DaveA
26.8k 153 171 257

Thanks Dave. Testing it right away...

Jul 19 '12 at 02:15 PM Karsnen
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5275
x4376
x3131
x2176
x2028

asked: Jul 18 '12 at 06:02 PM

Seen: 458 times

Last Updated: Jul 19 '12 at 02:15 PM