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 Letos009 · Jul 04, 2013 at 01:08 PM · androidrotationzoomtouching

Android touch and go,zoom,rotation

public float speed = 0.1f; Vector3 position; bool go; void Update() {

     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { go = true; }
     if (go)
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
         RaycastHit hit;

         if (Physics.Raycast(ray, out hit, Mathf.Infinity))
         {
             if (hit.collider.name == "land")                          //и если этот объект имеет имя "land", то
             {
                 position = hit.point;
             }
         }
         Vector3 direction = position - transform.position;
         float targ_pos = Vector3.Distance(transform.position, position);

         for (float a = 0; a < targ_pos; a++)
         {
             if (targ_pos > 1)
             {
                 transform.Translate(direction * speed, Space.World);
             }
             else go = false;
         }
        
     }
 }

my Cube has this script, when i touch the plane(with name "land") cube must be go in touch position, because he teleport there.

using UnityEngine; using System.Collections;

public class CameraScript : MonoBehaviour { // двигаем камеру private bool drag = false; // масштабируем private bool zoom = false;

 // экранные координаты начальной точки касания
 private Vector3 initialTouchPosition;
 // мировые координаты камеры при инициировании
 // перемещения/масштабирования
 private Vector3 initialCameraPosition;

 // экранные координаты начальной точки первого касания
 private Vector3 initialTouch0Position;
 // экранные координаты начальной точки второго касания
 private Vector3 initialTouch1Position;
 // средняя точка между начальными координатами касаний
 private Vector3 initialMidPointScreen;
 // ортогональный размер камеры на момент начала масштабирования
 private float initialOrthographicSize;

 // Use this for initialization
 void Start()
 {

 }

 // Update is called once per frame
 void Update()
 {
     if (Input.touchCount == 1)
     {
         zoom = false;
         Touch touch0 = Input.GetTouch(0);

         if (IsTouching(touch0))
         {
             if (!drag)
             {
                 initialTouchPosition = touch0.position;
                 initialCameraPosition = this.transform.position;

                 drag = true;
             }
             else
             {
                 Vector2 delta = camera.ScreenToWorldPoint(touch0.position) -
                                 camera.ScreenToWorldPoint(initialTouchPosition);

                 Vector3 newPos = initialCameraPosition;
                 newPos.x -= delta.x;
                 newPos.y -= delta.y;

                 this.transform.position = newPos;
             }
         }

         if (!IsTouching(touch0))
         {
             drag = false;
         }
     }
     else
     {
         drag = false;
     }

     if (Input.touchCount == 2)
     {
         drag = false;

         Touch touch0 = Input.GetTouch(0);
         Touch touch1 = Input.GetTouch(1);

         if (!zoom)
         {
             initialTouch0Position = touch0.position;
             initialTouch1Position = touch1.position;
             initialCameraPosition = this.transform.position;
             initialOrthographicSize = Camera.main.fieldOfView;
             initialMidPointScreen = (touch0.position + touch1.position) / 2;

             zoom = true;
         }
         else
         {
             this.transform.position = initialCameraPosition;
             camera.fieldOfView = initialOrthographicSize;

             float scaleFactor = GetScaleFactor(touch0.position,
                                                touch1.position,
                                                initialTouch0Position,
                                                initialTouch1Position);

             Vector2 currentMidPoint = (touch0.position + touch1.position) / 2;
             Vector3 initialPointWorldBeforeZoom = camera.ScreenToWorldPoint(initialMidPointScreen);

             Camera.main.fieldOfView = initialOrthographicSize / scaleFactor;

             Vector3 initialPointWorldAfterZoom = camera.ScreenToWorldPoint(initialMidPointScreen);
             Vector2 initialPointDelta = initialPointWorldBeforeZoom - initialPointWorldAfterZoom;

             Vector2 oldAndNewPointDelta =
                 camera.ScreenToWorldPoint(currentMidPoint) -
                 camera.ScreenToWorldPoint(initialMidPointScreen);

             Vector3 newPos = initialCameraPosition;
             newPos.x -= oldAndNewPointDelta.x - initialPointDelta.x;
             newPos.y -= oldAndNewPointDelta.y - initialPointDelta.y;

             this.transform.position = newPos;
         }
     }
     else
     {
         zoom = false;
     }
 }

 static bool IsTouching(Touch touch)
 {
     return touch.phase == TouchPhase.Began ||
             touch.phase == TouchPhase.Moved ||
             touch.phase == TouchPhase.Stationary;
 }

 public static float GetScaleFactor(Vector2 position1, Vector2 position2, Vector2 oldPosition1, Vector2 oldPosition2)
 {
     float distance = Vector2.Distance(position1, position2);
     float oldDistance = Vector2.Distance(oldPosition1, oldPosition2);

     if (oldDistance == 0 || distance == 0)
     {
         return 1.0f;
     }

     return distance / oldDistance;
 }

}

this code zoom camera succesfuly? how i can update it that he also rotate camera (onle x and y)

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

15 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

Related Questions

Check accelerometer's rotation Android 2 Answers

How can I rotate my player with a Joystick? 0 Answers

How to run unity app like a "live wallpaper" on android? 0 Answers

Is there any Speech/Voice recognition that is available in android also in IOS platform? 0 Answers

How to Move the rotated Game Object Forward 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