- Home /
Help with javascript texture change 2d platformer
Hello. I am new to Unity and am making a 2d pixely platformer. I have a run texture and a stand texture. This is the script I am using for my moving and animations(Changing texture). For some reason, it only works for moving right. Any help would be appreciated!
Script:
var MoveSpeed = 0.1; var runtexture : Texture; var standtexture : Texture;
function Update () {
if (Input.GetKey (KeyCode.A)){ transform.Translate(Vector3.right *MoveSpeed); renderer.material.mainTexture = runtexture;
}else{ renderer.material.mainTexture = standtexture; }
if (Input.GetKey (KeyCode.D)){ transform.Translate(Vector3.left *MoveSpeed); renderer.material.mainTexture = runtexture;
}else{ renderer.material.mainTexture = standtexture; }
}
PS:
If the "A" or "D" key is pressed, I want to switch to the walk texture. If not, I want the stand texture.
JAVASCRIPT
Answer by rubenpvargas · May 23, 2012 at 10:41 PM
First sorry for bad english. 1.- Make sure you have correctly atacched your textures 2.- There is nothig like "Vector3.left" you need to use -Vector3.right, remember, Vector3.right is = to Vector3(1,0,0). -Vector3.right is = to Vector3(-1,0,0) (left direction).
if (Input.GetKey (KeyCode.A)) {
transform.Translate(Vector3.right *MoveSpeed);
renderer.material.mainTexture = runtexture;
}
else if (Input.GetKey (KeyCode.D)){
transform.Translate(-Vector3.right *MoveSpeed);
renderer.material.mainTexture = runtexture;
}
else {
renderer.material.mainTexture = standtexture;
}
Answer by Pipercubjl · May 24, 2012 at 04:04 PM
The movement is not the promblem. It the textures not changing when pressing "A." I will check the texture attachment thought. Thanks.
PS your English is fine.
Answer by Pipercubjl · May 24, 2012 at 04:04 PM
Thanks. Your english is fine. The movement is not the problem I will check the texture attachment issue!
Your answer
Follow this Question
Related Questions
iTween NullReferenceException error 1 Answer
Compare texture from array to another one in list 1 Answer
Creating 2d shapes with code and filling them 1 Answer
Please Help!!! my LineRenderer Script is not show when bulid on android device 1 Answer
Making the players head face toward the location of the mouse? 1 Answer