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 shamblinwithshotguns · Feb 13, 2018 at 09:17 PM · raycasttouch controlsrotatearound

Using Touch/Mouse to rotate camera around gameobject

So, I'm trying to combine information (and code) from tutorials and documentation so that I can properly navigate in my game. This is an example of when my code worked (this was also when I was rotating the globe and not the camera like I should have been.) I put an invisible sphere around the globe that detects when the mouse is clicking on it, set the game camera as a child of that sphere, and the movement of the cursor would make the camera rotate around. However, I want the player to be able to interact with the continents and that would be impossible with the invisible sphere. So I looked up some proper mobile input controls and raycasting. At some point I would like to get it to a point where I can restrict the movement of the camera to prevent "snagging" at the north and south poles.

The code for the Touch Input is as follows:

 {
 // Designates the layer gameobjects will have to be in for the raycast to trigger
     public LayerMask touchInputMask;
 // A list of gameobjects that the player can drag across for the purposes of preventing issues when dragging back and forth
     private List<GameObject> touchList = new List<GameObject>();
     private GameObject[] touchesOld;
     private RaycastHit hit;

  //Detects input, sends a raycast to a game object, if it is of the touchInputMask layer it receives information about the nature of the user input.
     void Update()
     {
             if (Input.touchCount > 0)
             {
                 touchesOld = new GameObject[touchList.Count];
                 touchList.CopyTo(touchesOld);
                 touchList.Clear();
 
                 foreach (Touch touch in Input.touches)
                 {
                     Ray ray = GetComponent<Camera>().ScreenPointToRay(touch.position);

                     if (Physics.Raycast(ray, out hit, touchInputMask))
                     {
                         GameObject recipient = hit.transform.gameObject;
                         touchList.Add(recipient);
                         if (touch.phase == TouchPhase.Began)
                         {
                             recipient.SendMessage("OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver);
                         }
                         if (touch.phase == TouchPhase.Ended)
                         {
                             recipient.SendMessage("OnTouchUp", hit.point, SendMessageOptions.DontRequireReceiver);
                         }
                         if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
                         {
                             recipient.SendMessage("OnTouchStay", hit.point, SendMessageOptions.DontRequireReceiver);
                         }
                         if (touch.phase == TouchPhase.Canceled)
                         {
                             recipient.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
                         }                    }
                 }
                 foreach (GameObject g in touchesOld)
                 {
                     if (!touchList.Contains(g))
                     {
                         g.SendMessage("OnTouchExit", hit.point, SendMessageOptions.DontRequireReceiver);
                     }
                 }
             }
         }
     }
 }
 

The code for the Camera is as follows:

 {
     // In order: multiplier for rotation vector, two vectors to compare how far it should move in a single frame, and bool to make rotating stop and go
     private float _sensitivity;
     private Vector2 _inputRef, _inputOff;
     private Vector3 _rotation;
     private bool _isRotating;

     void Start () {
         _sensitivity = .4f;
         _rotation = Vector3.zero;
     }
 
     private void Update()
     {
         if (_isRotating)
         {
             foreach (Touch touch in Input.touches)
             {
                 _inputOff = (touch.position - _inputRef);
             }
             _rotation.y = _inputOff.x * _sensitivity;
             _rotation.x = -(_inputOff.y) * _sensitivity;
 
             transform.eulerAngles += _rotation;
         }
     }
 
     void OnTouchDown(Vector2 point)
     {
         _isRotating = true;
         _inputRef = new Vector2(point.x, point.y);
     }
 
     void OnTouchUp()
     {
         _isRotating = false;
     }
 
     void OnTouchStay(Vector2 point)
     {
         _isRotating = true;
         _inputRef = new Vector2(point.x, point.y);
     }
 
     void OnTouchExit()
     {
         _isRotating = false;
     }
 }


I'm not getting any errors, but the code is also not working at all and I have no idea where to start diagnosing. I'm also probably going about this entirely in the wrong way. Again, what I am attempting to do:

  • The player should be able to drag to move the camera around the globe.

  • The player should be able to also tap on continents to interact with them.

  • These two interactions will not interfere with each other.

  • The camera won't "snag" at the north and south poles.

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

Answer by Zodiarc · Feb 14, 2018 at 08:26 AM

You could try my answer from here. Here you can see it in action.

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

105 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How Do I add rotations to one of my objects in my AR application? 1 Answer

Touch controls for Pong based video games for Android 0 Answers

Touch controls for Pong game for Android devices 0 Answers

Paddle script for Pong-based video game for Android 0 Answers

how to rotatearound player and rotation eulerangle at the same time? 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