- Home /
Transform.position.x new vector3 NOT changing object position
Hi!
I'm making a basic scrolling effect for the ground and background. It moves constantly, not by user input.
When I play the game, the floor moves at the desired speed from right to left along the X axis. I want the background to then jump back around to the right of the canvas once it is out on the frame and carrying on moving left to create the infinite moving effect. The code I'm using isn't working. When the image reaches x -700, it is supposed to then jump back to the right of the canvas but it isn't. I have tried with and without the "transform.position.z" on the vector3 part and it doesn't change anything. Any help would be greatly appreciated!
public class grassScroller : MonoBehaviour
{
void Update()
{
transform.position += new Vector3(-0.005f, 0);
if (transform.position.x < -700)
{
transform.position = new Vector3(660, transform.position.y);
}
}
}
Answer by wfreeman480 · Jan 03 at 11:57 PM
I tested out your code and it reset for me. Need more details I guess. Also id recommend moving your code to a function:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class grassScroller : MonoBehaviour
{
void Update()
{
landscapeMove();
}
void landscapeMove()
{
transform.position += new Vector3(-0.005f, 0);
if (transform.position.x < -700)
{
transform.position = new Vector3(660, transform.position.y);
}
}
}
Your answer
Follow this Question
Related Questions
collider triggers transform position 1 Answer
Move an object to Input.Touch location 0 Answers
Problem with Vector3 position. 1 Answer
fixed distance for Vector3.moveTowards ? 1 Answer
Instantiate object in front of player? 2 Answers