Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 k_kenji · Mar 01, 2017 at 07:24 AM · c#scripting problemuimousecameras

When touching the UI, it also affects overlapping game objects under the UI, causing the camera to move. How can I prevent it?

I created a scene like the uploaded image file, set it so that the camera moves around the sphere by mouse dragging. The problem is that if you touch the UI, the overlapping game objects under the UI will also be touched and the camera will move. I want to avoid touching the game object that overlaps with the UI object. what should i do? I am a novice script, so please tell me the script to solve this. Also, please tell me the setting method. Please. I used this script to rotate the camera around the sphere.

 using UnityEngine;
 using System.Collections;
 
 public class CameraRotate : MonoBehaviour
 {
     public Transform targetObject;
     public Vector3 targetOffset;
     public float averageDistance = 5.0f;
     public float maxDistance = 20;
     public float minDistance = .6f;
     public float xSpeed = 200.0f;
     public float ySpeed = 200.0f;
     public int yMinLimit = -80;
     public int yMaxLimit = 80;
     public int zoomSpeed = 40;
     public float panSpeed = 0.3f;
     public float zoomDampening = 5.0f;
     public float rotateOnOff = 1;
 
     private float xDeg = 0.0f;
     private float yDeg = 0.0f;
     private float currentDistance;
     private float desiredDistance;
     private Quaternion currentRotation;
     private Quaternion desiredRotation;
     private Quaternion rotation;
     private Vector3 position;
 
     
     void Start() { Init(); }
     void OnEnable() { Init(); }
 
     public void Init()
     {
         if (!targetObject)
         {
             GameObject go = new GameObject("Cam Target");
             go.transform.position = transform.position + (transform.forward * averageDistance);
             targetObject = go.transform;
         }
 
         currentDistance = averageDistance;
         desiredDistance = averageDistance;
         
         position = transform.position;
         rotation = transform.rotation;
         currentRotation = transform.rotation;
         desiredRotation = transform.rotation;
        
         xDeg = Vector3.Angle(Vector3.right, transform.right );
         yDeg = Vector3.Angle(Vector3.up, transform.up );
         position = targetObject.position - (rotation * Vector3.forward * currentDistance + targetOffset);
     }
 
     void LateUpdate()
     {  
         if (Input.GetMouseButton(2) && Input.GetKey(KeyCode.LeftAlt) && Input.GetKey(KeyCode.LeftControl))
         {
             desiredDistance -= Input.GetAxis("Mouse Y") * 0.02f  * zoomSpeed*0.125f * Mathf.Abs(desiredDistance);
         }
          else if (Input.GetMouseButton(0) )
         {
             xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
             yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
             yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
             
             desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
             currentRotation = transform.rotation;
                rotation = Quaternion.Lerp(currentRotation, desiredRotation, 0.02f  * zoomDampening);
             transform.rotation = rotation;
         }
 
         desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * 0.1f  * zoomSpeed * Mathf.Abs
             (desiredDistance);
         desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
         currentDistance = Mathf.Lerp(currentDistance, desiredDistance, 0.1f  * zoomDampening);
         position = targetObject.position - (rotation * Vector3.forward * currentDistance + 
             targetOffset);
         transform.position = position;
 
     }
 
     private static float ClampAngle(float angle, float min, float max)
     {
         if (angle < -360)
             angle += 360;
         if (angle > 360)
             angle -= 360;
         return Mathf.Clamp(angle, min, max);
     }
 }

[1]: /storage/temp/89078-i01.jpg

i01.jpg (162.4 kB)
Comment
Add comment · Show 1
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 k_kenji · Mar 01, 2017 at 10:12 AM 0
Share

Harshad$$anonymous$$ Thanks for the reply.

I want to confirm, On line 61 "Else if (Input.Get$$anonymous$$ouseButton (0))" From Else if (Input.Get$$anonymous$$ouseButton (0) && EventSystem.current! = Null && EventSystem.current.IsPointerOverGameObject ()) I changed it but it did not work. Is something wrong?

1 Reply

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

Answer by HarshadK · Mar 01, 2017 at 08:53 AM

In your you can use EventSystem.IsPointerOverGameObject() to check if pointer is over an UI element and just ignore that input.

Replace line:

  else if (Input.GetMouseButton(0) )

with:

  else if (Input.GetMouseButton(0) && EventSystem.current != null && EventSystem.current.IsPointerOverGameObject())


Comment
Add comment · Show 3 · 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 k_kenji · Mar 01, 2017 at 10:21 AM 0
Share

Harshad$$anonymous$$ Thanks for the reply.

I want to confirm, On line 61 "Else if (Input.Get$$anonymous$$ouseButton (0))" From Else if (Input.Get$$anonymous$$ouseButton (0) && EventSystem.current! = Null && EventSystem.current.IsPointerOverGameObject ()) I changed it but it did not work. Is something wrong?

avatar image HarshadK k_kenji · Mar 01, 2017 at 11:10 AM 0
Share

Oh, it should run the code when pointer is not over UI gameobject. Currently it should be doing the opposite. It should check for false of EventSystem.current.IsPointerOverGameObject()

Just check with line below:

 else if (Input.Get$$anonymous$$ouseButton(0) && EventSystem.current != null && !EventSystem.current.IsPointerOverGameObject())

avatar image k_kenji HarshadK · Mar 02, 2017 at 07:32 AM 0
Share

Thank you for your reply. I did it! Thank you very much. I have been troubled with this for a long time, so I can proceed with work. ARIGATO! You are the best person. When another problem arises in the future, please tell me the solution.

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

Trigger Sound more than Once 0 Answers

How to get the GameObject in a callback function 1 Answer

Inspector changes set TextMeshPro values on play 1 Answer

Unity 2D - Text UI and Textbox scripting issue - HELP ASAP! 2 Answers

NullReferenceException: Object reference not set to an instance of an object. (Cameras switching) 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