Okay, so the first instance of this, I managed to fix. Basically, if I pressed the interact button to pick up something, the interact animation would play and the item would disappear. If I pressed interact but then moved left/right at the same time, the character’s animation would be interrupted, and instead of walking, he would float along the floor until stopping. As I said, I managed to fix the first case of this by doing everything in a coroutine and stopping the player’s movement for a second or two.
However, if I talk to an NPC first, and then attempt to pick up the item and move left/right at the same time, the issue happens again. I’m not sure why as the coroutine is supposed to stop the player’s movement once the item is interacted with. If the NPC has been interacted with AND the pickup has been collected, a spotlight activates indicating the player can leave the scene. The coroutine for picking up the item is located in my PlayerController script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public Animator anim;
public float moveSpeed;
public float jumpForce;
public bool jumped;
public bool allowInteract = false;
public float gravityScale;
public float knockBackForce;
public float knockBackTime;
//public Material textureChange;
//public Material textureDefault;
public bool allowCombat;
public bool allowJump;
public static bool canMove;
public ChestTrigger chest
{
get
{
if (_chest != null)
return _chest;
_chest = GameObject.FindWithTag("Chest Trigger")?.GetComponent<ChestTrigger>();
return _chest;
}
}
//public SkinnedMeshRenderer playerRenderer;
//public GameObject playerRendererRef;
//public static PlayerController instance;
private ChestTrigger _chest = null;
private Vector2 moveDirection;
private Vector2 moveHorizontal;
private float knockBackCounter;
private CharacterController controller;
private Quaternion targetRot;
private bool headingLeft = false;
private Pickup pickupWeapon;
void Awake()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
//playerRenderer = playerRendererRef.GetComponent<SkinnedMeshRenderer>();
/*if(instance != null)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
instance = this;
}*/
}
void Start()
{
Cursor.visible = false;
pickupWeapon = FindObjectOfType<Pickup>();
canMove = true;
targetRot = transform.rotation;
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area"))
{
allowCombat = false;
allowJump = true;
}
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("hut_interior"))
{
allowCombat = false;
allowJump = false;
}
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area 2"))
{
allowCombat = false;
allowJump = true;
}
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("level 1, room 1"))
{
allowCombat = true;
allowJump = true;
}
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("level 1, room 2"))
{
allowCombat = true;
allowJump = true;
}
}
void Update()
{
//To avoid any control or animation issues, each code block must be within this code block
if (knockBackCounter <= 0 && canMove)
{
moveHorizontal.x = Input.GetAxis("Horizontal");
//moveVertical.y = Input.GetAxis("Vertical");
moveDirection = new Vector2(moveHorizontal.x * moveSpeed, moveDirection.y);
controller.Move(moveDirection * Time.deltaTime);
//Adds character rotation when changing direction horizontally
if ((moveHorizontal.x < 0f && !headingLeft) || (moveHorizontal.x > 0f && headingLeft))
{
if (moveHorizontal.x < 0f) targetRot = Quaternion.Euler(0, 270, 0);
if (moveHorizontal.x > 0f) targetRot = Quaternion.Euler(0, 90, 0);
headingLeft = !headingLeft;
}
transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, Time.deltaTime * 20f);
//Adds character rotation when changing direction vertically
/*if(moveVertical.y < 0f && lookingUp || (moveVertical.y > 0f && !lookingUp))
{
if (moveVertical.y > 0f) targetrot = Quaternion.Euler(0, 0, 0);
if (moveVertical.y < 0f) targetrot = Quaternion.Euler(0, 180, 0);
lookingUp = !lookingUp;
}*/
if (SceneManagement.insideHut && canMove)
{
float moveHorizontalSnap = Input.GetAxis("Horizontal");
float moveVerticalSnap = Input.GetAxis("Vertical");
//Adds character rotation when changing direction horizontally, but snaps instead of fully rotating
if (moveHorizontalSnap > 0)
{
transform.eulerAngles = new Vector2(0, 90);
}
else if (moveHorizontalSnap < 0)
{
transform.eulerAngles = new Vector2(0, -90);
}
//To possibly prevent diagonal movement with some control setups, try adding 'else if'
//Adds character rotation when changing direction vertically, but snaps instead of fully rotating
else if (moveVerticalSnap > 0)
{
transform.eulerAngles = new Vector2(0, 0);
}
//Use this to make the character face towards the camera.
/*else if (moveVertical < 0)
{
transform.eulerAngles = new Vector3(0, 180);
}*/
}
if (controller.isGrounded)
{
if (allowJump)
{
moveDirection.y = -1f;
//GetKeyDown will require the player to press the button each time they want to jump. GetKey will allow the player to spam the jump button if they keep pressing it down.
if (Input.GetKeyDown(KeyCode.KeypadPlus))
{
moveDirection.y = jumpForce;
jumped = true;
}
else if (!Input.GetKeyDown(KeyCode.KeypadPlus))
{
jumped = false;
}
if (SceneManagement.xbox360Controller == 1)
{
if (Input.GetKeyDown("joystick button 0"))
{
moveDirection.y = jumpForce;
jumped = true;
}
else if (!Input.GetKeyDown("joystick button 0"))
{
jumped = false;
}
}
else if (SceneManagement.ps4Controller == 1)
{
if (Input.GetKeyDown("joystick button 1"))
{
moveDirection.y = jumpForce;
jumped = true;
}
else if (!Input.GetKeyDown("joystick button 1"))
{
jumped = false;
}
}
}
}
if (allowCombat)
{
if (Input.GetKeyDown(KeyCode.Space))
{
anim.SetTrigger("Attack");
//GameObject projectileObject = Instantiate(projectilePrefab);
//projectilePrefab.transform.position = spawnPoint.transform.position + spawnPoint.transform.forward;
}
else if (SceneManagement.xbox360Controller == 1)
{
if (Input.GetKeyDown("joystick button 1"))
{
anim.SetTrigger("Attack");
}
}
else if (SceneManagement.ps4Controller == 1)
{
if (Input.GetKeyDown("joystick button 2"))
{
anim.SetTrigger("Attack");
}
}
}
if (allowInteract)
{
if (SceneManagement.xbox360Controller == 1)
{
if (Input.GetKeyDown("joystick button 2"))
{
StartCoroutine("Pickup");
}
}
else if (SceneManagement.ps4Controller == 1)
{
if (Input.GetKeyDown("joystick button 0"))
{
StartCoroutine("Pickup");
}
}
else
{
if (Input.GetKeyDown(KeyCode.Return))
{
StartCoroutine("Pickup");
}
}
}
if (ChestTrigger.allowOpen)
{
if (Input.GetKeyDown(KeyCode.Return))
{
StartCoroutine("Open");
}
else if (Input.GetKeyDown(KeyCode.Return) && !ChestTrigger.allowOpen)
{
anim.SetBool("Interact", false);
}
else if (SceneManagement.xbox360Controller == 1)
{
if (Input.GetKeyDown("joystick button 2"))
{
StartCoroutine("Open");
}
else if (Input.GetKeyDown("joystick button 2") && !ChestTrigger.allowOpen)
{
anim.SetBool("Interact", false);
}
}
else if (SceneManagement.ps4Controller == 1)
{
if (Input.GetKeyDown("joystick button 0"))
{
StartCoroutine("Open");
}
else if (Input.GetKeyDown("joystick button 0") && !ChestTrigger.allowOpen)
{
anim.SetBool("Interact", false);
}
}
}
}
else
{
knockBackCounter -= Time.deltaTime;
}
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
anim.SetBool("isGrounded", controller.isGrounded);
//If the character can't move, then the Speed is set to 0. Otherwise it'll use the horizontal input value.
anim.SetFloat("Speed",
!canMove
? 0f
: Mathf.Abs(Input.GetAxis("Horizontal")));
//anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));
}
public IEnumerator Pickup()
{
canMove = false;
anim.SetBool("Interact", controller.isGrounded);
pickupWeapon.ObjectActivation();
yield return new WaitForSeconds(1f);
allowInteract = false;
canMove = true;
}
public IEnumerator Open()
{
canMove = false;
anim.SetBool("Interact", controller.isGrounded);
chest.ChestOpen();
yield return new WaitForSeconds(1.5f);
canMove = true;
}
public void Knockback(Vector3 direction)
{
knockBackCounter = knockBackTime;
moveDirection = direction * knockBackForce;
moveDirection.y = knockBackForce;
}
}
And here are my Pickup, DialogueManager, and SceneManagement scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickup : MonoBehaviour
{
public GameObject[] buttonPrompts;
public GameObject rayGun;
public GameObject pickupLight;
public static bool objectsDisabled;
public GameObject thePlayer;
private PlayerController thePlayerController;
void Awake()
{
thePlayerController = thePlayer.GetComponent<PlayerController>();
objectsDisabled = false;
}
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
thePlayerController.allowInteract = true;
ControllerDetection();
if (SceneManagement.ps4Controller == 1)
{
PS4Prompts();
}
else if (SceneManagement.xbox360Controller == 1)
{
Xbox360Prompts();
}
else
{
PCPrompts();
}
}
}
public void OnTriggerExit(Collider other)
{
thePlayerController.allowInteract = false;
}
public void ObjectActivation()
{
rayGun.SetActive(false);
pickupLight.SetActive(false);
objectsDisabled = true;
GetComponent<Collider>().enabled = false;
}
public void Hide()
{
buttonPrompts[0].SetActive(false);
buttonPrompts[1].SetActive(false);
buttonPrompts[2].SetActive(false);
}
public void Xbox360Prompts()
{
buttonPrompts[1].SetActive(true);
Invoke("Hide", 3f);
if (objectsDisabled)
{
buttonPrompts[1].SetActive(false);
}
}
public void PS4Prompts()
{
buttonPrompts[2].SetActive(true);
Invoke("Hide", 3f);
if (objectsDisabled)
{
buttonPrompts[2].SetActive(false);
}
}
public void PCPrompts()
{
buttonPrompts[0].SetActive(true);
Invoke("Hide", 3f);
if (objectsDisabled)
{
buttonPrompts[0].SetActive(false);
}
}
public void ControllerDetection()
{
string[] names = Input.GetJoystickNames();
for (int x = 0; x < names.Length; x++)
{
//print(names[x].Length);
if (names[x].Length == 19)
{
//print("PS4 CONTROLLER IS CONNECTED");
SceneManagement.ps4Controller = 1;
SceneManagement.xbox360Controller = 0;
if (SceneManagement.ps4Controller == 1)
{
//Debug.Log("PS4 controller detected");
}
}
else if (names[x].Length == 33)
{
//print("XBOX 360 CONTROLLER IS CONNECTED");
SceneManagement.ps4Controller = 0;
SceneManagement.xbox360Controller = 1;
if (SceneManagement.xbox360Controller == 1)
{
//Debug.Log("Xbox 360 controller detected");
}
}
else
{
SceneManagement.ps4Controller = 0;
SceneManagement.xbox360Controller = 0;
}
if (SceneManagement.xbox360Controller == 0 && SceneManagement.ps4Controller == 0)
{
//Debug.Log("No controllers detected");
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class DialogueManager : MonoBehaviour
{
public GameObject dialogueBox;
public TextMeshProUGUI nameDisplay;
public GameObject[] buttonPrompts;
public TextMeshProUGUI textDisplay;
public string[] sentences;
public string[] characterName;
public float typingSpeed;
private int xbox360Controller = 0;
private int pS4Controller = 0;
private int index;
private BoxCollider dialogueTrigger;
private int currentLine;
private bool interactPressed;
private bool continuePressed;
private bool endDialogue;
private bool continueAllowed;
public bool characterVicinity;
private bool dialogueActive;
private NPC theNPC;
void Start()
{
dialogueTrigger = GetComponent<BoxCollider>();
theNPC = FindObjectOfType<NPC>();
}
IEnumerator Type()
{
foreach (char letter in sentences[index].ToCharArray())
{
textDisplay.text += letter;
yield return new WaitForSeconds(typingSpeed);
}
}
public void NextSentence()
{
buttonPrompts[3].SetActive(false);
buttonPrompts[4].SetActive(false);
buttonPrompts[5].SetActive(false);
//If Index has less than the number of elements in the 'sentences' array by -1
if (index < sentences.Length - 1)
{
index++;
//Resets textDisplay so sentences don't stack
textDisplay.text = "";
StartCoroutine(Type());
}
else
{
textDisplay.text = "";
}
}
public void OnTriggerStay(Collider other)
{
if (other.gameObject.name == "Player")
{
characterVicinity = true;
ControllerDetection();
if (pS4Controller == 1)
{
PS4Prompts();
}
else if (xbox360Controller == 1)
{
Xbox360Prompts();
}
else
{
PCPrompts();
}
}
}
public void OnTriggerExit(Collider other)
{
characterVicinity = false;
}
public void Update()
{
if (characterVicinity)
{
dialogueActive = false;
//Set conditions to prevent button spamming. The Return key will only work if the dialogue box isn't active. If it is, it won't do anything.
if (Input.GetKeyUp(KeyCode.Return) && !dialogueBox.activeSelf)
{
PlayerController.canMove = false;
interactPressed = true;
if (interactPressed)
{
continueAllowed = false;
nameDisplay.enabled = true;
dialogueBox.SetActive(true);
StartCoroutine("Type");
}
}
else if (SceneManagement.xbox360Controller == 1)
{
if (Input.GetKeyUp("joystick button 2") && !dialogueBox.activeSelf)
{
PlayerController.canMove = false;
interactPressed = true;
if (interactPressed)
{
continueAllowed = false;
nameDisplay.enabled = true;
dialogueBox.SetActive(true);
StartCoroutine("Type");
}
}
}
else if (SceneManagement.ps4Controller == 1)
{
if (Input.GetKeyUp("joystick button 0") && !dialogueBox.activeSelf)
{
PlayerController.canMove = false;
interactPressed = true;
if (interactPressed)
{
continueAllowed = false;
nameDisplay.enabled = true;
dialogueBox.SetActive(true);
StartCoroutine("Type");
}
}
}
}
if (nameDisplay.enabled && dialogueBox.activeSelf)
{
dialogueActive = true;
if (SceneManagement.xbox360Controller == 1)
{
if (buttonPrompts[4].activeSelf && Input.GetKeyUp("joystick button 2"))
{
currentLine++;
if (currentLine >= sentences.Length)
{
nameDisplay.enabled = false;
dialogueBox.SetActive(false);
dialogueActive = false;
endDialogue = true;
characterVicinity = false;
dialogueTrigger.enabled = false;
theNPC.MoveRight();
}
}
}
else if (SceneManagement.ps4Controller == 1)
{
if (buttonPrompts[5].activeSelf && Input.GetKeyUp("joystick button 0"))
{
currentLine++;
if (currentLine >= sentences.Length)
{
nameDisplay.enabled = false;
dialogueBox.SetActive(false);
dialogueActive = false;
endDialogue = true;
characterVicinity = false;
dialogueTrigger.enabled = false;
theNPC.MoveRight();
}
}
}
else
{
//The button prompt must be active in conjunction with the Space bar being pressed before the next line will show.
if (buttonPrompts[3].activeSelf && Input.GetKeyUp(KeyCode.Space))
{
currentLine++;
if (currentLine >= sentences.Length)
{
nameDisplay.enabled = false;
dialogueBox.SetActive(false);
dialogueActive = false;
endDialogue = true;
characterVicinity = false;
dialogueTrigger.enabled = false;
theNPC.MoveRight();
}
}
}
}
if (textDisplay.text == sentences[index] && SceneManagement.xbox360Controller == 1)
{
buttonPrompts[4].SetActive(true);
continueAllowed = true;
if (Input.GetKeyUp("joystick button 2"))
{
continuePressed = true;
continueAllowed = false;
NextSentence();
}
else
{
continuePressed = false;
}
}
else if (textDisplay.text == sentences[index] && SceneManagement.ps4Controller == 1)
{
buttonPrompts[5].SetActive(true);
continueAllowed = true;
if (Input.GetKeyUp("joystick button 0"))
{
continuePressed = true;
continueAllowed = false;
NextSentence();
}
else
{
continuePressed = false;
}
}
else if(textDisplay.text == sentences[index])
{
buttonPrompts[3].SetActive(true);
continueAllowed = true;
//Placing this 'if' statement here within the previous one will stop the button from being spammed.
if (Input.GetKeyUp(KeyCode.Space))
{
continuePressed = true;
continueAllowed = false;
NextSentence();
}
else
{
continuePressed = false;
}
}
}
public void Hide()
{
buttonPrompts[0].SetActive(false);
buttonPrompts[1].SetActive(false);
buttonPrompts[2].SetActive(false);
}
public void Xbox360Prompts()
{
buttonPrompts[1].SetActive(true);
Invoke("Hide", 3f);
}
public void PS4Prompts()
{
buttonPrompts[2].SetActive(true);
Invoke("Hide", 3f);
}
public void PCPrompts()
{
buttonPrompts[0].SetActive(true);
Invoke("Hide", 3f);
}
public void ControllerDetection()
{
string[] names = Input.GetJoystickNames();
for (int x = 0; x < names.Length; x++)
{
//print(names[x].Length);
if (names[x].Length == 19)
{
//print("PS4 CONTROLLER IS CONNECTED");
pS4Controller = 1;
xbox360Controller = 0;
if (pS4Controller == 1)
{
//Debug.Log("PS4 controller detected");
}
}
else if (names[x].Length == 33)
{
//print("XBOX 360 CONTROLLER IS CONNECTED");
pS4Controller = 0;
xbox360Controller = 1;
if (xbox360Controller == 1)
{
//Debug.Log("Xbox 360 controller detected");
}
}
else
{
pS4Controller = 0;
xbox360Controller = 0;
}
if (xbox360Controller == 0 && pS4Controller == 0)
{
//Debug.Log("No controllers detected");
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class SceneManagement : MonoBehaviour
{
public GameObject[] buttonPrompts;
public bool entranceVicinity;
public bool exitVicinity;
public string levelToLoad;
public static int xbox360Controller = 0;
public static int ps4Controller = 0;
public GameObject exitLight
{
get
{
if (_exitLight != null)
return _exitLight;
_exitLight = GameObject.Find("Exit Light");
return _exitLight;
}
}
public static bool insideHut;
public static bool outsideHut;
public static bool backOutsideHut;
private GameObject _exitLight = null;
private PlayerController thePlayer;
void Start()
{
thePlayer = FindObjectOfType<PlayerController>();
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area"))
{
outsideHut = true;
insideHut = false;
backOutsideHut = false;
}
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("hut_interior"))
{
insideHut = true;
outsideHut = false;
backOutsideHut = false;
exitLight.SetActive(false);
}
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("start_area 2"))
{
backOutsideHut = true;
outsideHut = false;
insideHut = false;
}
}
public void OnTriggerEnter(Collider other)
{
if (outsideHut)
{
if (other.gameObject.CompareTag("Player"))
{
entranceVicinity = true;
exitVicinity = false;
ControllerDetection();
if (entranceVicinity && ps4Controller == 1)
{
PS4Prompts();
}
else if (entranceVicinity && xbox360Controller == 1)
{
Xbox360Prompts();
}
else
{
PCPrompts();
}
}
}
if (Pickup.objectsDisabled && NPC.leverActivated && insideHut)
{
if (other.gameObject.CompareTag("Player"))
{
entranceVicinity = false;
exitVicinity = true;
ControllerDetection();
if (exitVicinity && ps4Controller == 1)
{
PS4Prompts();
}
else if (exitVicinity && xbox360Controller == 1)
{
Xbox360Prompts();
}
else
{
PCPrompts();
}
}
}
else if(backOutsideHut)
{
if (other.gameObject.CompareTag("Player"))
{
SceneManager.LoadScene(levelToLoad);
}
}
}
public void OnTriggerExit(Collider other)
{
entranceVicinity = false;
exitVicinity = false;
}
public void Update()
{
if (entranceVicinity)
{
if (xbox360Controller == 1)
{
if (Input.GetKeyDown("joystick button 2"))
{
SceneManager.LoadScene(levelToLoad);
}
}
else if (ps4Controller == 1)
{
if (Input.GetKeyDown("joystick button 0"))
{
SceneManager.LoadScene(levelToLoad);
}
}
else
{
if (Input.GetKeyDown(KeyCode.Return))
{
SceneManager.LoadScene(levelToLoad);
}
}
}
else if (exitVicinity)
{
if (xbox360Controller == 1)
{
if (Input.GetKeyDown("joystick button 2"))
{
SceneManager.LoadScene(levelToLoad);
}
}
else if (ps4Controller == 1)
{
if (Input.GetKeyDown("joystick button 0"))
{
SceneManager.LoadScene(levelToLoad);
}
}
else
{
if (Input.GetKeyDown(KeyCode.Return))
{
SceneManager.LoadScene(levelToLoad);
}
}
}
if (Pickup.objectsDisabled && NPC.leverActivated && insideHut)
{
exitLight.SetActive(true);
}
}
public void Hide()
{
buttonPrompts[0].SetActive(false);
buttonPrompts[1].SetActive(false);
buttonPrompts[2].SetActive(false);
}
public void Xbox360Prompts()
{
buttonPrompts[1].SetActive(true);
Invoke("Hide", 3f);
}
public void PS4Prompts()
{
buttonPrompts[2].SetActive(true);
Invoke("Hide", 3f);
}
public void PCPrompts()
{
buttonPrompts[0].SetActive(true);
Invoke("Hide", 3f);
}
public void ControllerDetection()
{
string[] names = Input.GetJoystickNames();
for (int x = 0; x < names.Length; x++)
{
//print(names[x].Length);
if (names[x].Length == 19)
{
//print("PS4 CONTROLLER IS CONNECTED");
ps4Controller = 1;
xbox360Controller = 0;
if (ps4Controller == 1)
{
//Debug.Log("PS4 controller detected");
}
}
else if (names[x].Length == 33)
{
//print("XBOX 360 CONTROLLER IS CONNECTED");
ps4Controller = 0;
xbox360Controller = 1;
if (xbox360Controller == 1)
{
//Debug.Log("Xbox 360 controller detected");
}
}
else
{
ps4Controller = 0;
xbox360Controller = 0;
}
if(xbox360Controller == 0 && ps4Controller == 0)
{
//Debug.Log("No controllers detected");
}
}
}
}