- Home /
Follow Player in 2D Game
At present I am working on 2d side scrolling game. I want camera follow for player movement only in horizontal movement. Right now I have written this type of target following script
public class SmoothFollow : MonoBehaviour
{
public Transform target;
public float smoothness = 10f;
private Vector3 targetInitialPos;
void OnEnable ()
{
target = null;
}
void Update ()
{
if (!target) {
target = GameObject.Find ("MrJump").transform;
targetInitialPos = target.position;
} else {
Vector3 cameraPosition = target.position - targetInitialPos;
cameraPosition.y = 0f;
cameraPosition.z = -10f;
// transform.position = cameraPosition;
transform.position = Vector3.Lerp (transform.position, cameraPosition, Time.deltaTime * smoothness);
}
}
}
If I directly apply cameraPosition to camera transform then it stuttering in mobile devices. So that I used lerp for this purpose but didn't got success in that also. I have attached small video also to represent my problem correctly.
Using above code it shows jerky movement of camera. Not smoother one that I want. Please give some suggestion in this.
mm... and why did you comment out the 20th line ins$$anonymous$$d of 18th?
Because doesn't notice any changes in output. Still noticing jerky camera movement.
Answer by Surprisejedi · Mar 31, 2015 at 10:28 PM
Ahhh, I had this problem too! But I found the solution but very sadly it was in 3d. But I may be able to help you. First make sure that the smooth follow target is the player. Second make sure the distance is 0 and that's what helped me! Hope it helped you!
Answer by Surprisejedi · Mar 31, 2015 at 10:28 PM
And if that doesn't help than add mouse orbit. If your game is based on going upwards make sure that Max and Min x is 0 or if it is horizontal than make Max and Min y 0. Hope all my advice helped! :)
Your answer
Follow this Question
Related Questions
How to make an camera offsets in Unity Smooth Follow Standard Assets? 0 Answers
Camera not following player.,Camera not following player... 1 Answer
Camera smooth follow behaviour 0 Answers
Camera smoothing of a Child Camera 1 Answer
camera problem 0 Answers