THE ROAD RUNNER TUTORIAL 4: SET THE GROUND MOVING

Check out the gameplay of the finished game below:



In the previous post we added the character to our game and added animations to it. But, the character was not really moving. In this post we will set the ground moving, oh wait. Ground moving? Aren't we supposed to move the character? Well, yes we are supposed to move the character, but then since I ain't no 3d designer nor an artist, we will use the technique of scrolling the ground by using the texture offset property of the Materials (Ground Material in this case). What we need to do is modify these offsets runtime via script.

Follow the steps below to achieve this effect:

Step 1:  
Create a new folder named Scripts in the Project panel.

Step 2:
Add a new Script to the Scripts folder by right clicking and navigating to Create-> C# Script, name it as GroundControl.

Step 3:
Open the script, replace it with the following code:

//Material texture offset rate
float speed = .5f;

//Offset the material texture at a constant rate
void Update () {
 float offset = Time.time * speed;                             
 renderer.material.mainTextureOffset = new Vector2(0, -offset); 
}

We basically need to set up an illusion to the player so that he feels that he is moving even though he is not. And to achieve that we animated our character so as to make him run. Then, we are offsetting the texture of the ground in the negative y direction so as to complete the illusion setup.

In the code above we firstly have setup a rate at which the offset should take place. Then we have setup a counter by using Time.time, multiplying this counter with the offset rate gives us the desired offset. All that is remaining is to assign this to the rendering material.

The offset is a 2d offset with no offset i.e. 0 along the x axis and the required offset rate along the negative y axis. Negative y axis is because if we need to produce an effect of the character moving forward, we should make the ground move backward.
Refer the below image if you are not aware of the x and y axis.  


Step 4:
Save the script and go back to unity. Add this script to Ground and both the Wall Gameobjects to get this illusion into effect.

Once added, press ctrl+p or the play button to check out the magic!

Download the completed version of this game from the Resources page.

The Road Runner Tutorial 1: Setting Up The World
The Road Runner Tutorial 2: Setting Up The World Continued
The Road Runner Tutorial 3: Adding A Character To Our Game
The Road Runner Tutorial 5: Adding Snags and Powerups to Our Game
The Road Runner Tutorial 6: Collecting The Snags and Powerups
The Road Runner Tutorial 7: Adding Gameplay Logic
The Road Runner Tutorial 8: Creating a Custom GUI Skin
The Road Runner Tutorial 9: Creating a Pause Menu
The Road Runner Tutorial 10: Adding Countdown and Main Menu
The Road Runner Tutorial 11: Adding Sound Effects To Our Game
The Road Runner Tutorial 12: Porting The Game To Android
Share on Google+

About Sujit Horakeri

Sujit Horakeri is a game freak just like any other next door guy you would come across. He is a Web Developer by Profession, Game Developer by Choice.
Connect with him on:
    Blogger
    Facebook

