Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Gamershaze · Feb 11, 2014 at 11:13 PM · playercamera-movementfolloworthographicfollow player

Orthographic (Birds-Eye View/Top-Down) Player Follow Script w/ Smoothing

To put it simply, I'm very much hoping someone can quickly write up a fairly simple follow script (with dampening). I've opened approximately forty seven links from the Google machine and downloaded/copy-pasted more examples than I can count of follow scripts, and they either fell under one of two issues. Either A, they required the attached object to have a camera, which it can't. Either that, or B, they followed the object on the wrong axis and whenever I'd try to correct said axis it'd just flip out.

Currently I've got my main camera (as well as some other depth cameras) parented under an empty object. Meanwhile, I have my player object which is somewhere else. Due to various more complicated reasons, I can't simply child the camera object to the player object.

I should also mention this is done via an orthographic camera pointed down at the play field (in a birds-eye/top-down view), so the object should only follow the player on the X and Z axis. It also needs dampening, or at least some sort of smoothing to target.

Below, are some example scripts that might be useful to anyone who finds this thread but is looking for something simpler; and why they won't work in this situation.

 var dampTime : float = 0.3; //offset from the viewport center to fix damping
 private var velocity = Vector3.zero;
 var target : Transform;
  
 function Update() {
     if(target) {
         var point : Vector3 = camera.WorldToViewportPoint(target.position);
         var delta : Vector3 = target.position -
                     camera.ViewportToWorldPoint(Vector3(0.5, 0.5, point.z));
         var destination : Vector3 = transform.position + delta;
         transform.position = Vector3.SmoothDamp(transform.position, destination, 
                                                 velocity, dampTime);
     }
 }
 
 // http://answers.unity3d.com/questions/29183/2d-camera-smooth-follow.html - Source

The above code will not work as it adjusts a camera entity, rather than an object. Considering we're dealing with multiple cameras, we need it to move the parent object which this script will not allow. Otherwise, this works perfectly fine.

 var cameraSpring = 3.0;
 var target : Transform;
 
 function Update () {
     var wantedPos = target.position;
 
     wantedPos.z = transform.position.z;
     transform.position = Vector3.Lerp (transform.position, wantedPos, Time.deltaTime * cameraSpring);
 
 }
 // http://forum.unity3d.com/threads/215179-Need-some-help-with-some-2D-Camera-following - Source

This code works perfectly, except in that it only follows on the Z axis. Not to mention, it doesn't have proper dampening. .

As always, I'm forever grateful Unity Answers. I realize this isn't a place to have everything done for you, but I can't get anywhere on my own for this one. Thank you.

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 · Feb 12, 2014 at 12:23 AM 0
Share

On the second script what does 'It doesn't have proper dampening' mean to you.

avatar image vbs · Dec 29, 2014 at 01:17 AM 0
Share

I ended up using robertbu's answer as well, however, this script as is positions the camera right above the player. I needed the view to be from a particular disatance, orthographic, to emulate a "Diablo/Starcraft" like 45deg orthographic view. So I positioned my player object at X = 0, Y = 0. Placed the camera in the editor to where I felt like it showed the player aprox. in the middle of the screen. and subtracted the X and Y from the goalPos in this script to get the effect I wanted.

This is my setup:

  using UnityEngine;
  using System.Collections;
 
  public class VBSCameraFollow : $$anonymous$$onoBehaviour {
 
      public Transform target;
      public float smoothTime = 0.3f;
 
      private Vector3 velocity = Vector3.zero;
 
      void Update () {
          Vector3 goalPos = target.position;
          goalPos.y = transform.position.y;
          goalPos.x = target.position.x - 350f;
          goalPos.z = target.position.z - 350f;
          transform.position = Vector3.SmoothDamp (transform.position, goalPos, ref velocity, smoothTime);
      }
  }


Player is at X:0, Y:0.

Camera is:

Position

  • X: -350

  • Y: 300

  • Z: -350

Rotation

  • X: 30

  • Y 45

  • Z: 0

Orthographic view

  • Size: 100

This gives me the same view in the editor as when I play the game.

2 Replies

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

Answer by robertbu · Feb 12, 2014 at 12:31 AM

This might be what you are looking for:

 using UnityEngine;
 using System.Collections;
 
 public class OrthoSmoothFollow : MonoBehaviour {
 
     public Transform target;
     public float smoothTime = 0.3f;
 
     private Vector3 velocity = Vector3.zero;
 
     void Update () {
         Vector3 goalPos = target.position;
         goalPos.y = transform.position.y;
         transform.position = Vector3.SmoothDamp (transform.position, goalPos, ref velocity, smoothTime);
     }
 }
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 Gamershaze · Feb 12, 2014 at 03:45 AM 0
Share

Yes, thank you very much.

avatar image hosfordryan · Sep 16, 2015 at 07:09 PM 1
Share

When I do this, the player that is moving is quite choppy. Almost like it is running into an invisible wall and then it runs through it. When I disable to camera follow script it moves smoothly.

How can I fix this?

avatar image
1

Answer by devotionsolutions · Dec 11, 2015 at 12:35 PM

This script would run better if you use FixedUpdate instead of Update()

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 SignalZak · Nov 03, 2016 at 08:53 PM 1
Share

FIxedUpdate is intended for sychronizing physics updates and not rendering updates. Since the camera is tied to rendering, you don't benefit from using FixedUpdate (and since FixedUpdate could run multiple times between a regular Update, you could actually hurt performance).

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

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

Related Questions

How do I make an object follow the player? 1 Answer

Follow after height 1 Answer

How can I make the CarController (a gameobject) follow my currentCar 0 Answers

Camera rotation around player while following. 6 Answers

How do i get a camera to follow a Bird/Plane smoothly? 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