Page 1 of 1

Snap Dialogue UI to NPC

Posted: Sun Jul 01, 2018 5:26 pm
by focusonfungames
For my VR game I'm setting the dialogue UI window to be attached to the NPC. I've included a GitHub gist to the code in case anyone in the future needs it. This script should be attached to your NPC.



Here's a copy in case on day GitHub disappears:

Code: Select all

/*
MIT License

Copyright (c) 2018 John Rockefeller

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SnapDialogueUIToNPC : MonoBehaviour {

	public Canvas canvas;
	public Vector3 offset = new Vector3(0.0f, -0.65f, 1.1f);

	private void OnUse(Transform actor) {
		var camTransform = Camera.main.transform;
		canvas.transform.position = camTransform.position + camTransform.forward * offset.z + camTransform.right * offset.x + Vector3.up * offset.y;
		canvas.transform.rotation = Quaternion.Euler(canvas.transform.rotation.eulerAngles.x, camTransform.rotation.eulerAngles.y, canvas.transform.rotation.eulerAngles.x);
		canvas.transform.SetParent (gameObject.transform);
	}
}

Re: Snap Dialogue UI to NPC

Posted: Sun Jul 01, 2018 5:27 pm
by focusonfungames
I'll update the gist as I discover bugs and make improvments as my game is developed.

Re: Snap Dialogue UI to NPC

Posted: Sun Jul 01, 2018 6:42 pm
by Tony Li
Awesome! Thanks for sharing that. BTW, you could change OnUse to OnConversationStart. That way the dialogue UI will snap to the NPC even if the conversation starts in a manner other than OnUse, such as OnTriggerEnter.

Re: Snap Dialogue UI to NPC

Posted: Sun Jul 01, 2018 11:44 pm
by focusonfungames
Thanks! I had it that way originally, but I could've swore I saw OnConversationStart as being marked as deprecated in the docs. I can't that note again, so I'll switch to OnConversationStart.