Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 BlackSnow_2 · Aug 28, 2015 at 08:21 AM · gui3dhudspace shooterprediction

Object to always appear same size on HUD

I had a hard time deciding the title of this question because it's a very open-ended question (As I have nothing set in stone yet).

Essentially, what I'm doing is implementing a target "predictor" object that sits a certain amount (Some maths calculation including projectile speed, distance and enemy velocity) ahead of enemy ships (It's a space dog fighter). Now, this question is two fold:

  1. How would I make this "Predictor" object stay the same size to the camera no matter its distance?

  2. How can I accurately measure where the bullets will fire to? Currently, the camera is above where the bullets spawn so it obviously isn't the center of the camera. I want to put a HUD item similar to crosshairs on the screen.

I apologize for the vague nature of this question but I can't really implement a "predictor" until the enemies have simple instructions to move around.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Halfbiscuit · Aug 28, 2015 at 10:00 AM

1: Have you used the UI system at all?

If you have a Camera object in script using Camera.Main you can use the WorldToScreenPoint function.

With this you position a UI element: Image, text, whatever you want and it will always appear the same size. I use this for the health bars in my game by having a health bar object be a child of the players/enemies and follow above them.

2: Well for the second question I have some prediction code:

 float time = Distance/BulletSpeed;
 PredictedTargetPos = TargetPos + (TargetVel * time);

Then for more accuracy you can do this as many times as nessesary:

 float TimeToNewTargetPos = Vector3.Distance(PredictedTargetPos, TargetPos)/BulletSpeed;
 m_PredictedTargetPos = PredictedTargetPos + (TargetVel * TimeToNewTargetPos);
Comment
Add comment · Show 1 · 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 BlackSnow_2 · Aug 28, 2015 at 12:03 PM 0
Share

I admit, I haven't really used the UI system very much (Aside from buttons and keeping a compass in a panel). Though, I will point out this is my first game and quite a rushed job because it's for an assessment due Wednesday. (We were meant to do it in Scratch but it physically hurt me so I decided not to).

You actually gave me a formula for the first question ins$$anonymous$$d of answering the second question (Which is still very much appreciated). $$anonymous$$y second question was more about a cross hair accurately depicting where the bullets will fly to and hit. Perhaps a laser-pointer like function? I'm not quite sure.

EDIT: I've started writing up a basic script for the enemy ships to attack run but I've realised that each enemy ship that fires bullet projectiles will need its own predictor or some way to predict for its self, any ideas?

avatar image
0

Answer by pavlito · Jan 23, 2016 at 01:41 PM

You can create a world space canvas as the child of the object you wish to display the information of. Then set the desired width and height. I'm going to use 800 x 800 pixels. In order to bring this to proper world size, decrease the scale of the canvas to 0.005 on each axis. This means that your canvas is now 4m in width and height in world space. (800 x 0.005 = 4). You can set any combo of scale and surface size of the canvas to get the results you need.

Now that you have this setup, you need to tweak it with some code. Here's my example.

 using UnityEngine;
 using System.Collections;
 
 public class KeepScreenSize : MonoBehaviour {
 
     private float sizeModifier, cameraDistance;
     private RectTransform canvasTransform;
     private Vector3 tmpLocalScale = new Vector3();
     
     // Use this for initialization
     void Start ()
     {
         canvasTransform = GetComponent<RectTransform>();
         // get distance between camera and 0,0,0. Use this info to calculate size modifier.
         cameraDistance = Vector3.Distance(Camera.main.transform.position, Vector3.zero);
         sizeModifier = cameraDistance / canvasTransform.localScale.x;
     }
     
     // Now that we have the modifier, use varying camera distance to calculate the size of canvas
     void LateUpdate ()
     {
         cameraDistance = Vector3.Distance(Camera.main.transform.position, this.transform.position);
         tmpLocalScale.x = cameraDistance / sizeModifier;
         tmpLocalScale.y = tmpLocalScale.x;
         tmpLocalScale.z = tmpLocalScale.x;
 
         // Apply new scale
         canvasTransform.localScale = tmpLocalScale;
         // rotate canvas to be perpendicular to camera regardless of camera rotation.
         // Notice we don't use LookAt(camera). This would mirror the canvas.
         canvasTransform.rotation = Camera.main.transform.rotation;
     }
 }

Attach this script to the canvas object. You can use whatever reference you want for getting the sizeModifier (I usually use Vector3.zero), this value is virtual and it doesn't really matter in the beginning. Setting this modifier in Start() allows you to tweak the scale of the canvas in Editor and just hit run. You don't need to recalculate anything. If everything goes well, you should be able to create multiple objects with varying distance from the camera, each with his own modified canvas scale. Note, the canvas will slightly get "over" increased as you move your objects more and more to the left or right of the camera. It's due to the new distance, which is a diagonal and larger than the forward distance (cathetus).

Hope this helps.

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
0

Answer by micheal332001 · May 26, 2019 at 05:18 PM

I'v changed this a bit as the size on the y was not right

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class KeepScreenSize : MonoBehaviour
     {
     
         public float  sizeModifier, sizeModifierY, cameraDistance;
         public RectTransform canvasTransform;
         public Vector3 tmpLocalScale = new Vector3();
     
         void Start()
         {
             canvasTransform = GetComponent<RectTransform>();
             cameraDistance = Vector3.Distance(Camera.main.transform.position, Vector3.zero);
             sizeModifier = cameraDistance / canvasTransform.localScale.x;
             sizeModifierY = cameraDistance / canvasTransform.localScale.y;
         }
     
         void LateUpdate()
         {
             cameraDistance = Vector3.Distance(Camera.main.transform.position, this.transform.position);
             tmpLocalScale.x = cameraDistance / sizeModifier;
             tmpLocalScale.y = cameraDistance / sizeModifierY;
     
             canvasTransform.localScale = tmpLocalScale;
             canvasTransform.rotation = Camera.main.transform.rotation;
         }
     }


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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to position 3D-GUI-Mesh on change of aspect ratio? 0 Answers

If I have a button over my 3d world, how can I detect if a mouseClick was in the world or not? 5 Answers

GUIs for Power ups? No idea. 1 Answer

CoolDown Animation on the GUI 1 Answer

2D Arrow pointing at 3D Position 1 Answer


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