Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
2
Question by TheStarbox · Oct 09, 2014 at 05:28 AM · rotationinputtouchscreenwith

How can ı rotate camera with touch?

Hi everyone ı want to rotate camera with touch.

ı tried this logic:

 Function Update()
 {
 İf(Input.GetTouch(0).phase==TouchPhase.Began)
 {
  var firstposint:Transform;
  firstpoint.position=Input.GetTouch(0).position;
  
 
 }
 if(Input.GetTouch(0).phase==TouchPhase.Stationy)
 {
   var seconpoint:Transform;
   seconpoint.position=Input.GetTouch(0).position;
   this.camera.transform.rotation.x += seconpoint.position.x - firstpoint.position.x;
   this.camera.transform.rotation.y += seconpoint.position.y - firstpoint.position.y;
 }
 }

this isnt work! ı dont understand ı am getting to first touch position for example firstpoint=20 and while ı was touching to screen ı am getting position of second touch for example seconpoint=40

and ı am setting to them differences to rotation of camera! where ı am wrong? pls help me!

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 Landern · Oct 09, 2014 at 05:29 AM 0
Share

How is your script even building? on line 10:

 if(Input.GetTouch(0).phase==TouchPhase.Stationy)

"Stationy" isn't a enum member of TouchPhase, TouchPhase.Stationary would be the correct enum that you're looking for to begin with.

7 Replies

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

Answer by zharik86 · Oct 09, 2014 at 06:08 AM

Method transform.rotation is Quaternion. For more information about quaternion see docs. I correct your code:

  private var firstpoint:Vector3; //change type on Vector3
  private var secondpoint:Vector3;

  private var xAngle: float = 0.0; //angle for axes x for rotation
  private var yAngle: float = 0.0;
  private var xAngTemp: float = 0.0; //temp variable for angle
  private var yAngTemp: float = 0.0;

  function Start() {
   //Initialization our angles of camera
   xAngle = 0.0;
   yAngle = 0.0;
   this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0);
  }

  function Update() {
   //Check count touches
   if(Input.touchCount > 0) {
    //Touch began, save position
    if(Input.GetTouch(0).phase == TouchPhase.Began) {
     firstpoint = Input.GetTouch(0).position;
     xAngTemp = xAngle;
     yAngTemp = yAngle;
    }
    //Move finger by screen
    if(Input.GetTouch(0).phase==TouchPhase.Moved) {
     secondpoint = Input.GetTouch(0).position;
     //Mainly, about rotate camera. For example, for Screen.width rotate on 180 degree
     xAngle = xAngTemp + (secondpoint.x - firstpoint.x) * 180.0 / Screen.width;
     yAngle = yAngTemp - (secondpoint.y - firstpoint.y) *90.0 / Screen.height;
     //Rotate camera
     this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0);
    }
   }
  }

Attach this script at Main Camera on your scene. Of course, you may to use Touch.deltaPosition, but I write common method. I hope, that it will help you.

Comment
Add comment · Show 6 · 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 rafaelrfz · Mar 16, 2015 at 10:10 PM 0
Share

@zharik86 how this works. the camera rotate from some point. If the perspective camera is Orthographic, works good?.

avatar image Karios · Nov 23, 2015 at 08:41 AM 0
Share

It work's Pretty nicely just need to add your personal constraits

avatar image magma6770 · Mar 03, 2018 at 01:50 AM 2
Share
 Vector3 FirstPoint;
 Vector3 SecondPoint;
 float xAngle;
 float yAngle;
 float xAngleTemp;
 float yAngleTemp;

 void Start () {
     xAngle = 0;
     yAngle = 0;
     this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0);        
 }
 
 void Update () {
     if(Input.touchCount > 0){
         if(Input.GetTouch(0).phase == TouchPhase.Began){
             FirstPoint = Input.GetTouch(0).position;
             xAngleTemp = xAngle;
             yAngleTemp = yAngle;
         }
         if(Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved){
             SecondPoint = Input.GetTouch(0).position;
             xAngle = xAngleTemp + (SecondPoint.x - FirstPoint.x) * 180 / Screen.width;
             yAngle = yAngleTemp + (SecondPoint.y - FirstPoint.y) * 90 / Screen.height;
             this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
         }
     }
     
 }

