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 ca1ek · Jun 12, 2014 at 08:05 PM · camerathird-person

Drawing architecture normally, but with key items only visible in front of the player

Sorry if the question was hard to understand, but I don't know how to describe it myself. I want to make a game with the camera over the map(but not isometric), with architecture drawn all the time, but with enemies, key items, only drawn in a field in front of the controlled model. Something like in the "Monaco" video game, but in 3D.

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 supernat · Jun 12, 2014 at 08:44 PM

Uploading an image of what you want would go a long way, but I think I understand. You want the 3rd person view over the player's character, but you want to only see (from the 3rd person camera) what the player would see if they were walking on the terrain in the game. Correct?

It's a simple matter of trigonometry. You need to project a cone forward from the player, test all enemies in the scene to lie either inside or outside of that cone, and enable or disable their renderers. Make sure the cone is projected in the local body coordinates of the player so that when the player character turns left or right, the cone is always coming out of his/her eyes. When you do the dot product, go back to world coordinates for ease. I chose a cone because it can be easily represented with a single direction cosine value.

If you take the dot product between two normalized vectors, you end up with the cosine of the angle between them (called the direction cosine). It's always between -1 and +1 where 0 means the two lines are exactly 90 deg apart (orthogonal), +1.0 means they are completely parallel, and -1 means they are 180 degrees apart. If you take the dot product of the player's line of sight (the character down on the terrain I mean), with the vector created from the player to a given enemy, you can do a simple check if that value is within a tolerance. Don't take the acos(dot prod) to get the angle, that's wasteful, just compare the direction cosine itself. Something like this:

 // Run this somewhere in a script located on the main character game object
 Vector3 playerLOS = transform.forward;
 Vector3 enemyVector = (enemy.transform.position - transform.position).normalized;
 float dirCos = Vector3.Dot(playerLOS, enemyVector);
 if (dirCos > 0.8f)
     // Turn on enemy renderer
 else
     // Turn off enemy renderer

There's a simplified approach but more costly in terms in method calls, because it adds extra normalization calls and computing acos which are expensive, but you could say something like this:

 if (Vector3.Angle(transform.forward, enemy.transform.position - transform.position) < 15.0f)
     // Turn on enemy renderer
 else
     // Turn off enemy renderer

You'll want to do these operations continuously but intelligently. You could split the entire set of enemies up into 10 groups for instance, especially if you have a lot of them, and each frame in the Update() method of your class, only process turning the enemy renders on and off for 1 group at a time. You could process them all in a separate thread or even just do one enemy at a time in a coroutine if you don't have a lot of enemies. That's up to you. Also, you may want to only process them when an enemy is actually moving or the player is moving.

EDIT: A couple of other notes. the value I used above of 0.8 = cos(angle), so take acos(0.8) to see what angle that is. If you process turning the renderers on/off in a thread, it has to be thread safe, because you can't change actual components or their values from a non-main thread without crashing, so you'd have to buffer up the list. This all really depends on how many enemies you will have active in the world, as you could just process them all each frame, but if you do it for mobile, it's one way to save a little processing time. The cone will truly behave like a cone, meaning enemies that aren't near the axis of the cone (i.e. airplanes flying high up) will be culled with this method. To get those type of enemies, you'd want to create to planes, sort of like the left and right walls of a view frustum, and then determine if targets lie in front of or behind those planes (they must be in front of both).

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

ThirdPersonShooter-Camera-Script Optimization 0 Answers

third person camera script? 1 Answer

reduce speed at which main camera rotates... 1 Answer

third person camera 1 Answer

Toggling between two cameras/camera fix on fallout. 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