Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cbacary4 · Feb 03, 2021 at 08:59 PM · cameravrhud

Make VR HUD For health bar and mana.

Hello, I have two 3d Meshes that I need to put in as my HUD. I attached these two 3d Meshes to my main camera and positioned them accordingly. I also made a shader for them so they can be seen through walls, that way you can always see your health and mana. The problem with this is it looks very odd. It looks like the 3d objects are moving (which they are) while in the VR headset rather than being in the same spot relative to the camera, which overall just looks bad. Does anyone have any recommendations for how to create a proper HUD for VR. I am making a game where the player can move around the map, so a HUD the user can always see is necessary.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by cbacary4 · Feb 08, 2021 at 06:43 PM

Well I figured it out. Here is the solution I used. I basically make the object act as if it were a child of the Camera only via code this way I can still manipulate the position propperly. Then Vector3.SmoothDamp and the Github I found with Quanternion SmoothDamp, take care of the position and rotation.

 using UnityEngine;
 
 public class HUDTracker : MonoBehaviour
 {
 
     private Transform fakeParent;
     private Vector3 _positionOffset;
     private Quaternion _rotationOffset;
     private Camera cameraObj;
     private Transform cameraTransform;
     private Vector3 velocity = Vector3.zero;
 
     void Start()
     {
         cameraObj = Camera.main;
         transform.localPosition = new Vector3(cameraObj.transform.localPosition.x - 0.3f, cameraObj.transform.localPosition.y - 2.2f, cameraObj.transform.localPosition.z + 1.2f); 
 
         // Set parent object
         SetFakeParent(Camera.main.transform);
 
         cameraTransform = cameraObj.transform;
     }
 
     // Update is called once per frame
     void Update()
     {
         // this is the desired position for the HUD.
         Vector3 offsetHUDPos = cameraTransform.TransformPoint(new Vector3(cameraObj.transform.localPosition.x, cameraObj.transform.localPosition.y - 2.2f, cameraObj.transform.localPosition.z + 1.2f));
         
         var temp = new Quaternion(0.3f, 0.3f, 0.3f, 0.3f);
         transform.rotation = SmoothDamp(transform.rotation, fakeParent.rotation * _rotationOffset, ref temp, 0.05f);
 
         // Change position with SmoothDamp so it moves smoothly.
         transform.position = Vector3.SmoothDamp(transform.position, offsetHUDPos, ref velocity, 0.2f);
     }
 
     public void SetFakeParent(Transform parent)
     {
         //Offset vector
         _positionOffset = transform.InverseTransformPoint(transform.position) - transform.InverseTransformPoint(parent.position);
         //Offset rotation
         _rotationOffset = Quaternion.Inverse(parent.rotation) * transform.rotation;
         //Our fake parent
         fakeParent = parent;
     }
 
     private Quaternion SmoothDamp(Quaternion rot, Quaternion target, ref Quaternion deriv, float time)
     {
         if (Time.deltaTime < Mathf.Epsilon) return rot;
         // account for double-cover
         var Dot = Quaternion.Dot(rot, target);
         var Multi = Dot > 0f ? 1f : -1f;
         target.x *= Multi;
         target.y *= Multi;
         target.z *= Multi;
         target.w *= Multi;
         // smooth damp (nlerp approx)
         var Result = new Vector4(
             Mathf.SmoothDamp(rot.x, target.x, ref deriv.x, time),
             Mathf.SmoothDamp(rot.y, target.y, ref deriv.y, time),
             Mathf.SmoothDamp(rot.z, target.z, ref deriv.z, time),
             Mathf.SmoothDamp(rot.w, target.w, ref deriv.w, time)
         ).normalized;
 
         // ensure deriv is tangent
         var derivError = Vector4.Project(new Vector4(deriv.x, deriv.y, deriv.z, deriv.w), Result);
         deriv.x -= derivError.x;
         deriv.y -= derivError.y;
         deriv.z -= derivError.z;
         deriv.w -= derivError.w;
 
         return new Quaternion(Result.x, Result.y, Result.z, Result.w);
     }
 
     /* 
      * SmoothDamp function acquired from https://gist.github.com/maxattack/4c7b4de00f5c1b95a33b
      * Copyright 2016 Max Kaufmann (max.kaufmann@gmail.com)
      * 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:
      *
     */
 }


 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by rh_galaxy · Feb 04, 2021 at 11:28 AM

I placed mine in 3d game space and move it with the camera, which makes it not visible all the time but you can look at it if you like.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

211 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

HUD wont show with Cardboard 0 Answers

Google Cardboard Cannot See Reticle 1 Answer

Camera image flickers with SteamVR 0 Answers

Can I render two cameras simultaneously? 1 Answer

UI camera not matching main camera in VR 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges