parrallax texture offset based on camera movement.

Hey all,

I was wondering if this was possible and how it would be achieved. I have a plane on my stage and a texture attached to it. Currently I scroll the texture based on a value like the following.

stars.renderer.material.SetTextureOffset("_MainTex", new Vector2(0,Time.time * paralaxScrollRatio));

But i was wondering if i could make it more dynamic and have it scroll based on the camera movement in either the x or y at a slower speed.

I was thinking it might be hard to do it based on camera movement. The effect I’m trying to achieve is scrolling parallax stars that scroll based on the direction and speed the camera is moving.

Has anyone got any thought on this. It would be much appreciated.

1 Answer

1

I suppose you could just multiply the camera position offset by a small factor and use this value to offset the texture - something like this (camera script):

public float factor = 0.001f;
private Vector3 lastPos;

void Start(){
  lastPos = transform.position;
}

void Update(){
  Vector3 shift = transform.position-lastPos;
  lastPos = transform.position;
  Vector2 offset;
  offset.x = -shift.x*factor;
  offset.y = -shift.y*factor;
  stars.renderer.material.SetTextureOffset("_MainTex", offset);
}

You may have to scramble the axes, depending on how your objects are aligned - kind of:

  offset.y = -shift.z*factor;
  offset.x = -shift.x*factor;

You're right: I forgot to increment the offset! It was just "shaking" the stars in my code, because the offset was based on the movement since last Update. It should be accumulated over time, like you did in your version - this way the starts move according to the camera displacement.