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
1
Question by Adam 10 · Jan 16, 2011 at 07:53 AM · cameradrag

How to have Camera following a Target, but not directly centered?

I am using the CameraScroll with the target as my player. I want the player to be able to move freely in the cameras view and only when he nears the edge of the camera's FOV would it then shift either up/down and left/right depending on what portion of the screen he moves in.

I saw in the 2D tortorial something about a springiness, that makes the camera lag which could be another solution. I played with the Drag variable, but this doesn't appear to be changeing anything.

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

3 Replies

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

Answer by jashan · Jan 16, 2011 at 01:56 PM

One possible solution is to keep a separate game object that is focussed by the camera (instead of directly focussing the player). You could then check the distance between this object ("focuspoint") and the player and when it exceeds a certain amount, lerp this object towards the exact player position (which will "move the camera").

As I think you're in a 2D environment, instead of calculating the real distance between the focuspoint and the player, you could just calculate distance on x and distance on y and also have separate threshold values (those are subject to tweaking for the best player experience). This will give you somewhat more precise control and also will cost a little less performance.

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
Best Answer

Answer by Adam 10 · Jan 17, 2011 at 09:30 PM

You are correct in that this is a top down 2D game. Thanks for the help.

I went with Jashan's solution and although this is a very simple version and I may want to play with a little more, it does do the trick. Like he said I created an empty game object called Focal Point and attached the script below. Jeston I see how your solution works as well and if this doesn't work then I may apply yours instead.

//Player Object or whatever you deem necessary var Object1 : GameObject; //Modified variable for if Ojbect1 and our Focal Point get too far from each other var threshold : int = 0; //private variable for distance private var dist;

function Start(){ //First let's start with our Camera Centered on our Player transform.position = Object1.transform.position; }

function Update () { //Check the distance to see how far the Object1 and Focal Point are dist = Vector3.Distance(transform.position, Object1.transform.position);

//If distance exceeds our Threshold then Lerp our Focal Point to new location if(dist >= threshold){

transform.position = Vector3.Lerp (transform.position,Object1.transform.position, Time.deltaTime); }

}

Now of course set you Object1 in the Inspector to be what you need free moving, in my case the player. Then you can play with the Threshold to see what works, I have it low 10 currently because as the player goes to the top of the screen I dont want him getting hit by enemy fire he cant see. I will have to play with this more I can already tell.

If anyone needs to know I just have the Camera Scrolling Script on the MainCamera gameobject with it set as the target.

On a side note, I think you could use the for a Fairy or Floating Gun Turrent to Follow your Player around the level, pretty cool.

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 Jeston 1 · Jan 16, 2011 at 03:30 PM

Make a script where the camera continuously goes to a destination point, then on your characters update set the destination point to somewhere behind him.

Depending on your code, the camera moving to the destination point will have some quick n cheap physics or fancier ones...

I recommend an exponential velocity based on distance to the point or a logrithmic decay

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

No one has followed this question yet.

Related Questions

Mouse/Wacom stylus drag not working. 1 Answer

The solution with camera rotation which is used in most strategic games 1 Answer

Dragging camera around object using mouse issue 0 Answers

Rotating camera with RMB drag 1 Answer

Drag an object which camera follows 1 Answer


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