- Home /
3rd person camera view lock
Hey,
So the player controls a tank with two rotating parts. The bottom and the turret. I would like to have the camera lock on to the back of the body of the tank and rotate exactly like the tank bottom does.
My current version does follow behind the tank but it does not rotate accordingly, there will be a lag to rotate. I would like it to rotate exactly when the body does. Any help would be much appreciated.
Can I assign the camera's position to the tank's position and just increase the height and distance of the camera to desired? If so, how do I do that?
My current code:
Vector3 wantedPosition = transform.TransformPoint(transform.position.x, transform``.position.y - 50, -distance);
mainCamera.transform.position = Vector3.Lerp(mainCamera.transform.position, wantedPosition, Time.deltaTime * 2.0f);
Quaternion wantedRotation = Quaternion.LookRotation(transform.position + mainCamera.transform.position, transform.up);
mainCamera.transform.rotation = Quaternion.Slerp(mainCamera.transform.rotation, wantedRotation, Time.deltaTime * 2.0f);
Answer by syclamoth · May 10, 2012 at 10:05 AM
Why not simply make the camera a child of the tank? The whole point of the interpolation thing is so that it doesn't just snap straight to the position, but if that's what you actually want then there's a much easier way to do it.
If you make the camera a transform child, then you can just manipulate
camTransform.localPosition = whatever;
to have the camera move up and down and back and forwards as you please.
I have two camera modes. The main one follows the movement of the tank but not the orientation(From an angled birds eye view) and the second mode should follow the orientation of the tank (stuck behind the tank). I'm not so good at C# and Unity so could you please explain further?
Edit Ok so I did this:
mainCamera.transform.position = transform.position;
mainCamera.transform.rotation = transform.rotation;
And the camera follows both the position and rotation of the tank however I want it to match the height and angle of my first camera such that when it switches it will smoothly transition.
Ins$$anonymous$$d of having it move immediately, use a coroutine to make the switch:
IEnumerator SwitchCam(Transform newTarget, float time)
{
float curTime = 0;
Transform startTrans = transform.parent;
transform.parent = null;
while(curTime < time)
{
transform.position = Vector3.Lerp(startTrans.position, newTarget.position, curTime / time);
transform.rotation= Quaternion.Slerp(startTrans.rotation, newTarget.rotation, curTime / time);
curTime += time.deltaTime;
yield return null;
}
transform.position = newTarget.position;
transform.rotation = newTarget.rotation;
transform.parent = newTarget;
}
Then, whenever you want to change the target transform, use
StartCoroutine(SwitchCam(nextTransform, timePeriod));
In case it isn't clear, I'm saying you should put your camera movement scripts on two empty transforms, and move the actual camera between those.
Hey firstly thanks for all your help guys. Ok I successfully made the camera follow the tank like I want however I have uneven terrain and the camera bobs at unnatural angles and I would like to to follow the rotation on only the Y axis but not the other two axis.
Code:
//Restricts Y position to 410 so bobbing $$anonymous$$imised Vector3 wantedPosition = transform.TransformPoint(0, height, -distance); mainCamera.transform.position = Vector3.Lerp(mainCamera.transform.position, wantedPosition, Time.deltaTime * 6.0f);
Vector3 temp = mainCamera.transform.position; temp.y = 410.0f; mainCamera.transform.position = temp;
Quaternion wantedRotation2 = Quaternion.LookRotation(transform.position - mainCamera.transform.position, transform.up);
Quaternion wantedRotation = Quaternion.Euler(52.0f, transform.rotation.y - mainCamera.transform.rotation.y, 0.0f);
mainCamera.transform.rotation = Quaternion.Slerp(mainCamera.transform.rotation, wantedRotation, Time.deltaTime * 20.0f);
Well now. Just make your camera follow the position of a child, but ins$$anonymous$$d of following rotation directly, use transform.LookAt(tankTransform) to manage rotation.
Answer by Calumcj · May 10, 2012 at 09:25 AM
Im not great at C# so im not to sure how it works, but, unity come with a built in cammra follow system, simply possition the Main Cammra in the possition you want it and set it to "smooth follow" then change the options int he Inspector panal to fit your liking :)
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
I need help with TPS controls! 0 Answers
Is there a way to lock my camera's rotation and movement on certain axis? 2 Answers
RTS Camera Rotation and Movement 0 Answers
Rotate object ON THE CAMERA UP AXIS 2 Answers