# Help! Camera x,z plane movement

I’ve been busting my brains trying to figure out how to adjust my script, but I’m just not savvy enough yet. Can someone help me out? I’ve got a character doing a dance and I want my camera to be able to swing around him in a circle, maintaining focus on the character. I’ve got this so far and it works to a point:

################################
Camera Follow (this script keeps my moving camera pointed at the character) This one works fine!:

var target : Transform;
function Update () {
transform.LookAt(target);
}
################################

Camera move (this script moves my camera using the traditional arrow keys - forward, back and side to side):

var speed = 5.0;

function Update () {
var x = Input.GetAxis(“Horizontal”) * Time.deltaTime * speed;
var z = Input.GetAxis(“Vertical”) * Time.deltaTime * speed;
transform.Translate (x, 0, z);
}
################################

So here’s the problem with the camera movement script. Moving side to side gives me a great circle around the dancing character. Moving forward and back works as expected BUT, I don’t want my forward and back motion to follow the angle of the camera. Right now I’m about 2’ above the character looking down. I want to be able to move forward and back on the x,z plane without following the camera angle so I can prevent myself from going through the floor or the roof. Can I limit the distance of movement from the character? Like: camera can only move within a certain proximity of the character (i.e., no closer than 2", no further than 5’?)

Lastly, what do I need to do to my floors and walls to make it so that my camera doesn’t go through them?

Any help is GREATLY appreciated! I think it’s a simple solution, I just don’t know enough key words and programming terms to get through it.

I am not quite sure I understand what you are trying to accomplish, but I will take a shot anyway.
if you don’t want the motion to follow the angle of the camera you can create an empty object and attach the camera to this empty object (make the camera a child of the empty object). Then attach the your first script below to the camera and the second script to the empty object.

Sorry, I don’t know how this can be achieved. Some ideas:
Attach some collision geometry to your camera and check for collisions
Use Raycasting

I learned that if you make the floors and walls a rigidbody and take off the gravity setting, you will stop the camera from going through them.

Didn’t see if you attached a script or if you were referring to my scripts. I only had one script that controlled the movement of the camera. There was also a follow script that forced the camera to look at my character. What I think I might try is to have a key press control the up and down motion. If I find it, I’ll post it here!

OK, so I’ve been scouring Unity3D for scripts. I can not find a clear cut answer. My camera is in a room with an animated character. I can use left and right arrows to move in a circle around the character because the camera has a lookAt script on the character. Because I’m slightly above the character, when I move forward and back, it moves on the camera’s axis or angle that it is pointing. The problem, is that very quickly, the camera tries to go through the floor. I need to be able to move the camera on the x,z plane relative to the world vs. the camera.

See above posts to see the script I’m using. I need something in there that either a) restricts camera forward and back movement on the x,z plane OR b) allows me to set a key press (say U and J) to change my camera angle up and down.

Not a programmer, so any help you can give will be helpful. And to put it into the context of the scripts in my previous posts would be great!

I was referring to your scripts.
Create an empty object and attach to it:

``````function Update () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate (x, 0, z);
}
``````

make your camera a child of the empty object and attach the script:

``````var target : Transform;
function Update () {
transform.LookAt(target);
}
``````

to the camera.
The empty object does not rotate, so it will be moving in the x,z plane

you can also just translate the camera in world space when moving back and forward. Haven’t tested it but the below should work:

``````transform.Translate(0,0,z, Space.World);
``````

That works better, but I’m not quite there.

The Space.World call allows me to keep a good distance from the character as I’m zooming past. However, as soon as I go past the character, my controls are all backward. Here’s what happens.

I start off facing the character and I push forward with the up arrow. As soon as I move past the character, my camera behaves as needed, it turns to face the character. But now, the up arrow key is continuing to move me “forward” past the character vs. back the other way (the in which the camera is facing). I think it’s because I’m in the spacial environment vs. in relation to the character. Same goes with the left and right. If I’ve moved past the character, even though I’m facing the other other direction, my left and right arrows are now reversed in functionality.

Any ideas on a fix there?

By the way. I did attach the camera to an EmptyObject and assigned the move script to the EmptyObject and the follow script to the camera. Same issues arose with regards to the arrow keys being reversed once I moved past my character. However, it was a good answer to keeping my camera on the x,z plane!

So it looks like I have to think of the camera like the one that dangles over the NFL fields. Up arrow is always one direction down the field and the down arrow is the other, regardless of which way the camera is facing. Same with left and right. Probably better to think of them in terms of North, East, West and South.

I think you should be able to change the direction according to the way the camera is facing if you want to though and still stay in the x,z plane. I think you can take the x vector in the local coords of the camera, transform it to world coordinates with “transform.TransformPoint” and then take the only the x,z part and set the y part to 0… I hope this makes sense. If not, I can try to write you an example when I get home.

A sample would be most helpful! I am a programming newbie, but am catching on quick. It’s the application of some of those terms that gets me. An example would be great!! Thanks in advance!

sorry mate, but this will not work quite as I thought it would. The problem being that there is the case when you face directly above the object (dancing figure). Where do you move in this case? The idea was to take the projection of the local (rotated z unit vector) of the camera and project it to the x,z plane and move in the direction of the projection. However, when your camera is directly above the object this projection is 0, so you stay still.
Anyway, I don’t think that you would like to have the camera moving in the direction it is facing (even restricted in the x,z plane) because of the problem I just mentioned. It would be confusing for the user which way is forward/backward once the cam is above the object.

That’s too bad. So would the only option in this case be to have a regular First Person controller in the view finder? The only reason I don’t like that idea is because there are GUI buttons that control playback. And when you move the mouse down to the GUI elements, so goes the camera.