- Home /
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?
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.
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).
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.
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.
I did same as above but the target is not changing, the trigger works fine, somehow it is not changing to cameraTarget.
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.
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.
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 $$anonymous$$e, so if you did the other parts right the only thing that I can imagine going wrong is the script name.
It is working now! I did camera = GameObject.FindWithTag("$$anonymous$$ainCamera"); ins$$anonymous$$d of changing game object in inspector. Your way of changing game object strangely not worked for me. Thank you! I owe you one.
Your answer