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

Question Drag and drop script from multiple camera angles?

Discussion in 'Editor & General Support' started by a-d-h-a-m, Jan 22, 2023.

  1. a-d-h-a-m

    a-d-h-a-m

    Joined:
    Mar 28, 2020
    Posts:
    4
    I am new to Unity and I have a script that changes camera angle to a new position on a click of a button. I want to add the ability to drag and drop various objects from these multiple camera angles. The meshes and colliders on the object and its children are all setup.
    upload_2023-1-22_16-1-56.png


    This is the code for switching the camera on the button click:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class SwitchCameraPosition : MonoBehaviour
    {
    public Transform cameraTarget1;
    public Transform cameraTarget2;
    public Transform cameraTarget3;
    public Transform cameraTarget4;
    public float sSpeed = 10.0f;
    public Vector3 dist;
    public Transform lookTarget;
    private int currenttarget;
    private Transform cameraTarget;
    // Start is called before the first frame update
    void Start()
    {
    currenttarget = 1;
    SetCameraTarget(currenttarget);
    }
    // Update is called once per frame
    void Update()
    {
    }
    void FixedUpdate()
    {
    Vector3 dPos = cameraTarget.position + dist;
    Vector3 sPos = Vector3.Lerp(transform.position, dPos, sSpeed * Time.deltaTime);
    transform.position = sPos;
    transform.LookAt(lookTarget.position);
    //Camera.main.transform.position = Vector3.Lerp(transform.position, dPos, sSpeed * Time.deltaTime);
    }
    public void SetCameraTarget(int num)
    {
    switch (num)
    {
    case 1:
    cameraTarget = cameraTarget1.transform;
    break;
    case 2:
    cameraTarget = cameraTarget2.transform;
    break;
    case 3:
    cameraTarget = cameraTarget3.transform;
    break;
    case 4:
    cameraTarget = cameraTarget4.transform;
    break;
    }
    }
    public void SwitchCamera()
    {
    if (currenttarget < 4)
    currenttarget++;
    else
    currenttarget = 1;
    SetCameraTarget(currenttarget);
    }
    }

    This is the code for the drag and drop script that I have so far although it doesn't work even on the main camera. It was working without the different camera angles. How would i go about making the script function with different locations?

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class DragObject : MonoBehaviour
    {
    private Vector3 mOffset;
    private float mZCoord;

    void OnMouseDown()
    {

    mZCoord = Camera.main.WorldToScreenPoint(
    gameObject.transform.position).z;
    // Store offset = gameobject world pos - mouse world pos
    mOffset = gameObject.transform.position - GetMouseAsWorldPoint();
    }
    private Vector3 GetMouseAsWorldPoint()
    {
    // Pixel coordinates of mouse (x,y)
    Vector3 mousePoint = Input.mousePosition;
    // z coordinate of game object on screen
    mousePoint.z = mZCoord;
    // Convert it to world points
    return Camera.main.ScreenToWorldPoint(mousePoint);
    }
    void OnMouseDrag()
    {
    transform.position = GetMouseAsWorldPoint() + mOffset;
    }
    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,894
    First, this seems like a scripting problem.

    What does "it doesn't work" mean? Be specific. What happens when you try it? Are there errors? Unable to help you without details.
     
  3. a-d-h-a-m

    a-d-h-a-m

    Joined:
    Mar 28, 2020
    Posts:
    4
    I drag the script onto the camera and I am not able to drag any of the items on my screen. Nothing happens when I click and drag, not even an error.
    upload_2023-1-22_18-14-10.png
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,894
    What exactly are you trying to drag where?
    Note I do see at least one error in your console, which you should have open.
     
  5. a-d-h-a-m

    a-d-h-a-m

    Joined:
    Mar 28, 2020
    Posts:
    4

    I am trying to drag the individual parts of the object (it's a printer fuser) freely. Like clicking on parts of the overall object and being able to move the peices around freeform from different angles. There are different parts connected to the main fuser. How would I get unity to detect if I clicked on those parts and allow me to drag the indiviudal objects around?