Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Translating camera by ignoring rotation

Discussion in 'Scripting' started by tolgatanriverdi, Oct 25, 2013.

  1. tolgatanriverdi

    tolgatanriverdi

    Joined:
    Jan 30, 2013
    Posts:
    33
    Hi
    I'm using below script to translate my main camera . Everything works fine while the camera rotation vector was (0,0,0) however when I change my camera's rotatioin to Quaternion.Euler(40,0,0) it starts to act weird. Because it uses X axis according to new rotation vector.
    But what I want is to translate camera with ignoring rotation values. Is something like that possible ?
    Should I have to convert touch point to local coordinate somehow or should I have to do something?

    Code (csharp):
    1.  
    2.         if (Input.touchCount == 1 )
    3.         {                
    4.             Touch touch = Input.GetTouch(0);
    5.             switch (touch.phase)
    6.             {
    7.                 case TouchPhase.Began:
    8.                     moveVec = Vector2.zero;
    9.                
    10.                 break;    
    11.                 case TouchPhase.Moved:
    12.                    
    13.                     Vector2 deltaPos = touch.deltaPosition;
    14.                     moveVec = new Vector3(-1*deltaPos.x,0,-1*deltaPos.y);
    15.  
    16.                     //moveVec = new Vector3(-1*ray.direction.x,0,-1*ray.direction.z);
    17.                
    18.                 break;    
    19.                 case TouchPhase.Ended:
    20.                     worldCreator.updateCurrentCoordinates(Camera.main.transform.position.x,Camera.main.transform.position.z);
    21.                     moveVec = Vector2.zero;
    22.                 break;
    23.             }
    24.        
    25.             Vector3 deltaVector = moveVec*Time.deltaTime;
    26.             Camera.main.transform.Translate(deltaVector);
    27.            
    28.             rotating = false;
    29.         }    
    30.  
    Thanks
    Tolga
     
  2. rutter

    rutter

    Joined:
    Mar 21, 2012
    Posts:
    84
  3. tolgatanriverdi

    tolgatanriverdi

    Joined:
    Jan 30, 2013
    Posts:
    33
    But if I choose Space.World it always translate the camera according to world coordinates
    What I actually want is ignoring only the X parameter of the rotation
    so when I changed the y paramter of the rotation and try to move forward I want to go according to that rotation.
    All I want is just simply ignore the X parameter of the rotation while calculating the new position.
    But I couldnt find a proper way to do it

    Thanks
     
  4. tolgatanriverdi

    tolgatanriverdi

    Joined:
    Jan 30, 2013
    Posts:
    33
    any other opinion?
     
  5. PetahNZ

    PetahNZ

    Joined:
    Jan 3, 2014
    Posts:
    1
    Did you figure it out?
     
  6. impdesign

    impdesign

    Joined:
    Feb 9, 2016
    Posts:
    1
    My fix for this solution was to fix the position right after the translate, so something like this
    Code (CSharp):
    1. transform.Translate(new Vector3(x_axis, y_axis, z_axis));
    2. if(!changingY)
    3. {
    4.     transform.position = new Vector3(transform.position.x, heightCopy, transform.position.z);
    5. }
    Where changingY is some boolean that is true if the button a player pressed was a button that that you actually wanted to change the height for, and where heightCopy is going to store the transform.position.y of the camera before you start moving it.
     
  7. matta1989

    matta1989

    Joined:
    May 20, 2013
    Posts:
    27
    Im having the same problem here, I want my camera to translate around my local space, because if it is translating around world space it will not catch camera rotation and it will move in the world direction up, down, left right. Which is not what I want, I need it to move around itself so that when i rotate my camera I can still move forward in the way the camera is pointing.

    I have tried to set the movement.y = Camera.main.transform.position.y; without any luck.

    Does someone have a better fix than to use a boolean ?
     
  8. matta1989

    matta1989

    Joined:
    May 20, 2013
    Posts:
    27
    Okei I got it.

    Simple fix:


    Code (CSharp):
    1. Vector3 movement = Vector3.zero;
    2.  
    3. movement.y += movement.z;
    4.  
    5. transform.Translate(movement *Time.deltaTime * moveSpeed);
    Thats what I did to lock the movement.y axis on the z axis so that it stayed at the same position, makes my movement vector normalize on y relative to the z axis.