Search Unity

Move object relative to camera move to wrong direction when rotate it

Discussion in 'General Discussion' started by BradenAnwar, Nov 11, 2021.

  1. BradenAnwar

    BradenAnwar

    Joined:
    Nov 11, 2021
    Posts:
    1
    What I'm trying to do is to move an object relative to where the camera is facing.

    I wrote this ugly code so far Unionwells

    Code (CSharp):
    1. private bool ApplyPan_XZ(Vector2 old0, Vector2 new0)
    2. {
    3.    Vector2 panDirection = old0 - new0;
    4.  
    5.    Vector3 newDirection = new Vector3(panDirection.x, 0f, panDirection.y);
    6.  
    7.    if (newDirection.magnitude < PanMagnitudeThreshold)
    8.        return false;
    9.  
    10.    Vector3 movementX = myCamera.right * panDirection.x;
    11.    Vector3 movementZ = myCamera.forward * panDirection.y;
    12.    Vector3 movement = movementX + movementZ;
    13.    movement.y = 0f;
    14.  
    15.    transform.Translate(panSpeed * Time.deltaTime * movement);
    16.  
    17.    return true;
    18. }
    Basically, I get one old position and a new one, I compare the two positions to get the direction, and then multiply the resulting axis to the camera right and camera forward to get the relative position.

    Everything works so far, except when I rotate the transform. In this case, the movement is applied to the forward object for some reason and not anymore to the camera.

    What I tried:

    • Used the last relativeTo of transform.Translate parameter => I need to be relative only on the x and z-axis, and this is applied also to the y axis
    • Changed/converted word/local position and vice-versa
    What I want to accomplish:

    • Move the transform always relative to the camera, even if I rotate it.
    Extra stuff I tried: Stuff More stuff
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,572
    Two problems here:
    Code (csharp):
    1.  
    2. Vector2 panDirection = old0 - new0;
    3.  
    Your movement direction is reversed.

    You're moving the object from new position towards the old one.

    Code (csharp):
    1.  
    2.    Vector3 movementX = myCamera.right * panDirection.x;
    3.    Vector3 movementZ = myCamera.forward * panDirection.y;
    4.    Vector3 movement = movementX + movementZ;
    5.    movement.y = 0f;
    6.  
    You're assuming that movement.y should zero. If your camera is tilted up/down, its movementZ will have non-zero y component. You're erasing that component. If you look directly down, the camera will be stuck.

    If you were trying to move camera above horizontal plane (RTS style, I guess), you can't use camera vectors for movement. You'll either have to calculate ground-relative movement vectors (Vector3.ProjectOnPlane --> https://docs.unity3d.com/ScriptReference/Vector3.ProjectOnPlane.html ) and use those

    OR you need to fire a ray from camera's old and new mouse cursor positions, find intersection points with the ground plane and use those to move.