Search Unity

Android: Moving camera with different rotation values

Discussion in 'Scripting' started by TrinxudasDoZe, Nov 19, 2017.

  1. TrinxudasDoZe

    TrinxudasDoZe

    Joined:
    Sep 10, 2016
    Posts:
    41
    Hey guys,
    So i am coding a simple way to moving the camera on android. However the camera is rotated and because of that when i slide my finger up it moves the camera in a diagonal way. How can i make it so i compensate that rotation and make it actually go up if i slide up? Here's the code:

    Code (CSharp):
    1. if (Input.touchCount == 1) {
    2.             Touch touchZero = Input.GetTouch (0);
    3.             Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
    4.             float deltaMagnitudeX = -(touchZeroPrevPos.x - touchZero.position.x) + Mathf.Sin(55);
    5.             float deltaMagnitudeZ = -(touchZeroPrevPos.y - touchZero.position.y) + Mathf.Sin(55);
    6.             Camera.main.transform.position = new Vector3(Camera.main.transform.position.x + ((deltaMagnitudeX * 0.01f)),Camera.main.transform.position.y,Camera.main.transform.position.z + (deltaMagnitudeZ * 0.01f));
    7.         }

    PS: The camera's rotation is 35,135,0
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    It might be worth using https://docs.unity3d.com/ScriptReference/Transform.Translate.html to move your camera. By doing that you can specify that you want it to move in 'World' space rather than 'Self' space. (or the other way around, I'm not 100% on what effect you're trying to achieve)

    e.g.

    Code (CSharp):
    1. Camera.main.transform.Translate( new Vector3( ( deltaMagnitudeX * 0.01f ) , 0.0f , ( deltaMagnitudeZ * 0.01f ) ) , Space.World );