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
1
Question by theUndeadEmo · Apr 01, 2012 at 11:19 PM · z-axissmoothfollow

Smooth Follow script and z axis

hello i would like for the camera to smooth follow on the z axis aswell as the other axises whilst looking at my spaceship from a certain distance. The other axis works fines but z is causing me difficulties

 var target : Transform;
 var distance = 5.0;
 var height = 5.0;
 var heightDamping = 2.0;
 var rotationDamping = 3.0;

 @script AddComponentMenu("Camera-Control/Smooth Follow")


 function LateUpdate () {
 // Early out if we don't have a target
 if (!target)
     return;
 
 // Calculate the current rotation angles
 var wantedRotationAngleY = target.eulerAngles.y;
 var currentRotationAngleY = transform.eulerAngles.y;
 
 var wantedRotationAngleX = target.eulerAngles.x;
 var currentRotationAngleX = transform.eulerAngles.x;

 var wantedRotationAngleZ = target.eulerAngles.z;
 var currentRotationAngleZ = transform.eulerAngles.z;
 
 // Damp the rotation around the y-axis
 currentRotationAngleY = Mathf.LerpAngle (currentRotationAngleY, wantedRotationAngleY, rotationDamping * Time.deltaTime);
 currentRotationAngleX = Mathf.LerpAngle (currentRotationAngleX, wantedRotationAngleX, rotationDamping * Time.deltaTime);
 currentRotationAngleZ = Mathf.LerpAngle (currentRotationAngleZ, wantedRotationAngleZ, rotationDamping * Time.deltaTime);

 // Damp the height
 //currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

 // Convert the angle into a rotation
 var currentRotation = Quaternion.Euler (currentRotationAngleX, currentRotationAngleY, currentRotationAngleZ);
 
 // Set the position of the camera on the x-z plane to:
 // distance meters behind the target
 transform.position = target.position;
 transform.position -= currentRotation * Vector3.forward * distance;
 transform.rotation= currentRotation ;


 
 // Always look at the target
 transform.LookAt (target);

}

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

3 Replies

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

Answer by theUndeadEmo · Apr 02, 2012 at 10:51 AM

problem solved.

this was causing the problem from the above code

 transform.LookAt (target);

i am assuming it always makes you look at the target at an exact z rotation

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
avatar image
0

Answer by Posly · Apr 02, 2012 at 12:45 AM

I tried creating a camera like this before but I also got stumped on the Z-axis, but there's a script called "SmoothFollow" in the Standard Assets folder that does the trick!

Comment
Add comment · Show 1 · 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 theUndeadEmo · Apr 02, 2012 at 10:05 AM 0
Share

this is that script, it doen't let you have a z rotaion

avatar image
0

Answer by Harabeck · Apr 05, 2012 at 09:42 PM

Just modifying the original script like that doesn't allow you to properly set height anymore, and the camera freaks out at certain angles because they're using the euler angles. Here's my solution:

             //Get relevant rotations
         Quaternion currentRotation=transform.rotation;
         Quaternion wantedRotation=target.rotation;
         
         //Lerp to new rotation
         Quaternion newRotation=Quaternion.Lerp(currentRotation, wantedRotation, rotationDamping*Time.deltaTime);
         
         //set rotation
         transform.rotation=newRotation;
         
         //set position: back distance, up height
         transform.position=target.position-(transform.forward*distance)+(target.up*height);
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Smooth tail followscript 0 Answers

SmoothFollow script issue for multiplayer car game 0 Answers

camera smooth follow the targets after touch buttons 0 Answers

How do I change a TextMesh's Offset Z value in JavaScript? 1 Answer

How to rotate a gameObject towards a hit point along Z axis? 1 Answer


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