- Home /
How do I make the scrolling texture background change direction depending on keyboard input for the player movement controls?
Hi. I would really appreciate any help that I could get.
I am a designer/artist doing my own programming (I'm not great at programming!) for a relatively simple 2D Shooter with a scrolling background. I am using Javascript. My code for scrolling the texture from right to left on the x axis works fine.
However, I also need the scrolling direction to be able to change (go opposite) if the player is moving backwards. I thought that somehow I need the script to recognize the keyboard input for left or right and then alter the scrolling direction accordingly...?
Please, how do I make the scrolling texture background change direction depending on keyboard input for the player movement controls? If the player is moving backwards, I need the background texture to scroll left to right. I have copied my code attempt below and thank you all in advance for any help that you may be able to give me.
// Scrolls background texture offset over time
//Script should also register the player's keyboard input & change the scrolling direction accordingly...currently not working!
var scrollspeed = .005;
private var offsetForward = Vector2.right;
private var offsetBackward = Vector2.left;
private var offset = Vector2.zero;
function Update () {
if (Input.GetKeyDown ("left"))
{
offset.x += scrollspeed * Time.deltaTime;
renderer.material.SetTextureOffset ("_MainTex", offsetBackward);
} if (Input.GetKeyDown ("right"))
{
offset.x += scrollspeed * Time.deltaTime;
renderer.material.SetTextureOffset ("_MainTex", offsetForward);
}else
offset.x += scrollspeed * Time.deltaTime;
renderer.material.SetTextureOffset ("_MainTex", offset);
}
I didn't notice at the time I posted this that something weird happened when I copied & pasted my code example. Wherever it says " that should be " I just edited the question an think it's fixed now :/
Answer by fafase · Jul 31, 2012 at 05:48 AM
The principle should be the same as if you were moving the object.
if (Input.GetKey ("Left")){
offset.x += scrollspeed * Time.deltaTime;
renderer.material.SetTextureOffset ("_MainTex", Vector3.right);
}
if (Input.GetKey ("Right")){
offset.x -= scrollspeed * Time.deltaTime;
renderer.material.SetTextureOffset ("_MainTex", Vector3.left);
}
if (Input.GetKey ("Up")){
offset.y += scrollspeed * Time.deltaTime;
renderer.material.SetTextureOffset ("_MainTex", Vector3.up);
}
if (Input.GetKey ("Down")){
offset.y -= scrollspeed * Time.deltaTime;
renderer.material.SetTextureOffset ("_MainTex", -Vector3.up);
}
Answer by Creatorbot · Aug 02, 2012 at 06:32 AM
Hey! Thanks for your reply & help fafase, I'll give it a try & see if it works for me :D
Answer by Creatorbot · Aug 02, 2012 at 09:21 AM
I tried what you suggested and maybe I misunderstood? Here is what I did...
var scrollspeed = .005; private var offsetForward = Vector2.right; private var offsetBackward = Vector2.left; private var offset = Vector2.zero;
function Update () {
if (Input.GetKeyDown ("left")) { offset.x += scrollspeed Time.deltaTime; renderer.material.SetTextureOffset ("_MainTex", Vector2.left); } if (Input.GetKeyDown ("right")) { offset.x += scrollspeed Time.deltaTime; renderer.material.SetTextureOffset ("_MainTex", Vector2.right); }else offset.x += scrollspeed * Time.deltaTime; renderer.material.SetTextureOffset ("_MainTex", offset); }
I also tried this:
var scrollspeed = .005; private var offsetForward = Vector2.right; private var offsetBackward = Vector2.left; private var offset = Vector2.zero;
function Update () {
if (Input.GetKeyDown ("left")) { offset.x += scrollspeed Time.deltaTime; Plane.renderer.material.SetTextureOffset ("_MainTex", Vector2.left); } if (Input.GetKeyDown ("right")) { offset.x += scrollspeed Time.deltaTime; Plane.renderer.material.SetTextureOffset ("_MainTex", Vector2.right); }else offset.x += scrollspeed * Time.deltaTime; Plane.renderer.material.SetTextureOffset ("_MainTex", offset); }
and this, but none of it had any effect...
var scrollspeed = .005; private var offset = Vector2.zero;
function Update () {
if (Input.GetKeyDown ("left")) { offset.x += scrollspeed Time.deltaTime; renderer.material.SetTextureOffset ("_MainTex", Vector2.left); } if (Input.GetKeyDown ("right")) { offset.x += scrollspeed Time.deltaTime; renderer.material.SetTextureOffset ("_MainTex", Vector2.right); }else offset.x += scrollspeed * Time.deltaTime; renderer.material.SetTextureOffset ("_MainTex", offset); }
So as I wrote above, none of my attempts made any difference when using the keyboard. Please have I misunderstood? Thanks again for any help I may receive
O$$anonymous$$ my bad, you should not use Get$$anonymous$$eyDown qs it returns true only the the frame it got pushed. You want to try Get$$anonymous$$ey only. At the moment, you would have to press many times but I guess you want to keep pressing.
**Thanks once again for your help. $$anonymous$$uch appreciated! It's getting closer as now the keyboard input is registered but ins$$anonymous$$d of the scrolling direction reversing, it just stops. How do you get it to switch direction from the keyboard input please?
... this is my latest working code so far:**
// Scrolls background texture offset over time //Script should also register the player's keyboard input & change the scrolling direction accordingly...currently not working! //Slight improvement as the keyboard input is now registered but in$$anonymous$$d of reversing the scrolling direction, it just stops scrolling // // var scrollspeed = .005;
private var offset = Vector2.zero;
function Update () {
if (Input.Get$$anonymous$$ey ("left")) { offset.x += scrollspeed Time.deltaTime; renderer.material.SetTextureOffset ("_$$anonymous$$ainTex", Vector2.left); }else offset.x += scrollspeed Time.deltaTime; renderer.material.SetTextureOffset ("_$$anonymous$$ainTex", offset); }
You are missing a { right after else meaning that
renderer.material.SetTextureOffset ("_$$anonymous$$ainTex", offset);
is always done cancelling the input.
Hey! Good spotting! It's amazing what something simple like a missing semi-colon can do ;P
Thanks for your help. The input/registration is working fine now but I realised that the way the code currently works, it horizontally flips the scrolling texture ins$$anonymous$$d of just changing/ reversing it's direction. There was a funny glitch that occurred after you stopped moving backwards & it was initially hard to see what caused that glitch. I'm trying a few other things now that I've isolated my problem but if you have any quick fixes or solutions, they are more than welcome.
:)
Here's a development demo I posted a couple of weeks ago. I've done more since then on other stuff but it shows the glitch which is still there at the moment...
http://www.youtube.com/watch?v=30OfdexEJNo&playnext=1&list=PL0B46903B910F8111&feature=results_main