Search Unity

Move object position relative to camera

Discussion in 'Scripting' started by SiMULOiD, Dec 6, 2020.

  1. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Hi all,

    I've been trying without success to modify the below dragging script, and enable it to move an object relative to Camera.main.
    I thought I needed to follow the camera's Eular angles (Camera.main.transform.eulerAngles.y), but didn't see any results. Next I tried to affect the object's position transform by following the camera's forward direction: (Camera.main.transform.forward), but didn't appear to do anything.

    Any suggestions would be appreciated.
    Thanks.


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class DragRelativeToCamera : MonoBehaviour
    4.  
    5. {
    6.  
    7.     bool bDragging = false;
    8.     Vector3 oldPos;
    9.     Vector3 panOrigin;
    10.     float panSpeed = 26;
    11.  
    12.     public void Update()
    13.     {
    14.  
    15.  
    16.         if (Input.GetMouseButtonDown(1))
    17.  
    18.         {
    19.  
    20.             bDragging = true;
    21.             oldPos = transform.position;
    22.             panOrigin = Camera.main.ScreenToViewportPoint(Input.mousePosition);
    23.  
    24.         }
    25.  
    26.         if (Input.GetMouseButton(1))
    27.         {
    28.  
    29.             Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
    30.             pos = pos - panOrigin;
    31.             transform.position = oldPos + -pos * panSpeed;
    32.        
    33.  
    34.         }
    35.  
    36.         if (Input.GetMouseButtonUp(1))
    37.         {
    38.             bDragging = false;
    39.         }
    40.     }
    41. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Take a moment to review this function: Camera.ScreenToViewportPoint

    It needs a nonzero Z value, which is: "The z position is in world units from the camera."

    If you are dragging on a collider or plane on the ground, you want to do a Raycast instead to get your positions, then just update from that hit.point. See the raycast sample code in the docs.

    Also, I don't know what panSpeed is but it should probably not be there, because then your drag position won't match your intended drag position.

    As far as rotating the camera, it shouldn't matter: if you properly use either ScreenToViewportPoint, or else as I suggested raycasting, the hit point already takes all that into account because you use the camera to make the ray out of the mouse position.
     
  3. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    I would go with the Raycast suggestion, but don't think that would work considering I won't have a collider to hit above the ground plane. If I want to drag my objet by mousing over the space above the ground plane (sky), it wouldn't hit anything.
    Appreciate the help!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    You can raycast in three different ways, completely different APIs within Unity:

    Physics.Raycast
    Collider.Raycast
    Plane.Raycast

    It's possible the third option might be what you want.