Character facing issue after scene change.
Posted: Sun Jan 05, 2020 10:46 pm
Hi Tony, thanks for all the help you provide on this board. I've been using the crap outta your Dialogue system and I love it! I was hoping you could help me with an issue during scene change - I'm building a 2d 4-directional game, my character should be facing up when i go through a door into the next scene, but is instead facing down (in his downward idle animation). I use your scene transition tools for this. I was trying to use the TriggerEvent script you from your package (which is brilliantly versatile), but I can't seem to figure out how to call the command or manipulate it. Here's a copy of the code i'm using which controls my animator. I'm using a state machine, and a blend tree on the animator:
I feel like the solution is simple but I'm not finding it.
Thanks ahead of time!
Code: Select all
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum PlayerState
{
walk,
attack,
interact,
stagger,
idle
}
public class PlayerMovement : MonoBehaviour {
public PlayerState currentState;
public float speed;
private Rigidbody2D myRigidbody;
private Vector3 change;
private Animator animator;
// Start is called before the first frame update
void Start()
{
currentState = PlayerState.walk;
animator = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody2D>();
animator.SetFloat("x", 0);
animator.SetFloat("y", -1);
}
// Update is called once per frame
void FixedUpdate()
{
change = Vector3.zero;
change.x = Input.GetAxisRaw("Horizontal");
change.y = Input.GetAxisRaw("Vertical");
if(Input.GetButtonDown("attack") && currentState != PlayerState.attack && currentState != PlayerState.stagger)
{
StartCoroutine(AttackCo());
}
else if (currentState == PlayerState.walk || currentState == PlayerState.idle)
{
UpDateAnimationAndMove();
}
}
private IEnumerator AttackCo()
{
animator.SetBool("attacking", true);
currentState = PlayerState.attack;
yield return null;
animator.SetBool("attacking", false);
yield return new WaitForSeconds(0.2f);
currentState = PlayerState.walk;
}
void UpDateAnimationAndMove()
{
if (change != Vector3.zero)
{
MoveCharacter();
animator.SetFloat("x", change.x);
animator.SetFloat("y", change.y);
animator.SetBool("moving", true);
}
else
{
animator.SetBool("moving", false);
myRigidbody.velocity = Vector3.zero;
}
void MoveCharacter()
{
change.Normalize();
if (Input.GetKey(KeyCode.LeftShift))
{
/// myRigidbody.MovePosition(transform.position + change * speed * Time.fixedDeltaTime * 1.5f);
myRigidbody.velocity = change.normalized * speed * 1.3f;
}
else
{
/// myRigidbody.MovePosition(transform.position + change * speed * Time.fixedDeltaTime);
myRigidbody.velocity = change.normalized * speed;
}
}
}
public void Knock(float knockTime)
{
StartCoroutine(KnockCo(knockTime));
}
private IEnumerator KnockCo(float knockTime)
{
if (myRigidbody != null)
{
yield return new WaitForSeconds(knockTime);
myRigidbody.velocity = Vector2.zero;
currentState = PlayerState.idle;
myRigidbody.velocity = Vector2.zero;
}
}
private void Update()
{
if (Input.GetKey(KeyCode.W)) //Moves up
{
change += Vector3.up;
}
if (Input.GetKey(KeyCode.A)) //Moves left
{
change += Vector3.left; //Moves down
}
if (Input.GetKey(KeyCode.S))
{
change += Vector3.down;
}
if (Input.GetKey(KeyCode.D)) //Moves right
{
change += Vector3.right;
}
}
}
Thanks ahead of time!