Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Creatorbot · Jul 30, 2012 at 12:42 PM · javascriptinput2d gamebackgroundscrolling

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);
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Creatorbot · Jul 31, 2012 at 05:34 AM 0
Share

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 :/

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Aug 02, 2012 at 09:26 AM 1
Share

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.

avatar image Creatorbot · Aug 02, 2012 at 01:07 PM 0
Share

**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); }

avatar image fafase · Aug 02, 2012 at 04:16 PM 1
Share

You are missing a { right after else meaning that

 renderer.material.SetTextureOffset ("_$$anonymous$$ainTex", offset); 

is always done cancelling the input.

avatar image Creatorbot · Aug 12, 2012 at 04:17 AM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

scripting joystick input on javascript 1 Answer

How do I invert the Y axis in Penelope tutorial? 3 Answers

How can I make smooth scrolling background and obstacles ? Because my backgrounds and obstacles have shaking(vibration) when it play ? 0 Answers

Pause Menu Background 1 Answer

How to make a 3D ball stop rolling as soon as you are done pressing the assigned button 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges