Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by djdrool91 · May 10, 2012 at 03:40 AM · c#camerarotationthird-persontank

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);
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image djdrool91 · May 11, 2012 at 01:59 AM 0
Share

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.

avatar image syclamoth · May 11, 2012 at 03:32 AM 0
Share

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.

avatar image djdrool91 · May 11, 2012 at 07:44 AM 0
Share

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);      
avatar image syclamoth · May 11, 2012 at 02:08 PM 0
Share

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.

avatar image
-1

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges