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

People who like this

0 Show 0
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

  • Sort: 
avatar image
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
Orloffyeah

People who like this

1 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

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
Orloffyeah

People who like this

1 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 Mario, platformers like that.

avatar image

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
Orloffyeah

People who like this

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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Why does the camera smoothly follow in one direction but not the other? 0 Answers

Smooth Camera 2D Follow Player 0 Answers

Camera script 1 Answer

Camera 2d following player with delay jumps 1 Answer

Shaky player effect when I apply a damping camera follow script? 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