• 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 adit1 · Feb 13, 2013 at 12:21 PM · camera2dplatformer

2d camera along fixed path

I'm working on a 2d platformer. I want to implement a camera that moves along a fixed path based on player's movement. Does the array works? or anyone has a better solution to achieve this? I'm really stuck at this camera. I'm running out of ideas. Any ideas?

Comment

People who like this

0 Show 3
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 moghes · Feb 13, 2013 at 02:09 PM 0
Share

Where you need the array? Once I worked a 2d platformer on flash. You can leave the camera its place, have the character running at the same place, and just move the map backwards.

OR there's a script in the standard assets called "SmoothFollow", but havn't tryed for a 2d game, it might need some modifications.

avatar image EHogger · Feb 13, 2013 at 02:41 PM 0
Share

Is there a particular reason you want a fixed path rather than just follows alongside the player? A side scrolling version of the smoothfollow camera script is very simple to make (I'll give you code if needed).

avatar image adit1 · Feb 13, 2013 at 05:01 PM 0
Share

I dont want to follow around player every frame and also this is making my camera to show unwanted places. So I want it to be fixed path rather than basic one. I'm interested to see your approach.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by EHogger · Feb 13, 2013 at 08:45 PM

I've considered doing something similar myself. Here's a quick idea for it using triggers, but I can't promise this is optimal. It's just the simplest way I could think of.

First, I created a few triggers around the scene where your player will move through. I just used empty gameobjects with box colliders attached. (make sure to set "Is Trigger" in the inspector)

Then, I created some empty gameobjects at the positions you want the camera to move to.

On the triggers I added this small script:

 using UnityEngine;
 using System.Collections;
 
 public class CameraTrigger : MonoBehaviour {
 public GameObject camera;
 public Transform cameraTarget;
 
     void OnTriggerEnter(Collider other) {
         camera.GetComponent<CameraFollow>().target = cameraTarget;
     }    
     
 }

In the inspector for each one of your triggers, put your main camera in as the "camera" variable, and put one of the camera position objects you made as the "cameraTarget".

Finally, attach this script to your main camera:

         using UnityEngine;
         using System.Collections;
         
         public class CameraFollow : MonoBehaviour {
         
             public float cameraSpeed = 2 ; //the speed of the camera movement
             public Transform target;
     
         void Start () {
             target = transform;
         }
             
         void LateUpdate () {
             transform.position = Vector3.Lerp (transform.position, target.position, time.DeltaTime * cameraSpeed);
         }
     }

And that's it!

Good luck. Let me know if this works for you.

Comment

People who like this

0 Show 9 · 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 adit1 · Feb 14, 2013 at 12:33 AM 0
Share

I did same as above but the target is not changing, the trigger works fine, somehow it is not changing to cameraTarget.

avatar image EHogger · Feb 14, 2013 at 01:26 AM 0
Share

Check the script names. If you copy and pasted this into your own camera script then youll need to change the camera.GetComponent line on the trigger to make it point at your camera script.

avatar image adit1 · Feb 14, 2013 at 01:33 AM 0
Share

I did changed it, Its going into OnTrigger function( I had boolean to check in inspector), but the target is not changing to cameraTarget. Looks like I'm missing something.

avatar image EHogger · Feb 14, 2013 at 01:45 AM 0
Share

I'm not sure what to suggest apart from making new scripts with the names CameraTrigger and CameraFollow and copy/paste this exact code. It works on mine, so if you did the other parts right the only thing that I can imagine going wrong is the script name.

avatar image adit1 · Feb 14, 2013 at 01:51 AM 0
Share

It is working now! I did camera = GameObject.FindWithTag("MainCamera"); instead of changing game object in inspector. Your way of changing game object strangely not worked for me. Thank you! I owe you one.

Show more comments

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 on June 13. 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

12 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

Related Questions

Free Look Camera Pivot 1 Answer

2D Platformer Demo Camera Follow script doesn't follow player 1 Answer

Cinemachine camera weird movement on Windows build 0 Answers

ScreenToWorldPoint not accurate 0 Answers

Lock main camera on x-axis 2 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