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 ratboy · Jun 30, 2012 at 10:13 AM · 2diosspritetouchrotate

Finding the centre of two touches

Hello all, today i'm creating a pinch rotate script for my 2d sprite based game, now, i know i need to do the following: 1. Check if two fingers are touching 2. Find the centre of these two touches 3. Cast a ray 4. Does the ray hit a rigid body? 5. If so, the rigid body can be rotated

I've already done the majority of it, but whats the best way to find the centre of two touches? Bearing in mind its 2D and all objects are on a fixed Z Plane...

Any help would be appreciated, cheers. :)

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

2 Replies

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

Answer by dscroggi · Jun 30, 2012 at 11:30 AM

Well I'm no expert but Input.touches returns an array of all touches. It stores the position of each touch also. If you're positive that the first two touches will always be a pinching gesture you could do something like:

Vector3 center = Input.touches[0].position - Input.touches[1].position;

It would be best not to hard code the array indexes, but that could change depending on your setup. For example in your method before the pinch gesture you could get the Input.touchCount value and then get count+1 and count+2 for the pinch. Something like this could help in the case where you have other buttons/touches going on at the same time.

For the raycast, just after you find the center Vector3 setup Ray and RaycastHit objects then call Physics.Raycast()

Depending on your setup you'll want to use different overloads for this. For example if it is possibly to hit multiple colliders and you don't want to exclude any colliders from being hit then you could do Physics.RaycastAll() which returns a RaycastHit[] for you. If there's some colliders that you don't want to be a part of that raycast, there are overloads for layers in Physics.Raycast()

The RaycastHit stores the rigidbody so it can be rotated that way.

Something like this:

 void Update()
     {
         int touchCount = Input.touchCount;
         Touch[] touches = Input.touches;
         if(touchCount >= 2)
         {
             Vector2 center = touches[touchCount-1].position - touches[touchCount].position;
             
             Ray ray = Camera.main.ScreenPointToRay(center);
             RaycastHit hit;
             if(Physics.Raycast(ray, out hit))
                 hit.rigidbody.rotation = new Quaternion(0,0,0,0);
         }
     }



However, I recommend splitting that up into separate functions so you can reuse them with other things:

 void Update()
 {
     int touchCount = Input.touchCount;
     Touch[] touches = Input.touches;
     if(touchCount >= 2)
     {
         RotateRigidBody(CastRay(touches[touchCount-1].position - touches[touchCount].position).rigidbody);
     }
 }
 
 RaycastHit CastRay(Vector2 point)
 {
     Ray ray = Camera.main.ScreenPointToRay(point);
     RaycastHit hit;        
             
     if(Physics.Raycast(ray, out hit))
         return hit;
     else
         return null;
 }
 
 void RotateRigidBody(Rigidbody body)
 {
     rigidbody.rotation = new Quaternion(0,0,0,0);        
 }
 
 Vector3 FindCenter(Vector3 position1, Vector3 position2)
 {
     return position1 - position2;
 }



One other thing that could make it slightly more efficient is recording when the user does a pinch and call it through a function instead of in Update() this also would ensure that you're getting the right Touch objects from the Input.touches array.

I hope this helps.

Comment
Add comment · Show 1 · 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 Bunny83 · Jun 30, 2012 at 01:04 PM 1
Share

This:

 Vector2 center = touches[touchCount-1].position - touches[touchCount].position;

doesn't calculate the center between those two points. It calculates a relative vector that connects the two points.

The center would be:

 Vector2 center = (touches[touchCount-1].position + touches[touchCount].position) / 2;
avatar image
2

Answer by Bunny83 · Jun 30, 2012 at 10:36 AM

 var touch1 : Vector3;
 var touch2 : Vector3;
 
 var center = (touch1 + touch2) / 2;

It doesn't matter if it's 2d or 3d when they are at the same z pos.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate a 2D sprite without rotating the transform 5 Answers

Time.deltaposition problem on multiple devices 2 Answers

Rotate on drag for IOS? 1 Answer

[2D] How do you rotate a 2D sprite left and right with C#? 2 Answers

how do I create a 3d model viewer app using unity 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