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 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.

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

Smooth camera on moving platform 1 Answer

How would I go about making a camera system for a game akin to the starfighter mode of Battlefront 2? 1 Answer

3D Platformer Camera 0 Answers

How to make a camera Follow an Object moving in zigzag path? 1 Answer

How to change the focus of the camera? 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