- Home /
Simple camera question -- follow rotation of object
How can I get my camera to follow this object's rotation (about y) as well as position?
The object is three cylinders (two wheels and an axle) connected by joints. All three cylinders are children of an empty parent Gameobject called 'Segway'. Intuitively, the orientation of the object is the orientation of the axle (ignoring the fact that it's spinning).
The problem with making the camera a child of Segway is that Segway's rotation doesn't change when the compound object it contains rolls around. The problem with making the camera a child of one of the cylinders (wheel or axle) is that the cylinders rotate like crazy.
Answer by robertbu · Dec 31, 2013 at 01:37 AM
I'm going to assume that the axle is constructed from a cylinder and therefore the Transform.up of the axle will be along the axis of the cylinder. With that assertion, here is a bit of code that will cause the camera to follow and rotate to the axle:
#pragma strict
public var axle : Transform;
public var camHeight = 2.0;
public var camDist = 4.0;
function LateUpdate() {
var v = Vector3.Cross(axle.up, Vector3.up) * camDist;
v.y += camHeight;
transform.position = axle.position + v;
transform.LookAt(axel);
}
'camDist' is the distance behind the axle to follow. 'camHeight' is the height to place the camera above the axle. You will need to initialize 'axle' either by dragging and dropping in the Inspector, or you can use something like GameObject.Find() or GameObject.FindWithTag() in the Start() to initialize the variable. I may have the two parameters in the Vector3.Cross() reversed.
Thanks very much for this. I tried this on a new camera in the scene and it's a bit closer to what I'm after, but not quite. I don't want the camera to actually look down at the axel, but just to copy the axle's rotation around y. So when camHeight and camDist are very small, I want the camera to be looking at the horizon and not down at the ground. $$anonymous$$y fault for not being clearer.
I was trying to keep my question above as simple as possible, but I should say a little more about what I'm up to be clearer. $$anonymous$$y object is a very basic Segway-type vehicle that I want to use for moving around a scene. You can see from the tiny animated gif in my original post how it moves. I am using a prefab camera with its own controller script from Oculus VR. This camera is set up to track the rotation of a head-mounted display (allowing the user to look around the scene). Since I don't want to interfere with how this prefab camera works, I'm sticking it inside of an empty Gameobject called 'CameraEmpty', and I'm controlling it with the following script:
using System.Collections;
public class CameraEmptyController : $$anonymous$$onoBehaviour {
public GameObject segway;
private Vector3 offset;
void Start () {
offset = transform.position;
}
void LateUpdate () {
transform.position = segway.transform.position + offset;
}
}
That's what I have so far. The camera moves around the scene with the segway, but is in a fixed orientation. I've tried rather futilely to find the right bit of code to read off the y rotation from the axle and set it as the y rotation of the CameraEmpty (the empty object containing the special Oculus VR camera). The attempts below (within the controller script attached to CameraEmpty) obviously failed, but you can see what I'm getting at:
// transform.rotation.y = segway.transform.rotation.y;
// transform.eulerAngles = Vector3(0, segway.eulerAngles.y, 0);
// transform.rotation = Quaternion.Euler(segway.transform.rotation.y);
Try this and let me know how it works for you:
#pragma strict
public var axle : Transform;
public var camHeight = 2.0;
public var camDist = 4.0;
function LateUpdate() {
var v = Vector3.Cross(axle.up, Vector3.up) * camDist;
var lookDir = v;
v.y += camHeight;
transform.position = axle.position + v;
transform.rotation = Quaternion.LookRotation(lookdir);
}
Note I could help you calculate the rotation for your existing code, but I don't think that is what you need. If the 'segway' object is not rotating, then the position will be wrong.
Robert, you're a gentleman and a scholar. This is exactly what I wanted. Works straight off with a standard camera within my empty Gameobject (to which I've attached your script). The OVR camera was a little trickier (it still had fixed orientation), but its own controlling script comes with a 'Follow Orientation' public variable, to which I've assigned the empty Gamobject containing it (which in turn is controlled by your script).
As soon as I get voting rights I'll make sure I vote up your answer. $$anonymous$$any thanks.
Your answer
Follow this Question
Related Questions
[Solved :D] Camera auto turns 180° from its init position on Play (Animated GIF Included!) 1 Answer
Rotate an object around another on input 0 Answers
Rotation seems to pass through condition checks 1 Answer
I can not set a orientation in a build setting. 2 Answers
How to make my player look in the direction of its movement? 3 Answers