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 Silvia · Dec 28, 2010 at 05:22 PM · camerafollowsmooth

Smoothly following a moving object

I want the main camera to follow a spaceship. I attached the following code to the camera:

void Update ()
{
// target is the spaceship's transform
    Vector3 wantedPosition = target.TransformPoint (0, height, distance);
    transform.position = Vector3.Lerp (transform.position, 
wantedPosition, Time.deltaTime * damping);
    // ... code for rotation...
    }

However, as the target (my spaceship) moves forward, the camera shakes unpleasantly, I believe due to the Lerp function. Is there a way to avoid this and just have the camera move the same way as the spaceship does? Thanks.

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

5 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Jesse Anders · Dec 28, 2010 at 07:57 PM

For the 'shaking' problem, try moving the code you posted from Update() to FixedUpdate().

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 Silvia · Dec 29, 2010 at 12:01 PM 0
Share

$$anonymous$$mh.. the problem is even worse now... Can it depend from my computer being not enough powerful?

avatar image
3

Answer by Ben Ezard · May 17, 2012 at 03:45 PM

You could just use this script that I wrote - it's not the fanciest of things but it gets the job done

I had the same problem with a spaceship shaking too

 var playerMarker : Transform;
 
 function LateUpdate () {
     transform.position = Vector3.Lerp(transform.position, playerMarker.position, Time.deltaTime * 100);
     transform.rotation = Quaternion.Lerp(transform.rotation, playerMarker.rotation, Time.deltaTime * 100);
 }
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 Jonesy19 · Jan 19, 2017 at 12:32 PM 0
Share

This script is simple and works wonderfully...Simple is better than fancy!!

avatar image
2

Answer by efge · Dec 28, 2010 at 05:31 PM

You could use the 'built-in' script "Smooth Follow".

Attach the script (Component/Camera-Control/Smooth Follow) to your camera and adjust the values in the inspector.

Comment
Add comment · Show 2 · 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 Silvia · Dec 28, 2010 at 05:48 PM 0
Share

I tried to adjust the parameters, but still I always get the camera inside the spaceship (which is not what I wanted: I would like a third-person point of view) and the shaking effect. Is there something I am missing?

avatar image Karsnen_2 · Nov 05, 2012 at 09:32 PM 0
Share

IN which asset can we find the Smooth Follow script?

avatar image
0

Answer by urawhat · May 18, 2012 at 12:26 AM

// A simple smooth follow camera, // that follows the targets forward direction

var target : Transform; var smooth = 0.3; var height = 0; var distance = 5.0; private var yVelocity = 0.0;

function Update () { // Damp angle from current y-angle towards target y-angle var yAngle : float = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y, yVelocity, smooth); // Position at the target var position : Vector3 = target.position; // Then offset by distance behind the new angle position += Quaternion.Euler(0+height, yAngle, 0) * Vector3 (0, 0, -distance); // Apply the position transform.position = position;

 // Look at the target
 transform.LookAt(target);

}

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 $$anonymous$$ · Jul 08, 2015 at 09:02 AM

Why don't you use Unity 5's built in Smooth Follow script from the docs? It has no lagging issues for a powerful PC, but if you have issues, use FixedUpdate() instead of Update().

 var target : Transform;
 var smoothTime = 0.3;
 private var velocity = Vector3.zero;
 
 function Update () 
 {
     // Define a target position above and behind the target transform
     var targetPosition : Vector3 = target.TransformPoint(Vector3(0, 5, -10));
         
     // Smoothly move the camera towards that target position
     transform.position = Vector3.SmoothDamp(transform.position, targetPosition, velocity, smoothTime);
 }

It will not work if you have another script to control camera movement of ANY kind, so this is best used for a 2D game.

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

Smooth Camera help 0 Answers

Smooth camera not really smooth 6 Answers

How to make a camera that follows more than one character? 1 Answer

How do I make a camera look for a specific GameObject while networking? 1 Answer

Making a camera that follows a rigidbodied sphere. 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