34 comments:

  1. the script doesn't work for me. i keep getting errors

    ReplyDelete
    Replies
    1. We might be able to help you if you could be more clear on what the error is...

      Delete
    2. Assets/Scripts/GroundControl.cs(2,7): error CS0116: A namespace can only contain types and namespace declarations

      Delete
    3. well, may be you have pasted the script outside the class definition and you might not have included the namespaces either.
      The complete script would look like:

      using UnityEngine;
      using System.Collections;

      public class GroundScript : MonoBehaviour {
      //Material texture offset rate
      float speed = .5f;

      //Offset the material texture at a constant rate
      void Update () {
      float offset = Time.time * speed;
      renderer.material.mainTextureOffset = new Vector2(0, -offset);
      }
      }
      Make sure the script name is same as the class name

      Delete
    4. This comment has been removed by the author.

      Delete
    5. Well, my mistake. I believe you have named the script as GroundControl in the Assets folder and the class name is GroundScript.
      Paste the below script and everything will be fine.

      using UnityEngine;
      using System.Collections;

      public class GroundControl : MonoBehaviour {
      //Material texture offset rate
      float speed = .5f;

      //Offset the material texture at a constant rate
      void Update () {
      float offset = Time.time * speed;
      renderer.material.mainTextureOffset = new Vector2(0, -offset);
      }
      }

      Delete
    6. It worked. Awesome! Thank you

      Delete
    7. Sir, I tried the scripts you wrote but there is one error.
      renderer.material.mainTextureOffset = new Vector2(0, -offset);
      it says 'unityengine.component does not contain a definition for 'material' and no extension method 'material' accepting a first argument of type 'unityengine.component' could be found.

      what does this mean, how do I fix it. thanks in advance!

      Delete
    8. The shortcut seems to be removed from Unity 5 onwards.
      To get this working replace, renderer.material.mainTextureOffset with GetComponent ().material.mainTextureOffset.

      Delete
    9. GetComponent ().material.mainTextureOffset = new Vector2(0, -offset);
      in this line i'm getting following error -
      the type argument for method 'UnityEngine.Component.GetComponenet()' cann't be interred from the usage. try specifying the type argumentsexplicitly.

      Delete
    10. Try
      GetComponent().material.mainTextureOffset = new Vector2(0, -offset);

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. for me walls are going in opposite direction please help..

    ReplyDelete
  4. guys plz rply: 3errors are -No idle animation found. Turning off animations.
    UnityEngine.Debug:Log(Object)
    4th error as -No jump animation found and the character has canJump enabled. Turning off animations.
    UnityEngine.Debug:Log(Object)

    ReplyDelete
    Replies
    1. You need to set the animations to the character. The error message clearly says that the animations are not found. Add the animations to the Character(Player)

      Delete
    2. how can i add them
      if it was explained in above steps plz suggest in whch step it was explained?

      Delete
    3. The 3rd Person Controller had these animations when you imported the character controller package..
      Check out the step 4 of the Part 3 of this tutorial series..

      Delete
  5. when I'm on your object impose script knocks this error please tell how to fix
    MissingComponentException: There is no 'Renderer' attached to the "GameObject" game object, but a script is trying to access it.
    You probably need to add a Renderer to the game object "GameObject". Or your script needs to check if the component is attached before using it.
    NewBehaviourScript.Update ()

    ReplyDelete
  6. Sir uhmm, when I'm trying to add the script on the ground I encountered an error, the Message Box says:

    Can't add script
    can't add component 'GroundScript' because it doesn't exist. Check to see if the file name and name match

    ReplyDelete
  7. I really like your tutorial and it really help me in making the game But i have few question...
    1) How to add chaser like policeman and creature like subway surfer and temple run
    2) I want to add roof (wall) on the top .
    3) I want to add a image in infinite area of the game
    4) I want to add multiple obstacle and power ups.like magnet and jumper and etc
    5) I want to change the character

    ReplyDelete
  8. its written like this: using UnityEngine; using System.Collections; public class GroundControl : MonoBehavior{ //Use this for initialization void Start (){} //Update is called once per frame void Update (){}}

    ReplyDelete
  9. The character is falling down instead of moving forward :p please help

    ReplyDelete
  10. hey what is material.mainTextureOffset its my error in debugger

    ReplyDelete
  11. Hi,
    I´m a little stuck with this. The script in general is working, but i have a problem with the texture. When I start, at first everything is fine, the texture moving backwards, but then the texture is suddenly stretched, like you can see in this image:
    http://de.tinypic.com/r/9bdco5/9

    ReplyDelete
    Replies
    1. Okay, I found the problem myself :)
      In case someone has the same problem:
      When you import the images, set the image as a "texture" in the inspector and set "Wrap Mode" on "Repeat" instead of "Clamp".

      Delete
    2. Thanks for posting your solution. Much obliged.

      Delete
  12. GetComponent ().material.mainTextureOffset = new Vector2(0, -offset);
    in this line i'm getting following error -
    the type argument for method 'UnityEngine.Component.GetComponenet()' cann't be interred from the usage. try specifying the type argumentsexplicitly.

    ReplyDelete
  13. //docs.unity3d.com/ScriptReference/Material.SetTextureOffset.html


    using UnityEngine;
    using System.Collections;

    public class GroundControl : MonoBehaviour {
    public float scrollSpeed = 0.5F;
    public Renderer rend;
    void Start() {
    rend = GetComponent();
    }
    void Update() {
    float offset = Time.time * scrollSpeed;
    rend.material.SetTextureOffset("_MainTex", new Vector2(offset, 0));
    }
    }

    ReplyDelete
  14. Use this line :GetComponent ().material.mainTextureOffset = new Vector2(0, -offset);

    ReplyDelete
  15. 1) Check if the script's name is the same of the class.
    2) Use the code below.

    using UnityEngine;
    using System.Collections;

    public class GroundControl : MonoBehaviour {

    //Material texture offset rate
    float speed = .35f;

    //Offset the material texture at a constant rate
    void Update()
    {
    float offset = Time.time * speed;
    GetComponent().material.mainTextureOffset = new Vector2(0, -offset);
    }
    }

    ReplyDelete
  16. hi the assets are not in unity anymore do you still have it? thanks

    ReplyDelete
  17. how to increase speed after particular distance covered or time covered??

    ReplyDelete