- Home /
Set Distance to object
Because of a own Camera-Rotation script I can't make my camera a child of the main character because that would mess up all things. So, I need to make my camera to set a specific distance to another transform. I can calculate the actual Distance but how can set this distance?
Have you tried the camera script that comes with unity, there's one that makes the camera to follow the player's character, check it out may be can help you.
Yeah, but I've made one that makes the camera rotate around the player if he uses the mouse and I would like to keep this effect :/
Answer by Owen-Reynolds · Aug 19, 2011 at 04:53 PM
Say you have the camera angle computed, along with your distance. Maybe the camera is currently facing that angle, in its own transform.rotation
or maybe you have it stored in Quaternion Ang;
. Either way, you can start with a "straight back" vector of that length, then twist to your angle (from memory; tested, but not recently):
// A rotation of "none" means you are aimed forwards, so need to pull straight back:
Vector3 toCam = -Vector3.forwards * PULL_BACK_DIST;
// Adjust for actual rotation:
// o if cam has correct facing, tilt toCam to local camera backwards:
toCam = transform.TransformDirection(toCam);
// o or, if you have cam angle stored elsewhere, just apply it:
toCam = Ang * toCam; // NOTE: angle*vector = vector twisted by angle
// toCam should now be correct xyz pullback, so move there:
transform.position = myTarget.position + toCam;
This even solved another problem I had a while ago^^ Thanks for helping