1

When my character moves through my cameras 'FollowPlayer' script, the background just looks really glitchy and bad Is there anyway to fix this?

This is my script:

using UnityEngine;
using System.Collections;
public class FollowPlayer : MonoBehaviour {
    Transform bar;

    void Start() {
        bar = GameObject.Find("PlayerMovingBar").transform;
    }

    void Update() {
        transform.position = new Vector3(
            bar.position.x, transform.position.y, transform.position.z);
    }
}
Gnemlock
  • 5,263
  • 5
  • 28
  • 58
  • Can you describe the symptoms of "really glitchy and bad" in more detail? It's not obvious from your code what might be going wrong. Including an animated gif or a link to a short video of the problem can often help remove ambiguity when describing visual/animation artifacts. – DMGregory Aug 23 '17 at 16:29
  • Here is a really useful program for taking short screen recordings, and saving them as gifs. Take a recording of the background, and post it so we can see what you mean by "glitchy". As @DMGregory points out, you also want to describe "glitchy", to make your question understandable. – Gnemlock Aug 23 '17 at 23:52
  • You also want to have a look into "Smooth Follow"; you do not seem to be applying any smoothing, and thus, it is going to look odd. The default Unity packages contain an example script. – Gnemlock Aug 23 '17 at 23:53
  • well basically if im stationary the backround looks fine but right when I move it looks like the backround is moving up and down rapidly. I just want it smoother so it doesnt move up and down – shrookypranks Aug 25 '17 at 00:49

2 Answers2

1

Using Lerp will give a smooth following effect as @Hellium mentioned. It's also recommended to use it in the LateUpdate() method.

See Unity documentation for that : MonoBehaviour.LateUpdate()

Good luck on your project!

SAN ALexis
  • 11
  • 1
0

You may simply need to Lerp to the desired position :

public float followSpeed = 10 ; // Change the value here to have the desired result

void Update() {
    Vector3 targetPosition = new Vector3(bar.position.x, transform.position.y, transform.position.z);
    transform.position = Vector3.Lerp( transform.position, targetPosition, followSpeed * Time.deltaTime );
 }
Hellium
  • 2,949
  • 1
  • 11
  • 27