Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 BBrown4 · May 06, 2014 at 10:30 PM · positionstargetsinverted

Target Indicators Display On Two Sides

Ok, so I didn't really know how to word that question. Here is what's happening. I am using a very crude, but simple way to add target indicators to the game I'm working on. The problem is, while they display on the enemies, they are also displaying on the OPPOSITE side. Here are some images:

Here's facing front alt text

And here's facing back alt text

And here is the oh so complicated code:

 using UnityEngine;
 using System.Collections;
 
 public class TargetIndicator : MonoBehaviour {
     public Transform target;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 wantedPos = Camera.main.WorldToViewportPoint(target.position);
         transform.position = wantedPos;
     }
 }

I can't seem to figure out why there are two sets of indicators. Oh also, I should mention the GUI texture is a child of each enemy with the code displayed above attached to the Texture. It feels very crude and slammed together, but it's really simple and works fro what I need, if I could keep it that way, then it'd be great.

The theory that I have as to why it's doing this, is because the script is attached to the texture, which is attached to the enemy. Is it possible that the enemy is displaying the same texture, just with the coordinates inverted?

Anyhow, both sets of indicators move, and as the actual enemies pass by me, the other set of indicators passes by as well, heading in the opposite direction of the enemies. It's pretty strange.

Any and all help is deeply appreciated.

targind2.png (21.8 kB)
targind1.png (290.0 kB)
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

1 Reply

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

Answer by robertbu · May 06, 2014 at 10:33 PM

If I understand you correctly, this will fix your issue:

 void Update () {
    Vector3 wantedPos = Camera.main.WorldToViewportPoint(target.position);
    transform.position = wantedPos;
    renderer.enabled = wantedPosition.z > 0.0f;
 }

That is, when doing a WorldToViewportPoint() the returned 'z' value indicates the distance in front of the camera. Negative values are behind the camera, and positive values are in front of the camera. So this code turns off the renderer if the the position is behind the camera.

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 BBrown4 · May 07, 2014 at 04:30 AM 0
Share

Close, but no cigar. This requires that there be a renderer attached to the object. I am simply using a GUI Texture. I can try playing around with it, maybe add a sprite renderer and mess with that, see if it will fix the issue, but if there is anything else you can think of, I'd love to hear.

EDIT Fixed it, just had to make a slight change. Thanks so much robert :)

if you could update your post to display the correct answer, or should I say the $$anonymous$$ORE correct answer lol. Ins$$anonymous$$d of using

 renderer.enabled = worldPos.z > 0.0f;

I used:

 this.GetComponent<GUITexture>().enabled = worldPos.z > 0.0f;

Because there was no renderer attached to the object, it was just a straight GUITexture

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

20 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

Related Questions

Grouping Enemies/Targets 1 Answer

Moving an object between two positions (C#) 1 Answer

local space position and world space position do not in code match a game object with no parent in inspector, 0 Answers

Scripted rotation is inverted- GUI HUD. 0 Answers

navmesh multiple targets 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