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 kingsany · Jan 28, 2014 at 02:44 PM · object follow path

2D Camera following Distance

Hey guys,

I am struggling, with a little problem that I have: I started a 2D-Game, where the Camera follows my Character on the X and Y-Axis. But my problem is, the character is to close to the camera and I want adjust a defined distance. I looked on different tutorials, but nothing worked for me so far.

Here is my original following code

 var Target : Transform;
 public static var gameRunning : boolean = true;
 
 function Update () {
 
 if(gameRunning){
     transform.position.x = Target.position.x;
     transform.position.y = Target.position.y;
 }
 }

This is what I tried:

 var distance : float = -200f;
 var Target : Transform;
 public static var gameRunning : boolean = true;
 
 function Update () {
 
 if(gameRunning){
     transform.position.x = Target.position.x;
     transform.position.y = Target.position.y;
     transform.position.z = distance;
 }
 
 }

Hope you can help me with that.

Comment
Add comment · Show 5
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 ucfblythe83 · Jan 28, 2014 at 03:54 PM 0
Share

To start you really need to give us some more information on what you are trying to accomplish and what problem you are running into. But in the mean time here are just some friendly tips.

I'm assu$$anonymous$$g this code is attached to your camera, but is this a child of another GameObject or a standalone? If this script is attached to a child GameObject then you need to modify transform.localPosition ins$$anonymous$$d. Also, you generally don't set position one value at a time (for the most part these individual values are read only) so it's best practice to do something like this:

`transform.position = Vector3(Target.position.x,Target.position.y,distance)`

I also noticed you are using a gameRunning Boolean to control your Update function. For the most part you want to stay away from booleans that interrupt your update, there are a number of more efficient ways of pausing your game than this. Doing it this way would likely require you to continue checking if the game is running in all your script update functions which just becomes a mess.

Anyway, just some friendly tips. Post some more info on your current problem and I'll see if I can help you out :)

avatar image kingsany · Jan 28, 2014 at 04:46 PM 0
Share

Thanks for your answer.

Well, yeah this script is attached to my main camera and it gets the current position information from my character. I tried to replace my transform with yours, but I have still the same problem.

I don't know if you understood my problem. Basically I want to move the camera in z-position but it is staying still to close to my character.

avatar image ucfblythe83 · Jan 28, 2014 at 04:55 PM 0
Share

Well I understand your problem. You are just trying to move your camera closer to this object you are following on X and Y. However, we need to know key pieces of info like "this is an orthographic camera", since at that point moving the camera closer would do nothing. You would ins$$anonymous$$d have to change the Size of the camera ;)

avatar image kingsany · Jan 28, 2014 at 05:10 PM 0
Share

Oh thank you. You just solved my problem, by asking me which camera type i am using. ;) Thanks a lot ;)

avatar image ucfblythe83 · Jan 28, 2014 at 05:15 PM 0
Share

No Prob :) I'm just going to type this up as an answer so anyone looking can find it easier.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ucfblythe83 · Jan 28, 2014 at 05:43 PM

The OP was using a orthographic camera along with his 2D Scene. Moving an orthographic camera only changes position not how objects are viewed. Instead, changing the Size of an orthographic camera will allow you to 'zoom' to the target object.

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

19 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

Related Questions

Physics2D and curved movement 1 Answer

Companion follow and attack? 0 Answers

How can I get an object to orientate behind a plane? 0 Answers

How to make an object follow another object in a certain way 1 Answer

How do I get a script to make a character or object follow your player? 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