Here's the code provided in C# format

avatar image IgorNovik magma6770 · Oct 11, 2020 at 09:01 PM 0
Share

It is works fine! Thank you man. I clamped yAngle in this line for avoid overflip camera. this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);

avatar image AhmadYusaf magma6770 · Nov 24, 2020 at 08:21 AM 0
Share

Bro can you please tell me how to make the camera rotate only in between fixed angles in y axis .. this code is super helpful but the camera inverts scene in y axis if rotated too much .. Please Help $$anonymous$$e Bro.

avatar image bonannialberto magma6770 · Feb 01, 2021 at 12:38 AM 0
Share

Although this is pretty old, I am currently using a tweaked version of this code, apparently one of the few reasonable options to have touch inputs working on webgl. Despite working pretty good on iOS, it's got a bit of an offset on Android: touch.phase == began angles for both axis seems not to be stored, and every time the rotation of the camera starts from a greater x or y position than the one my finger is holding onto. It seems to have two possible reasons: a problem of sensibility (touching the screen extremely lightly seems to occasionally avoid this "jump") and a began phase that starts from the edges of the screen: when my finger is stationary in the middle, and makes little movements, jumps are almost unnoticeable.

I am trying to clamp the xAngTemp in the began phase as follows:

firstpoint = Input.GetTouch(0).position; xAngTemp = $$anonymous$$athf.Clamp(xAngle, $$anonymous$$imumX, maximumX);

I still can't make it work. Any suggestion?

avatar image
1

Answer by MonkeyPuzzle · Feb 28, 2017 at 07:50 AM

This code really helped me a lot! I tried a few different things and this turned out to be the right answer for my project. I did have one issue. When I rotated my camera on the y-axis until it was upside down, the x-axis was reversed. Here is my update to solve the issue. I am also clamping the values to be 0 to 360. My code is also for rotating the Player, not a camera, but that can be easily changed.

My project is written in c#. @zharik86

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 
 public class touchCam : MonoBehaviour {
 
     private Vector3 firstpoint; 
     private Vector3 secondpoint;
     private float xAngle = 0.0f; //angle for axes x for rotation
     private float yAngle = 0.0f;
     private float xAngTemp = 0.0f; //temp variable for angle
     private float yAngTemp = 0.0f;
     private GameObject playRot;
 
 
     void  Start() {
         
         dtext=GameObject.Find("debugText").GetComponent<Text>();
 
         firstpoint = new Vector3 (0, 0, 0);
         secondpoint = new Vector3 (0, 0, 0);
 
         playRot = GameObject.Find ("Player");
     
         xAngle = 0.0f; 
         yAngle = 0.0f;
    
         transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
     }
 
     void Update() {
         //Check count touches
         if (PlayerController.instance.TabletMode == 1) {
             if (Input.touchCount > 0) {
                 //Touch began, save position
                 if (Input.GetTouch (0).phase == TouchPhase.Began) {
                     firstpoint = Input.GetTouch (0).position;
                     xAngTemp = xAngle;
                     yAngTemp = yAngle;
                 }
                 //Move finger by screen
                 if (Input.GetTouch (0).phase == TouchPhase.Moved) {
                     secondpoint = Input.GetTouch (0).position;
                     //Mainly, about rotate camera. For example, for Screen.width rotate on 180 degree
                     yAngle = yAngTemp - (secondpoint.y - firstpoint.y) * 90.0f / Screen.height;
 
                     if (yAngle < 0)
                         yAngle  +=360;
                     if (yAngle > 360)
                         yAngle  -=360;
 
                     if (yAngle > 90 && yAngle < 270)
                         xAngle = xAngTemp - (secondpoint.x - firstpoint.x) * 180.0f / Screen.width;
                     else
                         xAngle = xAngTemp + (secondpoint.x - firstpoint.x) * 180.0f / Screen.width;
 
                     if (xAngle < 0)
                         xAngle  +=360;
 
                     if (xAngle > 360)
                         xAngle  -=360;
                     
                     transform.rotation = Quaternion.Euler (yAngle, xAngle, 0.0f);
 
                 }
             }
         }
     }
 }

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
1

