hello, I am currently finishing up a 3D drag and drop script, everything works fine, but when I change the rotation of the camera the draggable object moves in a diagonal way along the Z axis, but I still want it to move in a straight up and down way no matter what the camera rotation is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseDrag : MonoBehaviour {
Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody>();
}
private void OnMouseDrag()
{
Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.x); // Y and Z did not work so I endet up using X for Camera.main.transform.position.x
transform.position = Camera.main.ScreenToWorldPoint(mousePosition);
}
}
You’re creating a “mouse position” that is kind of nonsense so I’m not sure what you expect. You put in X = x, Y = y and Z = camera X". No idea what that’s supposed to mean. Z = X?
Shouldn’t Z be a constant like zero? You need to choose a constant Z plane surely? Why would you make it the camera X? Do you understand what ScreenToWorld means? If you’re using a perspective view then there are infinite points that intersect a screen point (along the Z in your view). You need to chose a specific Z value which then gives you a specific Z plane and a single point.
I will also add, that you asked this in the physics sub-forum but your question has nothing whatsoever related to physics in it if you ignore the fact that you grab a Rigidbody in your script but don’t use it. This also highilghts yet another issue, you add a Rigidbody which means you want it to drive the Transform yet you then stomp over the Transform yourself so you need to make your mind up what you want here. Use the Rigidbody API to effect changes in pose, NOT the Transform. This isn’t the source of your error though.