0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

unity_インタラクトした際にプレイヤーが転倒する

Posted at

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

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();
        }
    }
}

}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?