Answer by rajannath · Dec 15, 2017 at 02:09 PM

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class rotate : MonoBehaviour {

 float rotSpeed =20;

 void OnMouseDrag ()
 {
     float rotX = Input.GetAxis("Mouse X")*rotSpeed*Mathf.DegZRad;
     float rotY = Input.GetAxis("Mouse Y")*rotSpeed*Mathf.DegZRad;

     transform.RotateAround(Vector3.up, -rotX);
     transform.RotateAround(Vector3.right, rotY);
 }

}

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 Neogene · Apr 19, 2018 at 06:38 AM 0
Share

Note is $$anonymous$$athf.Deg2Rad and not $$anonymous$$athf.degZRad;

avatar image
1

Answer by kaksaveksaka · Oct 23, 2021 at 08:16 AM

 {
     Vector3 FirstPoint;
     Vector3 SecondPoint;
     float xAngle;
     float yAngle;
     float xAngleTemp;
     float yAngleTemp;
     public float ymax;
     public float ymin;
     
 
 
     void Start()
     {
        
 
         xAngle = 0;
         yAngle = 0;
         
 
     }
 
     void Update()
     {
        
         if (Input.touchCount > 0)
         {
             bool yclamp = false;
             if (Input.GetTouch(0).phase == TouchPhase.Began)
             {
                 FirstPoint = Input.GetTouch(0).position;
                 xAngleTemp = xAngle;
                 if(!yclamp)
                 yAngleTemp = yAngle;
             }
             if (yAngle > ymax && yAngle - yAngleTemp > 0 || yAngle < ymin && yAngle - yAngleTemp < 0)
             {
                 yclamp = true;
 
                 if (Input.GetTouch(0).phase == TouchPhase.Moved)
                 {
                     SecondPoint = Input.GetTouch(0).position;
                     xAngle = xAngleTemp + (SecondPoint.x - FirstPoint.x) * 180 / Screen.width;
                     // yAngle = yAngleTemp + (SecondPoint.y - FirstPoint.y) * 90 / Screen.height;
                     this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
                 }
             }
             else
             {
                 if (Input.GetTouch(0).phase == TouchPhase.Moved)
                 {
                     SecondPoint = Input.GetTouch(0).position;
                     xAngle = xAngleTemp + (SecondPoint.x - FirstPoint.x) * 180 / Screen.width;
                     yAngle = yAngleTemp + (SecondPoint.y - FirstPoint.y) * 90 / Screen.height;
                     this.transform.rotation = Quaternion.Euler(yAngle, xAngle, 0.0f);
                     yclamp = false;
                 }
 
 
                 Debug.Log(yAngle);
             }
         }
 
     }
    
 }


@Jaskirat_Games this works smoothly for me to clamp y axis

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 Jaskirat_Games · Aug 31, 2019 at 06:49 AM

The above code nearly solved my problem but could anybody help me to clamp the rotation in x and y axis.@zharik86 .

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 vraj95soni · Mar 31, 2020 at 08:38 PM 0
Share

xAngle = $$anonymous$$athf.clamp(xAngle,-180f,180f); you can change the restrictions based on your requirements and can use the same code for yAngle as well by substituting xAngle with yAngle

  • 1
  • 2
  • ›

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

19 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

Related Questions

Different touches on screen 1 Answer

Advanced Unity: Custom touch input origin 1 Answer

Samsung 42" Touch Screen: Touch response issues 1 Answer

2D Rotation Lerp Back To 0 Degrees, Please Help 2 Answers

Input.GetTouch(0) doesn't work 2 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