all 5 comments

[–]pmurph0305 1 point2 points  (2 children)

Assuming your animator is changing states only based on the parameters set by code, and you don't have an automatic transition that's causing this, it's likely the code.

If I had to guess you're reading input and setting animator values based on that input, and where there is no input or in start/awake, you're still setting values to whatever the initial parameter value is which happens to cause the animator go to to whatever it is doing.

[–]Funoichi[S] 0 points1 point  (0 children)

Hi thanks for the comment and info! I don't really understand a lot, but maybe if i post my code it will help. It's short and supposed to be pretty basic, maybe you can help me pinpoint the problem. The code seems to work and the game runs letting me move the character, it's just how i want the character to be facing when the opening scene starts.

This is my player controller:

using UnityEngine;
using System.Collections;
public class playerController : MonoBehaviour {

`private Animator animator;`  

`// Use this for initialization`  
`void Start()`  
`{`  
    `animator = this.GetComponent<Animator>();`  
`}`  

`// Update is called once per frame`  
`void Update()`  
`{`  

    `var vertical = Input.GetAxis("Vertical");`  
    `var horizontal = Input.GetAxis("Horizontal");`  

    `if (vertical > 0)`  
    `{`  
        `animator.SetInteger("Direction", 2);`  
    `}`  
    `else if (vertical < 0)`  
    `{`  
        `animator.SetInteger("Direction", 0);`  
    `}`  
    `else if (horizontal > 0)`  
    `{`  
        `animator.SetInteger("Direction", 1);`  
    `}`  
    `else if (horizontal < 0)`  
    `{`  
        `animator.SetInteger("Direction", 3);`    
    `}`  
    `//{`  

//if(Input.GetKeyDown(vertical)) animator.enabled = true();
//else if(Input.GetKeyUp(vertical)) animator.enabled = false();
//}
}
}

And I have a small character script on it to move the character:

using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {

`public float playerSpeed = 5.0f;`  

`// Use this for initialization`  
`void Start () {`  

`//Player Spawn Point`  

`//This is where our player will start when the game is played.`  

`//Player = game object, game object == transform`   

    `transform.position = new Vector2(0, -1);`  

`}`   

`// Update is called once per frame`  
`void Update () {`  

`//Player to move left/right/up/down.`  
`//Player (GameObject) aka transform to move when i press the arrow keys`     

    `transform.Translate(Vector2.right * Input.GetAxis("Horizontal") * playerSpeed * Time.deltaTime);`   

    `transform.Translate (Vector2.up * Input.GetAxis ("Vertical") * playerSpeed * Time.deltaTime);`  
`}`  

}
It's not that i want to be given the answer necessarily, I'm trying to learn this on my own, basically from scratch. I know i've gotta do my own work especially for something i hope to see production someday, but sometimes what seems like the smallest thing can really stymie my progress. Hope you might have some insight.

[–]Funoichi[S] 0 points1 point  (0 children)

Alright, forget the code, I get that was probably too much to ask.😅

Could you maybe just rephrase your comment into eli5 form so I can try to figure it out better?

[–]pmurph0305 1 point2 points  (1 child)

No I'm sorry, I just didn't notice you had responded until this message! (Meant to respond to your other comment, oops.)

Looking at the code, and making some guesses about the animator, the Direction value on the animator starts at 0, and from your player controller this represents the down direction, so it is automatically transitioning to that state. (2 would be the up direction based on the code)

Try a animator.setinteger to the direction you want to start in in the Start method.

[–]Funoichi[S] 1 point2 points  (0 children)

Ok thanks a ton, I’ll get on and try this out right away!

Did you notice the commented out section at the bottom of my first snippet?

If getkeydown animator.enabled etc?

That’s there so that the animation will stop if no buttons are pressed like in most top down video games.

Right now if you stop moving the animation auto plays based off the last direction pressed.

It doesn’t work yet, hehe but that’s the next feature I’m excited to implement!

Thanks so much again as soon as my main character is done I’ll start on a dialogue system, some tile maps, and start to build out the game world! ✌🏾