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 Orloffyeah · Jul 08, 2014 at 01:51 AM · camera2dfollow

Problem With 2D Camera Following Target

Hi, I'm having a problem with a camera script that I found. It is written in C# and it works perfectly except for one thing, when I run the game the camera places itself on top of the character, making it imposible to see anything except the background, how can I fix this problem?

Camera Script:

 using UnityEngine;
 using System.Collections;
 
 public class SmoothCamera: MonoBehaviour {
 
 public Transform target;
 public float smooth= 5.0f;
 
     void  Update ()
     {
         transform.position = Vector3.Lerp (transform.position, target.position, Time.deltaTime * smooth);
     } 
 
 }
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 14ercooper · Aug 07, 2015 at 09:56 AM

You can try to use a new Vector3 that places the camera farther away. Using this code inside of update should have the camera follow the player with the default Z.

 Vector3 targetPosMod = new Vector3 (target.position.x, target.position.y, target.position.z - 10);
 transform.position = Vector3.Lerp (transform.position, targetPosMod, Time.deltaTime * smooth);
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 Orloffyeah · Aug 07, 2015 at 11:53 PM 0
Share

Thanks for the reply, I've finished this project long time ago, exactly like you are suggesting. Forgot to post and answer and marking it as correct. :)

avatar image
1

Answer by IvovdMarel · Jul 08, 2014 at 02:17 AM

If you want your camera to follow your character (like in an RPG) it's easiest to create a gameobject called 'CameraTarget' and child it to your character. Move the target behind the character as far as you'd like your camera to be and then Lerp the camera towards that target. While lerping, you'll need to make sure your camera targets the character. (Use .LookAt)

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 Orloffyeah · Jul 08, 2014 at 04:51 PM 0
Share

Its not like in a third person game, is like in $$anonymous$$ario, platformers like that.

avatar image
1

Answer by OxDEADBAAD · Jul 08, 2014 at 03:04 AM

Your problem seems to be that you're moving the camera to the exact position of the player, when you should instead be moving it to its x and y coordinates but keeping your camera's z intact (assuming you're placing your things "the standard way" :P)

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 Orloffyeah · Jul 08, 2014 at 04:51 PM 0
Share

Yeah, the problem is that I don't find a way to make it "freeze" its z position.

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

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

Related Questions

camera follow problem-unity 4.3 2d 1 Answer

2D Camera Smooth follow, FixedUpdate and LateUpdate odd difference, help needed. 1 Answer

ortographic camera smooth follow with zoom 1 Answer

2d orthographic camera follow 1 Answer

Cinemachine 2d camera problem 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