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 stephenlegrand · Jul 06, 2013 at 04:30 AM · cameratriggerdirection

How to trigger with camera?

I'm trying to make a scary game, and at one point a sound will go off behind the player. They will (In theory) turn around to investigate, and while their back is turned an object (a zombie or something) will appear behind them, giving them a scare when the turn back around. What code would I use to trigger a script or animation when the camera is pointed a certain direction? Do I use collision boxes? A piece of code? Magic? Help!

Comment
Add comment · Show 2
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 Lo0NuhtiK · Jul 06, 2013 at 04:30 AM 0
Share

$$anonymous$$agic, yeah, definitely; definitely magic.

avatar image Benproductions1 · Jul 06, 2013 at 06:15 AM 0
Share

Ima go with magic too on this one.
A famous person once said something similar to this: "$$anonymous$$agic is just the science that is beyond us"

Take that however you like ;)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by maddymac · Jul 06, 2013 at 06:57 AM

Hi I am assuming it is First Person camera, so have a dummy object behind the player\camera and check when "Renderer.isVisible" or "Renderer.OnBecameVisible" becomes active. when it does, trigger your "zombie" spawn or anything you want.

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 robertbu · Jul 06, 2013 at 07:13 AM

Answering your question in bold: One easy way to trigger something when the camera is pointed in a certain direction would be to compare two vectors using Vector3.Angle(). Vector3.Angle() returns an unsigned angle between two vectors. So if the two vectors are pointed in a similar direction, then the angle between the vectors will be below some threshold. In the case of game objects like cameras and characters, you can use the 'transform.forward' to say where they are pointing. But having two vectors pointing in the same direction does not mean that one is behind the other. For example one may be beside the other. But if one vector is looking at the game object of the other vector, and if the angle is small, then the one is behind the other.

Here is a script that causes one game object to "circle around and sneak up" on another game object when that other game object has its back turned:

 #pragma strict 
 
 var speed = 0.85;
 private var player : GameObject;
 
 function Start() {
     player = GameObject.Find("Player");
 }
  
 function Update() {
     transform.LookAt(player.transform);
 
     if (Vector3.Angle(player.transform.forward, transform.forward) < 45.0) {
         transform.Translate(transform.forward * speed * Time.deltaTime);
     }
 }
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

18 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

Related Questions

Move character in direction its facing 1 Answer

Need help with third-person camera stuttering involving raycasts and clamp 0 Answers

Can't get AR camera to trigger events 0 Answers

Not shooting in desired direction 1 Answer

Getting 3rd person to look in direction of camera when right mouse clicked 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