1
\$\begingroup\$

I added a 3D Object > Quad and added a graphic on it with the Wrap Mode set to Repeat. I'm animating the Offset to scroll the repeating image for a parallax effect. Everything is working perfectly with in Editor and Android devices. On iOS devices, the image shows fine and then stretches as the offset is changing. I'm attaching 2 screen shots of the image import settings - Quad settings, and the issue on the iPhone. Below is my code:

 public float scrollSpeed = 0.05f;
 private Vector2 savedOffset;
 Rigidbody2D player;

 void Start () {
     GameObject player_go = GameObject.FindGameObjectWithTag("Player");

     if (player_go == null)
     {
         //Debug.LogError("Couldn't find an object with tag 'Player'");
         return;
     }

     player = player_go.GetComponent<Rigidbody2D>();

     savedOffset = GetComponent<Renderer>().sharedMaterial.GetTextureOffset("_MainTex");
 }

 void FixedUpdate()
 {
     if(player.GetComponent<BirdMovement>().dead)
     {
         return;
     }
     float vel = Time.fixedTime * scrollSpeed;

     float y = Mathf.Repeat(vel, 1);
     Vector2 offset = new Vector2(y, savedOffset.x%1);

     offset.x = (float) System.Math.Round (offset.x, 3); //Mathf.Round (offset.x * 10) / 10;

     Debug.Log ("Offset:" + offset.x);
     if (offset.x > 1) {
         offset.x = 0;
     }
     GetComponent<Renderer>().sharedMaterial.SetTextureOffset("_MainTex", offset);
 }

 void OnDisable()
 {
     GetComponent<Renderer>().sharedMaterial.SetTextureOffset("_MainTex", savedOffset);
 }

enter image description here enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ Is your parallax texture of screen size and looping it? or wider than that? \$\endgroup\$ Commented Mar 17, 2016 at 16:45

2 Answers 2

1
\$\begingroup\$

Just in case you haven't fix this or anyone else comes across this question, what worked for me was to change some import settings for the texture: Non Power of 2 from None to ToNearest and Sprite Mode from Single to None. I hope this helps.

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

Ahh, if you have parallax of screen size then my answer would be useful for you. I have had a bad experience with this issue even in Editor. What I did is just change the approach for parallax. That is working fine in all circumstances.

So follow these simple steps,

  • Create a Quad at origin(center of screen) of unit size with Position (0,0,0), rotation (0,0,0) and scaling (1,1,1).
  • Apply material with shader Unlit/Texture or Unlit/Transparent.
  • Apply the texture to that material.
  • Duplicate it and make that new Quad child of the original one.
  • Retain everything same except x position that should be 1. Means the position of the child Quad would be (1,0,0).

Now Attach the following script to the parent Quad only.

using UnityEngine;
using System.Collections;

public class Parallex : MonoBehaviour
{

    // Scrolling speed
    public float scrollSpeed;

    // Sorting layer of each parallax. Very helpful for multiple parallax in layers with different speed
    public int _sortingLayer = 0;

    // Ending X value on which parallax would repeat.
    float tileSize;
    float _worldScreenHeight;
    float _worldScreenWidth;

    private Vector3 startPosition;

    void Start ()
    {
        _worldScreenHeight = Camera.main.orthographicSize * 2.0f;
        _worldScreenWidth = _worldScreenHeight / Screen.height * Screen.width;
        transform.localScale = new Vector3 (_worldScreenWidth, _worldScreenHeight);
        tileSize = _worldScreenWidth;
        startPosition = transform.position;
    }

    void Update ()
    {
        float newPosition = Mathf.Repeat (Time.time * scrollSpeed, tileSize);
        transform.position = new Vector3 ((startPosition + Vector3.right * -newPosition).x, (startPosition + Vector3.right * -newPosition).y, -_sortingLayer);
    }
}

That's it. Obviously you also can modify it according to your needs.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .