Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 Chocolade · Oct 18, 2016 at 07:17 PM · c#scripting problemscript.scriptingbasics

How can i check and fire an event when the user look at specific object ?

For example in my case i have a lot of objects moving around up in the sky. I want to make that each time the user look up at the objects to fire the event. For example if i have 60 spheres moving in the sky above the player when he look up and the player look get to see the objects the spheres then fire the event. Not just look up but only if the player see the objects the spheres.

I'm not sure if it's relative to the camera view or it should be something with the character it self face ? The character in my case is ThirdPersonController.

I saw some solutions in the answer in this question

Question with some solutions

but this is relative to the camera/s view. And i want to use the ThirdPersonCharacter view. My character view is controlled by a child main camera under the ThirdPersonCharacter with a script so i can move the mouse 360 degrees around. So i guess it's relative to this camera. I want when i move the mouse around up and the user see the Spheres it's important to know when he see them and not just to look up then fire the event.

  1. Not sure what event/s to register to and how to fire them .

  2. Not sure how to make "seen" event so only when the user/camera saw the specific object/s then fire the trigger.

This is my camera script:

 using UnityEngine;
 using System.Collections;
 
 public class CameraScript : MonoBehaviour {
 
 
     public Transform TargetLookAt;
 
     public float Distance = 5.0f;
     public float DistanceMin = 3.0f;
     public float DistanceMax = 10.0f;
 
     private float mouseX = 0.0f;
     private float mouseY = 0.0f;
     private float startingDistance = 0.0f;    
     private float desiredDistance = 0.0f;
 
     public float X_MouseSensitivity = 5.0f;
     public float Y_MouseSensitivity = 5.0f;
     public float MouseWheelSensitivity = 5.0f;
     public float Y_MinLimit = -40.0f;
     public float Y_MaxLimit = 80.0f;
 
     public float DistanceSmooth = 0.05f;    
     private float velocityDistance = 0.0f;    
     private Vector3 desiredPosition = Vector3.zero;
 
     public float X_Smooth = 0.05f;
     public float Y_Smooth = 0.1f;
     private float velX = 0.0f;
     private float velY = 0.0f;
     private float velZ = 0.0f;
     private Vector3 position = Vector3.zero;
 
     //CursorLockMode wantedMode;    
 
 
     void  Start (){
         Distance = Mathf.Clamp(Distance, DistanceMin, DistanceMax);
         startingDistance = Distance;
         Reset();
         //SetCursorState();
         //OnGUI();
     }
 
     void Update(){
     }
 
     void  FixedUpdate (){
         if (TargetLookAt == null)
             return;
 
         HandlePlayerInput();
 
         CalculateDesiredPosition();
 
         UpdatePosition();
     }
 
     void  HandlePlayerInput (){
         float deadZone= 0.01f; // mousewheel deadZone
 
         //if (Input.GetMouseButton(1))
         //{
         mouseX += Input.GetAxis("Mouse X") * X_MouseSensitivity;
         mouseY -= Input.GetAxis("Mouse Y") * Y_MouseSensitivity;
         //}
 
         // this is where the mouseY is limited - Helper script
         mouseY = ClampAngle(mouseY, Y_MinLimit, Y_MaxLimit);
 
         // get Mouse Wheel Input
         if (Input.GetAxis("Mouse ScrollWheel") < -deadZone || Input.GetAxis("Mouse ScrollWheel") > deadZone)
         {
             desiredDistance = Mathf.Clamp(Distance - (Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity), 
                 DistanceMin, DistanceMax);
         }
     }
 
     void  CalculateDesiredPosition (){
         // Evaluate distance
         Distance = Mathf.SmoothDamp(Distance, desiredDistance, ref velocityDistance, DistanceSmooth);
 
         // Calculate desired position -> Note : mouse inputs reversed to align to WorldSpace Axis
         desiredPosition = CalculatePosition(mouseY, mouseX, Distance);
     }
 
     Vector3  CalculatePosition ( float rotationX ,   float rotationY ,   float distance  ){
         Vector3 direction = new Vector3(0, 0, -distance);
         Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0);
         return TargetLookAt.position + (rotation * direction);
     }
 
     void  UpdatePosition (){
         float posX= Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, X_Smooth);
         float posY= Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, Y_Smooth);
         float posZ= Mathf.SmoothDamp(position.z, desiredPosition.z, ref velZ, X_Smooth);
         position = new Vector3(posX, posY, posZ);
 
         transform.position = position;
 
         transform.LookAt(TargetLookAt);
     }
 
     void  Reset (){
         mouseX = 0;
         mouseY = 10;
         Distance = startingDistance;
         desiredDistance = Distance;
     }
 
     float ClampAngle ( float angle ,   float min ,   float max  ){
         while (angle < -360 || angle > 360)
         {
             if (angle < -360)
                 angle += 360;
             if (angle > 360)
                 angle -= 360;
         }
 
         return Mathf.Clamp(angle, min, max);
     }
 
     // Apply requested cursor state
     /*void SetCursorState ()
     {
         Cursor.lockState = wantedMode;
         // Hide cursor when locking
         Cursor.visible = (CursorLockMode.Locked != wantedMode);
     }
 
     void OnGUI ()
     {
         GUILayout.BeginVertical ();
         // Release cursor on escape keypress
         if (Input.GetKeyDown (KeyCode.Escape))
             Cursor.lockState = wantedMode = CursorLockMode.None;
 
         switch (Cursor.lockState) {
         case CursorLockMode.None:
             GUILayout.Label ("Cursor is normal");
             if (GUILayout.Button ("Lock cursor"))
                 wantedMode = CursorLockMode.Locked;
             if (GUILayout.Button ("Confine cursor"))
                 wantedMode = CursorLockMode.Confined;
             break;
         case CursorLockMode.Confined:
             GUILayout.Label ("Cursor is confined");
             if (GUILayout.Button ("Lock cursor"))
                 wantedMode = CursorLockMode.Locked;
             if (GUILayout.Button ("Release cursor"))
                 wantedMode = CursorLockMode.None;
             break;
         case CursorLockMode.Locked:
             GUILayout.Label ("Cursor is locked");
             if (GUILayout.Button ("Unlock cursor"))
                 wantedMode = CursorLockMode.None;
             if (GUILayout.Button ("Confine cursor"))
                 wantedMode = CursorLockMode.Confined;
             break;
         }
 
         GUILayout.EndVertical ();
 
         SetCursorState ();
     }*/
 }


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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why when using get; set; in one script i'm getting null in other script that use it ? 1 Answer

How can i move the objects once they are created in random positions but only inside a specific given area ? 1 Answer

How can I animate linerenderer lines over time ? 1 Answer

How can i rotate the camera to the player facing direction ? 1 Answer

how can I change a light with multiple triggers. ? 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