質問

I'm currently trying to create a 2d Platformer in Unity, Everything is working fine except one thing. When I push Left or Right, my sprite animation mirrors to the right direction. I'm currently trying to add controller input, but I cant seem to target pushing the analog left or right. It is supposed to mirror when pushing the stick right and vice versa.

Hope anyone can help me :)

#pragma strict

var X : float;

function Start () {
//Gathering normal object scale
X = transform.localScale.x;
}

function Update () {
    if(Input.GetKey("a")) {
    // Gamer pushes left arrow key
    // Set texture to normal position
    transform.localScale.x = -X;
}
else if (Input.GetKey("d")) {
    // Gamer pushes right arrow key
    // Flip texture
    transform.localScale.x = X;
}
if(Input.GetKey("left")) {
    // Gamer pushes left arrow key
    // Set texture to normal position
    transform.localScale.x = -X;
}
else if (Input.GetKey("right")) {
    // Gamer pushes right arrow key
    // Flip texture
    transform.localScale.x = X;
}
if(Input.GetAxis("Horizontal")) {
    // Gamer pushes left arrow key
    // Set texture to normal position
    transform.localScale.x = -X;
}
else if (Input.GetAxis("Horizontal")) {
    // Gamer pushes right arrow key
    // Flip texture
    transform.localScale.x = X;
    }
}
役に立ちましたか?

解決

You have this:

if(Input.GetAxis("Horizontal")) {
// Gamer pushes left arrow key
// Set texture to normal position
transform.localScale.x = -X;
}
else if (Input.GetAxis("Horizontal")) {
// Gamer pushes right arrow key
// Flip texture
transform.localScale.x = X;
}

The second else if will always be called because you are checking the exact same Input.GetAxis().

try something like this:

if (Input.GetAxis("Horizontal") < 0)
{
    transform.localScale.x = -X;
}
else if (Input.GetAxis("Horizontal") > 0)
{
    transform.localScale.x = X;
}

Input.GetAxis ("Horizontal") checks all left and right keys that could be pressed and spits out a number depending on either the left or right key...

Example If I were to press the 'Left Arrow' key, it would return a number between 0 and -1. If I were to press the 'Right Arrow' key, it would return a number between 0 and 1.

Does this make sense?

他のヒント

I would completely ditch the standard Unity Input which is simply horrendous and switch to InControl.

Their API allows you to detect which device is being used and allows for very simple and efficient multi-player support.

The mappings for different gamepads and platforms will let you develop for a standard layout and you will spend much less time trying to get the controls to more or less work across various platforms.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top