unity初心者の備忘録。
インタラクト時にプレイヤーオブジェクトが転倒する問題の対処法。

1.プレイヤーオブジェクトのRigidbodyのConstraints(制限・制約の意)を確認する
2.【回転を固定】のXYZ軸にチェックを入れる
※【位置を固定】にチェックを入れると、階段を下りるなどの挙動の際に思った通りに動かない可能性あり。
以下、Dialoguesystemのコード全文(made Gemini)
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.Rendering.PostProcessing;
public class DialogueSystem : MonoBehaviour
{
public enum CharacterSpeaking
{
Player,
NPC
}
[System.Serializable]
public class DialogueLine
{
[TextArea(3, 5)]
public string line;
public Sprite[] characterSprites;
public CharacterSpeaking speakingAs;
}
public DialogueLine[] dialogueLines;
public DialogueLine[] itemDialogueLines;
public GameObject dialoguePanel;
public TMP_Text dialogueText;
public Image playerImage;
public Image npcImage;
public GameObject playerCharacter;
[SerializeField]
private PlayerMovement playerMovement;
[SerializeField]
private CameraController cameraController;
private Rigidbody playerRigidbody;
[SerializeField]
private PostProcessVolume postProcessVolume;
public KeyCode nextLineKey = KeyCode.Space;
public float typingSpeed = 0.05f;
private int currentLineIndex = 0;
private bool dialogueIsActive = false;
private Coroutine typingCoroutine;
void Start()
{
if (playerCharacter != null)
{
playerMovement = playerCharacter.GetComponent<PlayerMovement>();
playerRigidbody = playerCharacter.GetComponent<Rigidbody>();
cameraController = FindObjectOfType<CameraController>();
}
if (postProcessVolume == null)
{
postProcessVolume = FindObjectOfType<PostProcessVolume>();
}
if (postProcessVolume != null)
{
postProcessVolume.weight = 0f;
}
}
public bool IsDialogueActive()
{
return dialogueIsActive;
}
public void OnInteract()
{
StartDialogue();
}
public void StartDialogue()
{
dialogueIsActive = true;
currentLineIndex = 0;
dialoguePanel.SetActive(true);
if (playerRigidbody != null)
{
playerRigidbody.isKinematic = true;
}
if (playerMovement != null)
{
playerMovement.SetCanMove(false);
}
if (cameraController != null)
{
cameraController.SetCanLook(false);
}
if (postProcessVolume != null)
{
postProcessVolume.weight = 1f;
}
typingCoroutine = StartCoroutine(TypeLine(dialogueLines[currentLineIndex]));
}
IEnumerator TypeLine(DialogueLine line)
{
playerImage.gameObject.SetActive(false);
npcImage.gameObject.SetActive(false);
if (line.speakingAs == CharacterSpeaking.Player)
{
if (playerImage != null && line.characterSprites.Length > 0)
{
playerImage.sprite = line.characterSprites[0];
playerImage.gameObject.SetActive(true);
}
}
else
{
if (npcImage != null && line.characterSprites.Length > 0)
{
npcImage.sprite = line.characterSprites[0];
npcImage.gameObject.SetActive(true);
}
}
dialogueText.text = "";
foreach (char c in line.line.ToCharArray())
{
dialogueText.text += c;
yield return new WaitForSeconds(typingSpeed);
}
}
private void EndDialogue()
{
dialogueIsActive = false;
dialoguePanel.SetActive(false);
playerImage.gameObject.SetActive(false);
npcImage.gameObject.SetActive(false);
if (playerRigidbody != null)
{
playerRigidbody.velocity = Vector3.zero;
playerRigidbody.angularVelocity = Vector3.zero;
playerCharacter.transform.rotation = Quaternion.Euler(0, playerCharacter.transform.rotation.eulerAngles.y, 0);
playerRigidbody.isKinematic = false;
}
if (playerMovement != null)
{
playerMovement.SetCanMove(true);
}
if (cameraController != null)
{
cameraController.SetCanLook(true);
}
if (postProcessVolume != null)
{
postProcessVolume.weight = 0f;
}
}
private void Update()
{
if (dialogueIsActive && Input.GetKeyDown(nextLineKey))
{
if (typingCoroutine != null)
{
StopCoroutine(typingCoroutine);
dialogueText.text = dialogueLines[currentLineIndex].line;
typingCoroutine = null;
return;
}
currentLineIndex++;
if (currentLineIndex < dialogueLines.Length)
{
typingCoroutine = StartCoroutine(TypeLine(dialogueLines[currentLineIndex]));
}
else
{
EndDialogue();
}
}
}
}