Im trying to make a script, that does the same when you walk in pokemon. so when you press
w,s,a,d it changes to different textures. Pressing w you see the player back texture, pressing d you see the rightside texture. You know what i mean. So i wrote this script but it does not work. (the script is not fully made, i only have made one texturechange, but why continue if it does not work?)
#pragma strict
var BACK : Texture;
var FRONT : Texture;
var LEFTS : Texture;
var RIGHTS : Texture;
function Start () {
var BACK = true;
var FRONT = false;
var LEFTS = false;
var RIGHTS = false;
}
function Update () {
if(Input.GetKeyDown("s")) {
var BACK = false;
var FRONT = true;
var LEFTS = false;
var RIGHTS = false;
}
}
At a very basic level, it looks like you’re trying to do something like this:
#pragma strict
Texture2d front;
Texture2d back;
Texture2d left;
Texture2d right;
function Update () {
if (Input.GetKey(KeyCode.W)) renderer.material.mainTexture = back;
if (Input.GetKey(KeyCode.S)) renderer.material.mainTexture = front;
if (Input.GetKey(KeyCode.A)) renderer.material.mainTexture = left;
if (Input.GetKey(KeyCode.D)) renderer.material.mainTexture = right;
}
This should work, however creating a game with 2d animated characters following this method will start to get complex.
If you want to create a sprite-based 2d game like this, you should really look at Unity’s new 2D features that were introduced in Unity 4.3. Read more about it here!