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
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;
}
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.
Thanks. Your english is fine. The movement is not the problem I will check the texture attachment issue!