1
\$\begingroup\$

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);
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Aug 23, 2017 at 16:29
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Aug 23, 2017 at 23:52
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Aug 23, 2017 at 23:53
  • \$\begingroup\$ 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 \$\endgroup\$ Commented Aug 25, 2017 at 0:49

2 Answers 2

1
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

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 );
 }
\$\endgroup\$
5
  • \$\begingroup\$ when I add that to my script the pubic followspeed turns red and the follow speed in the other part of it \$\endgroup\$ Commented Aug 23, 2017 at 17:35
  • \$\begingroup\$ Yes, indeed, sorry, I have forgotten the type of the variable.... \$\endgroup\$ Commented Aug 23, 2017 at 18:41
  • \$\begingroup\$ well I added that to the script and it still seems like the backround is jumping around and moving when I move \$\endgroup\$ Commented Aug 23, 2017 at 18:53
  • \$\begingroup\$ The problem may not come from the script then. Please, provide additional information about your problem (gif or video) and the setup of your scene. \$\endgroup\$ Commented Aug 23, 2017 at 19:31
  • \$\begingroup\$ The deltaTime correction you've applied is not correct for the exponential ease-in style of Lerp you're using, which can cause the scene to judder on a variable framerate. You can use the formula for weight in this answer to correct it. \$\endgroup\$ Commented Aug 23, 2017 at 19:33

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.