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 mystery_planet · Mar 23, 2014 at 09:57 PM · smoothfollow

Camera SmoothFollow place mesh not in the center

Hi,

I am using SmoothFollow camera. Right now I modified it so that the person is not in the center but at the center-bottom. I want to make a person appear in the bottom-left. Please, could you tell me how to modify SmoothFollow more.

 ![// The target we are following
 var target : Transform;
 // The distance in the x-z plane to the target
 var distance = 10.0;
 // the height we want the camera to be above the target
 var height = 5.0;
 // How much we 
 var heightDamping = 2.0;
 var rotationDamping = 3.0;
 
 // Place the script in the Camera-Control group in the component menu
 @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 wantedRotationAngle = target.eulerAngles.y;
     var wantedHeight = target.position.y + height;
         
     var currentRotationAngle = transform.eulerAngles.y;
     var currentHeight = transform.position.y;
     var screenPosition = new Vector3(0,10,0);
     // Damp the rotation around the y-axis
     currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
 
     // Damp the height
     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
 
     // Convert the angle into a rotation
     var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
     
     // 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;
 
     // Set the height of the camera
     transform.position.y = currentHeight;
     
     // Always look at the target
     var cameraTarget= target.position + screenPosition;
     transform.LookAt (cameraTarget);
 }][1]

Thank you. I attach a picture.

[1]: /storage/temp/24133-camerafollow.png

camerafollow.png (20.6 kB)
Comment
Add comment · Show 2
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 robertbu · Mar 23, 2014 at 11:52 PM 0
Share

The easiest solution is to make a child empty game object of the character. Place the child to the right and above the parent (visible character) object. Have the camera follow the child.

avatar image mystery_planet · Mar 24, 2014 at 12:53 AM 0
Share

Thank you. I feel like there should be some way to do it in the code

1 Reply

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

Answer by robertbu · Mar 24, 2014 at 01:32 AM

If you really want to do it in code, an offset can be used and added to the target position. Here is a quick rewrite of the standard SmoothFollow.js script with that change. Set the value of 'offset' in the Inspector. Positive value for x and y will move the object to the lower left.

 // The target we are following
 var target : Transform;
 // The distance in the x-z plane to the target
 var distance = 10.0;
 // the height we want the camera to be above the target
 var height = 5.0;
 // How much we 
 var heightDamping = 2.0;
 var rotationDamping = 3.0;
 
 var offset : Vector3 = Vector3.zero;
 
 // Place the script in the Camera-Control group in the component menu
 @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 wantedRotationAngle = target.eulerAngles.y;    
     var pos = target.position + Quaternion.AngleAxis(wantedRotationAngle, Vector3.up) * offset;
     var wantedHeight = height + pos.y;
 
         
     var currentRotationAngle = transform.eulerAngles.y;
     var currentHeight = transform.position.y;
     
     // Damp the rotation around the y-axis
     currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
 
     // Damp the height
     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
 
     // Convert the angle into a rotation
     var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
     
     // Set the position of the camera on the x-z plane to:
     // distance meters behind the target
     transform.position = pos;
     transform.position -= currentRotation * Vector3.forward * distance;
 
     // Set the height of the camera
     transform.position.y = currentHeight;
     
     // Always look at the target
     transform.LookAt (pos);
 }
Comment
Add comment · Show 5 · 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 mystery_planet · Mar 24, 2014 at 02:21 AM 0
Share

Thank you for the answer. When I test this script it works fine when my person moves horizontally. But the camera misses the person sometimes when the movement is vertical (45 degrees). I used Offsets x=15, y=10, z=0

avatar image robertbu · Mar 24, 2014 at 03:34 AM 0
Share

I edited my script and fixed that problem plus fixed some problems for 3D movement.

avatar image mystery_planet · Mar 24, 2014 at 03:51 AM 0
Share

Thank you! This works much better, but there is still one thing. So now when it goes downwards (at any angle, ex:45 degrees), then it misses the person because for some reason the camera zooms in. So the camera zooms in when the cart goes downwards and this leaves the person out of the frame.

avatar image robertbu · Mar 24, 2014 at 05:08 AM 0
Share

I changed the script.

avatar image mystery_planet · Mar 25, 2014 at 12:55 AM 0
Share

That fixed it! Thank you very-very much!

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

20 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

wrong localScale on instantiated object unity 0 Answers

Networking When Offline 1 Answer

Noobish Question Pertaining to JS Movement 2 Answers

How to read "mp3" or "other Audio" file from SDcard in runtime 0 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