Page 1 of 1

Character facing issue after scene change.

Posted: Sun Jan 05, 2020 10:46 pm
by chrislow19
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:

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;
        }
    }
}
I feel like the solution is simple but I'm not finding it.

Thanks ahead of time!

Re: Character facing issue after scene change.

Posted: Mon Jan 06, 2020 9:14 am
by Tony Li
Here's one idea:

Add four methods to PlayerMovement: FaceUp, FaceDown, FaceLeft, and FaceRight, where each one looks similar to this:

Code: Select all

public void FaceUp()
{
    animator.SetFloat("x", 0);
    animator.SetFloat("y", 1);
}
I'm assuming you've set up a spawnpoint GameObject (typically an empty GameObject) in the next scene. This spawnpoint GameObject specifies where the player should appear when he goes through the door from the previous scene.

Add a trigger collider and a TriggerEvent component to this GameObject. Configure the TriggerEvent's OnTriggerEnter() to call the player's PlayerMovement.FaceUp method. When the player enters the scene and is moved to this position, the TriggerEvent will fire and make him face up.

Finally, add a TimedEvent component to the same GameObject. Set the timing type of Frames, and specify 1 more than the Save System's Frames To Wait Before Apply Data value. The Save System's Frames To Wait Before Apply Data value is initially set to 1, so you can probably set the TimedEvent to 2. Then configure its OnTimeReached() event to deactivate the spawnpoint GameObject. This will prevent it from changing the player's facing if the player re-enters the trigger collider.

Re: Character facing issue after scene change.

Posted: Mon Jan 06, 2020 5:13 pm
by chrislow19
Tony that worked perfectly. I can't thank you enough for the help. I didn't even know you included a timed event script in the product - I'm going to play with that! I've been using trigger event on unrelated tasks even.

Thanks again!

Re: Character facing issue after scene change.

Posted: Mon Jan 06, 2020 8:01 pm
by Tony Li
Glad to help!