Home c# Infinite 2D Rear Background Animation (C # Unity)

Infinite 2D Rear Background Animation (C # Unity)

Author

Date

Category

I want to make the animation of the rear background in a simple 2D game, so that when you press the movement keys, after the character started to move the back background in the desired side.
I tried to do so to start

Public GameObject GG; // for whom to follow
  RENDERER R;
  Public float offset = 0.01f;
  Vector2 offsettexture = vector2.zero;
  Float Lastpos;
  Void Start ()
  {
    lastpos = gg.transform.position.x;
    r = getComponent & lt; renderer & gt; ();
    offsettexture = r.material.maintextureoffset;
  }
    Void Update ()
  {
    If (Lastpos! = GG.transform.position.x)
    {
      offsettexture.x + = (lastpos - gg.transform.position.x) * offset;
      r.material.maintextureOffset = offsettexture;
      lastpos = gg.transform.position.x;
    }
  }

But it did not work.


Answer 1, Authority 100%

The question seems to be solved, but if you suddenly need a smooth movement, then:

Public Class Follow2D: Monobehaviour
{
  Public Transform Target;
  Public Vector3 Pose;
  Private Vector3 Velocity;
  Public Float Speed ​​= 0.1f;
  Void FixedUpdate ()
  {
    pose.x = mathf.smootdamp (pose.x, target.position.x, ref verocity.x, speed);
    pose.y = mathf.smootdamp (pose.y, target.position.y, ref verocity.y, speed);
    transform.position = pose;
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions