• 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 paulaceccon · Nov 06, 2013 at 11:00 PM · cameracamera followcamera movement

Camera Follow a Target, but not Centered on it

Hi, I'm doing a game inspired on Xonix to improve some skills and learn more about Unity3D.

I'm trying to do an script so that my camera follows a target, but not directly centered on it, and allowing the player to have a good perception of the grid when he is situated at the end of the grid, so he would know if there is an enemy when he comes back.

Something like in this game:

Camera Follow - AirXonix

Here, in my opinion: - the camera has a good distance from the player, it's not centered on him (i.e., the player is not always in the middle of the screen); - the player can know if there is an enemy when he is in the end of the grid and he decides to move to the begin of the grid; - the camera has a dynamic distance from the player (small distance: player in the first line of the grid; large distance: player in the lest camera of the grid).

So, this is what I'm trying to do. I've tried some approaches and now I have this script:

 public class CameraFollow : MonoBehaviour
 {
     // The distance that the camera will keep from the target.
     Vector3 distanceFromTarget = new Vector3 (0f, 13f, -10f);
     
     // The target.
     [SerializeField]
     Transform Target;
     
     // A thereshold to define when the camera has to move.
     int threshold = 10;
     
     // The time to accomplish the movement.
     float journeyTime = 12.0F;
     
     // The time when the camera starts to follow the target.
     float startTime;
     
     // The camera is already following the target?
     bool following = false;
     
     void Start ()
     { 
         //First let's start with our Camera Centered on our Player 
         transform.position = Target.position + distanceFromTarget;
     }
     
     void Update ()
     {
         float dist = Vector3.Distance (this.transform.position, Target.transform.position);
         
         if (dist >= threshold && !following) {
             StartCoroutine (FollowPlayer ());
         }    
     }
     
     private IEnumerator FollowPlayer () 
     {
         following = true;
         startTime = Time.time;
         float fracComplete;
         float dist;
         do
         {
             fracComplete = (Time.time - startTime) / journeyTime;
             dist = Vector3.Distance (Target.position, this.transform.position);
             this.transform.position = Vector3.Lerp (this.transform.position, Target.position + distanceFromTarget, fracComplete);
             yield return new WaitForEndOfFrame();
         }
         while (dist > 0.1f);    
         following = false;
     }
 }

But with this script my camera is always centered in the player, I don't consider that the player can have a good perception of what is behind him when he is in the end of the grid and I guess I should change the camera distance from the player dynamically in some way, 'cause until now, when the player is in the first line of the grid he camera is too far from him.

Any suggestion so I could achieve this would be appreciated.

Thanks in advance.

Comment
Add comment · Show 1
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 MarkD · Dec 08, 2013 at 09:11 PM 0
Share

You could also to have a more smooth effect create a lerp of the rotation, based on old rotation and new rotation. I use this for fps arms and other objects to follow smoothly, this can easily be configured to give your desired result.

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by MarkD · Nov 06, 2013 at 11:16 PM

One of the things you could do, is for every unit your character rotates, make you camera double that value, so it will always over rotate a bit, this way it will never be centered and feel more dynamic.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

16 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

Related Questions

FPS cam for Roll -a-Ball like game 1 Answer

[SOLVED] Third Person Spherically Orbiting Camera 1 Answer

Help make camera zoom smoother 3 Answers

3D Platformer Camera 0 Answers

Trying to follow tutorial to set up Multiplayer camera follow on UNET... and it goes wrong. 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges